1use crate::JsString;
32use crate::sparse_collection::SparseCollection;
33
34use crate::input::{DataFingerprintInput, PropertyEntryInput, PropertyValue, TypeAssignmentInput};
35use crate::js_json::{cmp_sorted, non_finite_token, JsonValue};
36
37#[path = "fingerprint_checked.rs"]
38mod checked;
39pub use checked::{
40 build_component_fingerprint_entries, build_component_fingerprint_entries_checked,
41 build_component_fingerprints, build_data_fingerprint, build_data_fingerprint_checked,
42 check_property_sets, check_quantity_sets, check_type_assignments,
43};
44
45#[path = "fingerprint_pair.rs"]
46mod pair;
47pub use pair::build_fingerprint_pair_checked;
48
49#[derive(Debug, Clone, PartialEq)]
51enum NormalizedValue {
52 Null,
53 Bool(bool),
54 Number(f64),
56 String(JsString),
57}
58
59impl NormalizedValue {
60 fn to_json(&self) -> JsonValue {
61 match self {
62 NormalizedValue::Null => JsonValue::Null,
63 NormalizedValue::Bool(value) => JsonValue::Bool(*value),
64 NormalizedValue::Number(value) => JsonValue::Number(*value),
65 NormalizedValue::String(value) => JsonValue::String(value.clone()),
66 }
67 }
68}
69
70fn normalize_value(value: &PropertyValue) -> NormalizedValue {
83 match value {
84 PropertyValue::Null => NormalizedValue::Null,
85 PropertyValue::Bool(flag) => NormalizedValue::Bool(*flag),
86 PropertyValue::Number(number) if number.is_finite() => NormalizedValue::Number(*number),
87 _ => NormalizedValue::String(canonical_value_string(value).expect("text-normalized value")),
88 }
89}
90
91pub fn canonical_value_string(value: &PropertyValue) -> Option<JsString> {
98 match value {
99 PropertyValue::String(text) => Some(text.clone()),
100 PropertyValue::Number(number) if !number.is_finite() => Some(non_finite_token(*number).into()),
101 PropertyValue::Array(_) | PropertyValue::Object(_) => Some(crate::property_json::stringify_property_sorted(value).into()),
102 _ => None,
103 }
104}
105
106struct NamedEntry {
108 name: JsString,
109 value: NormalizedValue,
110}
111
112impl NamedEntry {
113 fn to_json(&self) -> JsonValue {
114 static KEYS: std::sync::OnceLock<[JsString; 2]> = std::sync::OnceLock::new();
117 let [name, value] = KEYS.get_or_init(|| ["name".into(), "value".into()]);
118 JsonValue::Object(vec![
119 (name.clone(), JsonValue::String(self.name.clone())),
120 (value.clone(), self.value.to_json()),
121 ])
122 }
123}
124
125fn cmp_name_then_content(a: (&JsString, &JsonValue), b: (&JsString, &JsonValue)) -> std::cmp::Ordering {
134 a.0.cmp(b.0).then_with(|| cmp_sorted(a.1, b.1))
137}
138
139fn sorted_entries(entries: &SparseCollection<PropertyEntryInput>) -> Vec<JsonValue> {
140 let mut normalized: Vec<NamedEntry> = entries
141 .present()
142 .map(|entry| NamedEntry {
143 name: entry.name.clone(),
144 value: normalize_value(&entry.value),
145 })
146 .collect();
147 normalized.sort_by(|a, b| {
148 a.name.cmp(&b.name).then_with(|| cmp_sorted(&a.to_json(), &b.to_json()))
150 });
151 with_holes(normalized.into_iter().map(|entry| entry.to_json()).collect(), entries.holes())
152}
153
154fn core_attributes(input: &DataFingerprintInput) -> Vec<(JsString, JsonValue)> {
157 let text = |value: &Option<JsString>| JsonValue::String(value.clone().unwrap_or_default());
158 vec![
159 ("Type".into(), JsonValue::String(input.ifc_type.clone())),
160 ("Name".into(), text(&input.name)),
161 ("Description".into(), text(&input.description)),
162 ("ObjectType".into(), text(&input.object_type)),
163 ("PredefinedType".into(), text(&input.predefined_type)),
164 ("Tag".into(), text(&input.tag)),
165 ]
166}
167
168fn set_to_json(name: &JsString, members_key: &str, members: &[JsonValue]) -> JsonValue {
171 JsonValue::Object(vec![
172 ("name".into(), JsonValue::String(name.clone())),
173 (
174 members_key.into(),
175 JsonValue::Array(members.to_vec()),
176 ),
177 ])
178}
179
180fn sorted_property_sets(input: &DataFingerprintInput) -> Vec<(JsString, JsonValue)> {
181 let mut sets: Vec<(JsString, JsonValue)> = input
182 .property_sets
183 .present()
184 .map(|set| {
185 let members = sorted_entries(&set.properties);
186 (set.name.clone(), set_to_json(&set.name, "properties", &members))
187 })
188 .collect();
189 sets.sort_by(|a, b| cmp_name_then_content((&a.0, &a.1), (&b.0, &b.1)));
190 sets
191}
192
193fn sorted_quantity_sets(input: &DataFingerprintInput) -> Vec<(JsString, JsonValue)> {
194 let mut sets: Vec<(JsString, JsonValue)> = input
195 .quantity_sets
196 .present()
197 .map(|set| {
198 let members = sorted_entries(&set.quantities);
199 (set.name.clone(), set_to_json(&set.name, "quantities", &members))
200 })
201 .collect();
202 sets.sort_by(|a, b| cmp_name_then_content((&a.0, &a.1), (&b.0, &b.1)));
203 sets
204}
205
206fn sorted_type_assignments(types: &SparseCollection<TypeAssignmentInput>) -> Vec<JsonValue> {
217 let mut projected: Vec<(JsString, JsString)> = types
218 .present()
219 .map(|assignment| {
220 (
221 assignment.name.clone().unwrap_or_default(),
222 assignment.type_name.clone().unwrap_or_default(),
223 )
224 })
225 .collect();
226 projected.sort_by(|a, b| a.1.cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
227 let values = projected
228 .into_iter()
229 .map(|(name, type_name)| {
230 JsonValue::Object(vec![
231 ("name".into(), JsonValue::String(name)),
232 ("type".into(), JsonValue::String(type_name)),
233 ])
234 })
235 .collect();
236 with_holes(values, types.holes())
237}
238
239fn with_holes(mut values: Vec<JsonValue>, holes: usize) -> Vec<JsonValue> {
241 if holes > 0 { values.push(JsonValue::Holes(holes)); }
242 values
243}
244
245fn string_array(values: &[JsString]) -> JsonValue {
246 JsonValue::Array(values.iter().cloned().map(JsonValue::String).collect())
247}
248
249#[cfg(test)]
250#[path = "fingerprint_sparse_tests.rs"]
251mod sparse_tests;
252
253#[cfg(test)]
254#[path = "fingerprint_counted_tests.rs"]
255mod counted_tests;
256
257#[cfg(test)]
258#[path = "fingerprint_set_ordinal_tests.rs"]
259mod set_ordinal_tests;