ifc_lite_diff/
transport_result.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//! Result transport contains row indices, never copied adapter references.
5use crate::DocumentValue as Value;
6// Fixed protocol vocabulary is immutable. Share its UTF-16 storage across rows.
7macro_rules! text {
8    ($value:literal) => {{
9        static VALUE: std::sync::OnceLock<crate::JsString> = std::sync::OnceLock::new();
10        VALUE.get_or_init(|| $value.into()).clone()
11    }};
12}
13macro_rules! json {
14    ({$($key:literal : $value:expr),* $(,)?}) => {
15        Value::Object(vec![$((text!($key), json!($value))),*])
16    };
17    ($value:expr) => { crate::document_serde::to_document(&$value).expect("result DTO serialization") };
18}
19use crate::{engine::ModelDiff,model::{DiffScope,DiffState,DiffChangeKind},content_types::{ContentMatchKind,ContentMatchTier},
20    split_types::{SplitMergeKind,SplitConfidence},successor::SuccessorConfidence};
21pub fn result_to_document(diff: &ModelDiff<'_,usize>) -> Value {
22    result_document(diff, Value::Array(diff.core.entries.iter().map(entry_document).collect()))
23}
24
25/// Metadata for the packed facade writer, which streams entries directly.
26pub fn result_metadata(diff: &ModelDiff<'_, usize>) -> Value {
27    result_document(diff, Value::Array(Vec::new()))
28}
29
30fn result_document(diff: &ModelDiff<'_, usize>, entries: Value) -> Value {
31    let core=&diff.core;
32    // Already-built documents must move into the envelope. Serializing them
33    // through serde again re-allocates every row, key and UTF-16 value (#8042).
34    let mut result = Value::Object(vec![
35        (text!("scope"), json!(match core.scope {DiffScope::Data=>"data",DiffScope::Geometry=>"geometry",DiffScope::Both=>"both"})),
36        (text!("excludedTypes"), strings(&core.excluded_types)),
37        (text!("entries"), entries),
38        (text!("counts"), json!({"added":core.counts.added,"modified":core.counts.modified,"deleted":core.counts.deleted,"unchanged":core.counts.unchanged})),
39    ]);
40    if let Some(aliases)=&core.applied_key_aliases {result["appliedKeyAliases"]=json!(aliases);}
41    if let Some(matches)=&diff.content_matches {
42        result["contentMatches"]=Value::Array(matches.iter().map(|m|{
43            let kind=match m.kind {ContentMatchKind::Renamed=>"renamed",ContentMatchKind::Moved=>"moved",ContentMatchKind::Reshaped=>"reshaped",ContentMatchKind::Respecified=>"respecified",ContentMatchKind::Duplicated=>"duplicated",ContentMatchKind::Deduplicated=>"deduplicated",ContentMatchKind::Ambiguous=>"ambiguous"};
44            let tier=match m.tier {ContentMatchTier::GeometryHash=>"geometry-hash",ContentMatchTier::GeometryOnly=>"geometry-only",ContentMatchTier::ResidueOneToOne=>"residue-1-1",ContentMatchTier::Positional=>"positional",ContentMatchTier::Unresolved=>"unresolved"};
45            let mut v=json!({"kind":kind,"tier":tier,"dataHash":m.data_hash,"base":m.base.iter().map(|e|e.reference).collect::<Vec<_>>(),"head":m.head.iter().map(|e|e.reference).collect::<Vec<_>>()});
46            if let Some(hash)=&m.geometry_hash {v["geometryHash"]=json!(hash);}
47            if let Some(keys)=&m.changed_components {v["changedComponents"]=json!(keys);}
48            if let Some(distance)=m.distance {v["distance"]=json!(distance);}
49            v
50        }).collect());
51    }
52    if let Some(claims)=&diff.split_merges {
53        result["splitMerges"]=Value::Array(claims.iter().map(|c|{
54            let kind=match c.kind {SplitMergeKind::Split=>"split",SplitMergeKind::Merge=>"merge"};
55            let confidence=match c.confidence {SplitConfidence::Verified=>"verified",SplitConfidence::Extent=>"extent",SplitConfidence::Displaced=>"displaced"};
56            let mut v=json!({"kind":kind,"confidence":confidence,"whole":c.whole.reference,"pieces":c.pieces.iter().map(|e|e.reference).collect::<Vec<_>>()});
57            for (key,n) in [("wholeVolume",c.whole_volume),("piecesVolume",c.pieces_volume),("volumeResidual",c.volume_residual)] {if let Some(n)=n {v[key]=json!(n);}}
58            if let Some(e)=c.excluded {v["excluded"]=json!(e.reference);}
59            if c.cross_class {v["crossClass"]=json!(true);}
60            v
61        }).collect());
62    }
63    if let Some(claims)=&diff.successors {
64        result["successors"]=Value::Array(claims.iter().map(|c|{
65            let confidence=match c.confidence {SuccessorConfidence::Footprint=>"footprint",SuccessorConfidence::Position=>"position"};
66            let mut v=json!({"confidence":confidence,"base":c.base.reference,"head":c.head.reference,"overlap":c.overlap,"distance":c.distance});
67            if let Some(keys)=&c.agreeing_components {v["agreeingComponents"]=json!(keys);}
68            if c.cross_class {v["crossClass"]=json!(true);}
69            v
70        }).collect());
71    }
72    result
73}
74
75fn strings(values: &[crate::JsString]) -> Value {
76    Value::Array(values.iter().cloned().map(Value::String).collect())
77}
78
79fn entry_document(entry: &crate::model::DiffEntry<'_, usize>) -> Value {
80    let state = match entry.state {
81        DiffState::Added => text!("added"), DiffState::Deleted => text!("deleted"),
82        DiffState::Modified => text!("modified"), DiffState::Unchanged => text!("unchanged"),
83    };
84    let kinds = entry.change_kinds.iter().map(|kind| Value::String(match kind {
85        DiffChangeKind::Data => text!("data"), DiffChangeKind::Geometry => text!("geometry"),
86        DiffChangeKind::Container => text!("container"),
87    })).collect();
88    let mut fields = Vec::with_capacity(6);
89    fields.push((text!("key"), Value::String(entry.key.clone())));
90    fields.push((text!("state"), Value::String(state)));
91    fields.push((text!("changeKinds"), Value::Array(kinds)));
92    if let Some(base) = entry.base { fields.push((text!("base"), Value::Number(base.reference as f64))); }
93    if let Some(head) = entry.head { fields.push((text!("head"), Value::Number(head.reference as f64))); }
94    if let Some(keys) = &entry.changed_components { fields.push((text!("changedComponents"), strings(keys))); }
95    Value::Object(fields)
96}