ifc_lite_diff/
fingerprint_pair.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4//! Batch snapshots can reuse one canonical projection for both hashes (#8042).
5use crate::{JsString, input::DataFingerprintInput, js_json::{JsonValue, stable_hash_json, utf16_length}};
6use super::checked::{project_data, has_holes, OrderedComponents, SetOrdinals,
7    check_property_sets, check_quantity_sets, check_type_assignments};
8
9/// Equivalent to the data builder followed by the component builder, including
10/// sparse string-length checks in the same order. Inputs are owned DTOs: no
11/// host getter is deferred across either hash. Solo builders remain available
12/// without computing an unwanted second hash.
13pub fn build_fingerprint_pair_checked<E>(input: &DataFingerprintInput,
14    mut check: impl FnMut(u64) -> Result<(), E>) -> Result<(String, Vec<(JsString, String)>), E> {
15    check_property_sets(&input.property_sets, false, &mut check)?;
16    check_quantity_sets(&input.quantity_sets, false, &mut check)?;
17    let (value, core_len) = project_data(input);
18    if has_holes(input) { check(utf16_length(&value, false))?; }
19    let data = stable_hash_json(&value);
20    let JsonValue::Object(mut fields) = value else { unreachable!("canonical data is an object") };
21    // core_attributes owns the prefix; moving it avoids cloning
22    // any property member tree. The remaining projections stay in this owner.
23    let core = JsonValue::Object(fields.drain(..core_len).collect());
24    let mut components = OrderedComponents(Vec::new());
25    components.insert("attr:core".into(), stable_hash_json(&core));
26    check_property_sets(&input.property_sets, true, &mut check)?;
27    assert_eq!(input.property_sets.holes(), 0, "component property sets require present records");
28    sets(&fields, "PropertySets", "pset", &mut components);
29    check_quantity_sets(&input.quantity_sets, true, &mut check)?;
30    assert_eq!(input.quantity_sets.holes(), 0, "component quantity sets require present records");
31    sets(&fields, "QuantitySets", "qset", &mut components);
32    check_type_assignments(&input.type_assignments, true, &mut check)?;
33    for (field, key) in [("TypeAssignments", "type-assignment"), ("Materials", "material"), ("Classifications", "classification")] {
34        if let Some((_, value @ JsonValue::Array(items))) = fields.iter().find(|(name, _)| name == field) {
35            if !items.is_empty() { components.insert(key.into(), stable_hash_json(value)); }
36        }
37    }
38    Ok((data, components.0))
39}
40
41fn sets(fields: &[(JsString, JsonValue)], field: &str, kind: &str, components: &mut OrderedComponents) {
42    let Some((_, JsonValue::Array(sets))) = fields.iter().find(|(name, _)| name == field) else {
43        unreachable!("canonical data always has set collections")
44    };
45    let mut ordinals = SetOrdinals::default();
46    for set in sets {
47        let JsonValue::Object(entries) = set else { unreachable!("outer holes were rejected") };
48        let JsonValue::String(name) = &entries[0].1 else { unreachable!("canonical set name") };
49        components.insert(ordinals.key(kind, name), stable_hash_json(&entries[1].1));
50    }
51}