MCPcopy Create free account
hub / github.com/apache/fory / compute_struct_fingerprint

Function compute_struct_fingerprint

rust/fory-derive/src/object/util.rs:779–826  ·  view source on GitHub ↗

Computes struct fingerprint string at compile time (during proc-macro execution). Fingerprint Format:** ` , , , [ ];` Tagged fields are sorted by numeric ID. Untagged fields are sorted by name lexicographically.

(fields: &[&Field])

Source from the content-addressed store, hash-verified

777/// **Fingerprint Format:** `<field_name_or_id>,<type_id>,<ref>,<nullable>[<child...>];`
778/// Tagged fields are sorted by numeric ID. Untagged fields are sorted by name lexicographically.
779fn compute_struct_fingerprint(fields: &[&Field]) -> String {
780 use super::field_meta::parse_field_meta;
781 use std::cmp::Ordering;
782
783 let mut field_infos: Vec<FieldFingerprintInfo> = Vec::with_capacity(fields.len());
784
785 for (idx, field) in fields.iter().enumerate() {
786 let meta = parse_field_meta(field).unwrap_or_default();
787 if meta.skip {
788 continue;
789 }
790
791 let name = get_field_name(field, idx);
792 let field_id = meta.effective_id();
793 let name_or_id = if field_id >= 0 {
794 field_id.to_string()
795 } else {
796 to_snake_case(&name)
797 };
798
799 field_infos.push(FieldFingerprintInfo {
800 field_id,
801 name_or_id,
802 type_fingerprint: build_type_fingerprint(&field.ty, &meta, true, true),
803 });
804 }
805
806 field_infos.sort_by(|a, b| match (a.field_id >= 0, b.field_id >= 0) {
807 (true, true) => a
808 .field_id
809 .cmp(&b.field_id)
810 .then_with(|| a.name_or_id.cmp(&b.name_or_id)),
811 (true, false) => Ordering::Less,
812 (false, true) => Ordering::Greater,
813 (false, false) => a.name_or_id.cmp(&b.name_or_id),
814 });
815
816 // Build fingerprint string
817 let mut fingerprint = String::new();
818 for info in &field_infos {
819 fingerprint.push_str(&info.name_or_id);
820 fingerprint.push(',');
821 fingerprint.push_str(&info.type_fingerprint);
822 fingerprint.push(';');
823 }
824
825 fingerprint
826}
827
828/// Generates TokenStream for struct version hash (computed at compile time).
829pub(crate) fn gen_struct_version_hash_ts(fields: &[&Field]) -> TokenStream {

Callers 1

Calls 7

parse_field_metaFunction · 0.85
get_field_nameFunction · 0.85
build_type_fingerprintFunction · 0.85
lenMethod · 0.80
effective_idMethod · 0.80
to_snake_caseFunction · 0.50
to_stringMethod · 0.45

Tested by

no test coverage detected