cadmpeg-ir 0.5.3

CAD data model, validation, diffing, and codec API for cadmpeg.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// SPDX-License-Identifier: Apache-2.0
//! Structural comparison of IR documents.
//!
//! Numbers compare through [`cadmpeg_ir::compare`], so a coordinate that
//! differs only in the last place — what the same file decoded under two
//! platforms' libm produces — is not reported as a change, while an integer
//! count, index, or degree that moved by one always is. That module states the
//! tolerance and its caveats, including that the relation is not transitive:
//! every verdict here concerns exactly the two documents passed in.
//!
//! The comparison covers units, tolerances, every model and native arena, and
//! [`crate::document::SourceMeta`] — the source format id and its attributes,
//! where a codec records the program version, the object count, and the rest of
//! what it read out of the container.
//!
//! One class of attribute is carved out. A machine-local digest, named by the
//! [`cadmpeg_ir::compare::LOCAL_DIGEST_SUFFIX`] convention, is a bitwise
//! fingerprint of the very values this module compares tolerantly: two decodes
//! that agree to fourteen significant digits hash differently, and no tolerance
//! can reconcile them. Reporting such a difference as a difference would make
//! every cross-platform comparison of one file report that file as changed.
//! [`SourceDiff::local_digests`] therefore reports them for information, outside
//! [`IrDiff::is_empty`] and outside the exit code derived from it, while every
//! other source attribute counts.
//!
//! A document with no `source` compares as one whose source is
//! [`SourceMeta::default`]: an empty format id and no attributes. Two documents
//! that both lack source metadata therefore agree, and a document that gained a
//! populated one differs.

use std::collections::BTreeMap;

use crate::compare::{floats_agree, is_local_digest_attribute, values_agree};

#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::Serialize;
use serde_json::Value;

use crate::document::SourceMeta;
use crate::CadIr;

/// One differing source attribute.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct AttributeChange {
    /// Attribute key.
    pub key: String,
    /// Value in the left-hand document, absent when only the right-hand document
    /// carries the key.
    pub left: Option<String>,
    /// Value in the right-hand document, absent when only the left-hand document
    /// carries the key.
    pub right: Option<String>,
}

/// Changes in the two documents' source metadata.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct SourceDiff {
    /// `(left, right)` source format ids, present only when they differ.
    pub format_change: Option<(String, String)>,
    /// Differing attributes, each a difference.
    pub attributes: Vec<AttributeChange>,
    /// Differing machine-local digest attributes, reported for information and
    /// never counted as a difference.
    ///
    /// See the module documentation: such a digest cannot agree across platforms,
    /// so a verdict that turned on one would call the same file changed.
    pub local_digests: Vec<AttributeChange>,
}

impl SourceDiff {
    /// Returns `true` when nothing that counts as a difference changed.
    ///
    /// [`Self::local_digests`] is not consulted.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.format_change.is_none() && self.attributes.is_empty()
    }
}

/// A modified entity and its differing top-level fields.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct ModifiedEntity {
    /// Diff key of the entity, as produced by the arena's key function.
    pub id: String,
    /// Names of the top-level entity fields whose JSON-serialized values differ
    /// between the two documents.
    pub fields: Vec<String>,
}

/// Changes within one entity arena.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct ArenaDiff {
    /// Arena name, matching the field name in [`crate::CadIr`] (e.g. `"faces"`).
    pub kind: String,
    /// Diff keys of entities present only in the right-hand document.
    pub added: Vec<String>,
    /// Diff keys of entities present only in the left-hand document.
    pub removed: Vec<String>,
    /// Entities present in both documents with at least one differing field.
    pub modified: Vec<ModifiedEntity>,
}

/// Structural changes between two IR documents.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct IrDiff {
    /// `(left, right)` units, present only when the two documents' units differ.
    pub unit_change: Option<(crate::units::Units, crate::units::Units)>,
    /// `(left, right)` tolerances, present only when the two documents' tolerances differ.
    pub tolerance_change: Option<(crate::units::Tolerances, crate::units::Tolerances)>,
    /// Source-metadata changes, including the informational digest section.
    pub source: SourceDiff,
    /// Per-arena diffs, one entry per arena compared.
    pub per_arena: Vec<ArenaDiff>,
}

impl IrDiff {
    /// Returns `true` when neither units, tolerances, source metadata, nor any
    /// arena differ.
    ///
    /// A difference confined to [`SourceDiff::local_digests`] leaves this `true`.
    pub fn is_empty(&self) -> bool {
        self.unit_change.is_none()
            && self.tolerance_change.is_none()
            && self.source.is_empty()
            && self.per_arena.iter().all(|arena| {
                arena.added.is_empty() && arena.removed.is_empty() && arena.modified.is_empty()
            })
    }
}

/// Top-level field names whose values do not agree, tolerating last-place
/// disagreement in fractional numbers at any depth beneath a field.
///
/// A field present on one side and absent on the other always counts as
/// differing, so an `Option` that gained or lost a value is reported even when
/// the value would have agreed.
fn differing_fields<T: Serialize>(left: &T, right: &T) -> Vec<String> {
    let (Ok(Value::Object(left)), Ok(Value::Object(right))) =
        (serde_json::to_value(left), serde_json::to_value(right))
    else {
        return vec!["value".to_string()];
    };
    left.keys()
        .chain(right.keys())
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .filter(|key| match (left.get(*key), right.get(*key)) {
            (Some(before), Some(after)) => values_agree(before, after).is_err(),
            (None, None) => false,
            _ => true,
        })
        .cloned()
        .collect()
}

fn arena<T, F>(kind: impl Into<String>, left: &[T], right: &[T], identity: F) -> ArenaDiff
where
    T: PartialEq + Serialize,
    F: for<'a> Fn(&'a T) -> &'a str,
{
    let left: BTreeMap<_, _> = left
        .iter()
        .map(|entity| (identity(entity), entity))
        .collect();
    let right: BTreeMap<_, _> = right
        .iter()
        .map(|entity| (identity(entity), entity))
        .collect();
    let removed = left
        .keys()
        .filter(|id| !right.contains_key(*id))
        .map(|id| (*id).to_owned())
        .collect();
    let added = right
        .keys()
        .filter(|id| !left.contains_key(*id))
        .map(|id| (*id).to_owned())
        .collect();
    let modified = left
        .iter()
        .filter_map(|(id, before)| {
            let after = right.get(id)?;
            // Empty differing_fields means every difference was below tolerance.
            if *before == *after {
                return None;
            }
            let fields = differing_fields(*before, *after);
            (!fields.is_empty()).then(|| ModifiedEntity {
                id: (*id).to_owned(),
                fields,
            })
        })
        .collect();
    ArenaDiff {
        kind: kind.into(),
        added,
        removed,
        modified,
    }
}

macro_rules! define_diff_arenas {
    ($( $field:ident: $element:ty, $doc:literal, [$($attribute:meta),*]; )*) => {
        fn diff_arenas(left: &CadIr, right: &CadIr) -> Vec<ArenaDiff> {
            vec![$(arena(
                stringify!($field),
                &left.model.$field,
                &right.model.$field,
                crate::schema::EntitySchema::identity,
            )),*]
        }
    };
}
crate::document::arena_registry!(define_diff_arenas);

fn diff_native_namespaces(left: &CadIr, right: &CadIr) -> Vec<ArenaDiff> {
    let namespaces = left
        .native
        .0
        .keys()
        .chain(right.native.0.keys())
        .collect::<std::collections::BTreeSet<_>>();
    namespaces
        .into_iter()
        .flat_map(|namespace| {
            let left_ns = left.native.namespace(namespace);
            let right_ns = right.native.namespace(namespace);
            let arenas = left_ns
                .into_iter()
                .flat_map(|value| value.arenas.keys())
                .chain(right_ns.into_iter().flat_map(|value| value.arenas.keys()))
                .collect::<std::collections::BTreeSet<_>>();
            arenas.into_iter().map(move |name| {
                arena(
                    format!("native.{namespace}.{name}"),
                    left_ns
                        .and_then(|value| value.arenas.get(name))
                        .map(Vec::as_slice)
                        .unwrap_or_default(),
                    right_ns
                        .and_then(|value| value.arenas.get(name))
                        .map(Vec::as_slice)
                        .unwrap_or_default(),
                    |record| record.id(),
                )
            })
        })
        .collect()
}

/// Whether two tolerance declarations agree, each component within the
/// comparator's tolerance.
fn tolerances_agree(left: crate::units::Tolerances, right: crate::units::Tolerances) -> bool {
    floats_agree(left.linear, right.linear) && floats_agree(left.angular, right.angular)
}

/// Compare the source metadata of two documents, classifying each differing
/// attribute as a difference or as an informational machine-local digest.
fn diff_source(left: &CadIr, right: &CadIr) -> SourceDiff {
    let absent = SourceMeta::default();
    let left = left.source.as_ref().unwrap_or(&absent);
    let right = right.source.as_ref().unwrap_or(&absent);
    let mut result = SourceDiff {
        format_change: (left.format != right.format)
            .then(|| (left.format.clone(), right.format.clone())),
        ..SourceDiff::default()
    };
    let keys = left
        .attributes
        .keys()
        .chain(right.attributes.keys())
        .collect::<std::collections::BTreeSet<_>>();
    for key in keys {
        let before = left.attributes.get(key);
        let after = right.attributes.get(key);
        if before == after {
            continue;
        }
        let change = AttributeChange {
            key: key.clone(),
            left: before.cloned(),
            right: after.cloned(),
        };
        if is_local_digest_attribute(key) {
            result.local_digests.push(change);
        } else {
            result.attributes.push(change);
        }
    }
    result
}

/// Compare units, tolerances, source metadata, and every entity arena by stable
/// entity ID.
///
/// Fractional numbers compare within the tolerance stated by
/// [`cadmpeg_ir::compare`]; integers, strings, enums, and structure
/// compare exactly. Source attributes are strings and compare exactly; a
/// machine-local digest among them is reported without counting as a difference.
pub fn diff(left: &CadIr, right: &CadIr) -> IrDiff {
    // `Units` carries only the `LengthUnit` enum, so exact comparison is the
    // correct relation for it; there is no float to tolerate.
    let unit_change =
        (left.units != right.units).then(|| (left.units.clone(), right.units.clone()));
    let tolerance_change = (!tolerances_agree(left.tolerances, right.tolerances))
        .then_some((left.tolerances, right.tolerances));
    let mut per_arena = diff_arenas(left, right);
    per_arena.extend(diff_native_namespaces(left, right));
    IrDiff {
        unit_change,
        tolerance_change,
        source: diff_source(left, right),
        per_arena,
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::diff;
    use crate::compare;
    use crate::examples::unit_cube;

    #[test]
    fn detects_changes_in_all_document_dimensions() {
        let left = unit_cube();
        let mut right = left.clone();
        right.model.points[0].position.x += 1.0;
        right.model.loops.pop();
        right.model.coedges.pop();

        let result = diff(&left, &right);
        assert!(!result.is_empty());
        assert_eq!(
            result
                .per_arena
                .iter()
                .find(|a| a.kind == "points")
                .expect("required invariant")
                .modified[0]
                .fields,
            ["position"]
        );
        assert_eq!(
            result
                .per_arena
                .iter()
                .find(|a| a.kind == "loops")
                .expect("required invariant")
                .removed
                .len(),
            1
        );
        assert_eq!(
            result
                .per_arena
                .iter()
                .find(|a| a.kind == "coedges")
                .expect("required invariant")
                .removed
                .len(),
            1
        );
    }

    /// Modified entity IDs in one arena of a diff.
    fn modified(result: &super::IrDiff, kind: &str) -> Vec<String> {
        result
            .per_arena
            .iter()
            .find(|arena| arena.kind == kind)
            .expect("every model arena appears in every diff")
            .modified
            .iter()
            .map(|entity| entity.id.clone())
            .collect()
    }

    /// Index of a point whose `x` is at unit magnitude or above.
    fn scaled_point(ir: &crate::CadIr) -> usize {
        ir.model
            .points
            .iter()
            .position(|point| point.position.x.abs() >= 1.0)
            .expect("the cube fixture places points away from the origin")
    }

    /// A coordinate moved by one unit in the last place, which is what the same
    /// file decoded under two platforms' libm produces.
    #[test]
    fn a_last_place_coordinate_move_is_not_a_difference() {
        let left = unit_cube();
        let mut right = left.clone();
        let index = scaled_point(&left);
        let before = right.model.points[index].position.x;
        let after = f64::from_bits(before.to_bits() + 1);
        assert_ne!(
            before.to_bits(),
            after.to_bits(),
            "the coordinate must move, or this test proves nothing"
        );
        right.model.points[index].position.x = after;

        assert_ne!(
            serde_json::to_value(&left.model.points).unwrap(),
            serde_json::to_value(&right.model.points).unwrap(),
            "the serialized documents must differ, or exact equality would pass too"
        );
        let result = diff(&left, &right);
        assert!(result.is_empty(), "{result:?}");
    }

    /// A tolerance declaration moved in the last place is the same declaration.
    #[test]
    fn a_last_place_tolerance_move_is_not_a_difference() {
        let left = unit_cube();
        let mut right = left.clone();
        right.tolerances.linear = f64::from_bits(left.tolerances.linear.to_bits() + 1);
        assert!(diff(&left, &right).is_empty());

        right.tolerances.linear = left.tolerances.linear * 2.0;
        assert!(diff(&left, &right).tolerance_change.is_some());
    }

    /// A change with physical meaning stays reported. A relative `1e-6` on a
    /// coordinate is six orders above the tolerance.
    #[test]
    fn a_genuine_coordinate_change_is_still_reported() {
        let left = unit_cube();
        let mut right = left.clone();
        let index = scaled_point(&left);
        let point = &mut right.model.points[index].position;
        point.x = point.x.mul_add(1e-6, point.x);

        let result = diff(&left, &right);
        assert!(!result.is_empty());
        assert_eq!(
            modified(&result, "points"),
            [left.model.points[index].id.0.clone()]
        );
    }

    /// An integer field is never tolerated, however large its magnitude and
    /// however small the change relative to it.
    #[test]
    fn an_integer_field_differing_by_one_is_always_reported() {
        use crate::geometry::{Curve, CurveGeometry, NurbsCurve};
        use crate::ids::CurveId;
        use crate::math::Point3;

        let nurbs = |degree: u32| Curve {
            id: CurveId("synthetic:tolerance:curve#nurbs".into()),
            geometry: CurveGeometry::Nurbs(NurbsCurve {
                degree,
                knots: vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
                control_points: vec![
                    Point3::new(0.0, 0.0, 0.0),
                    Point3::new(1.0, 0.0, 0.0),
                    Point3::new(2.0, 0.0, 0.0),
                ],
                weights: None,
                periodic: false,
            }),
            source_object: None,
        };

        let mut left = unit_cube();
        let mut right = left.clone();
        left.model.curves.push(nurbs(2));
        right.model.curves.push(nurbs(3));

        let result = diff(&left, &right);
        assert!(!result.is_empty());
        assert_eq!(
            modified(&result, "curves"),
            ["synthetic:tolerance:curve#nurbs"]
        );
    }

    /// A cube carrying source metadata with the given attributes.
    fn with_source(attributes: &[(&str, &str)]) -> crate::CadIr {
        let mut ir = unit_cube();
        ir.source = Some(crate::document::SourceMeta {
            format: "synthetic".into(),
            attributes: attributes
                .iter()
                .map(|(key, value)| ((*key).to_owned(), (*value).to_owned()))
                .collect(),
        });
        ir
    }

    #[test]
    fn a_source_attribute_difference_is_a_difference() {
        let left = with_source(&[("program_version", "1.0"), ("object_count", "3")]);
        let right = with_source(&[("program_version", "1.1"), ("object_count", "3")]);

        let result = diff(&left, &right);
        assert!(!result.is_empty());
        assert_eq!(
            result.source.attributes,
            [super::AttributeChange {
                key: "program_version".to_owned(),
                left: Some("1.0".to_owned()),
                right: Some("1.1".to_owned()),
            }]
        );
        assert!(result.source.local_digests.is_empty());
    }

    /// An attribute present on one side only is a difference in either direction.
    #[test]
    fn an_added_or_removed_source_attribute_is_a_difference() {
        let left = with_source(&[("object_count", "3")]);
        let right = with_source(&[]);

        let forward = diff(&left, &right);
        assert!(!forward.is_empty());
        assert_eq!(forward.source.attributes[0].left.as_deref(), Some("3"));
        assert_eq!(forward.source.attributes[0].right, None);

        let backward = diff(&right, &left);
        assert!(!backward.is_empty());
        assert_eq!(backward.source.attributes[0].left, None);
        assert_eq!(backward.source.attributes[0].right.as_deref(), Some("3"));
    }

    /// A machine-local digest is a bitwise fingerprint of tolerantly compared
    /// values, so two platforms' decodes of one file disagree on it while
    /// agreeing on the model. Such a difference is reported and does not make the
    /// documents different.
    #[test]
    fn a_machine_local_digest_difference_is_reported_but_is_not_a_difference() {
        let left = with_source(&[("document_local_sha256", &"0".repeat(64))]);
        let right = with_source(&[("document_local_sha256", &"1".repeat(64))]);

        let result = diff(&left, &right);
        assert_ne!(left.source, right.source);
        assert!(result.is_empty(), "{result:?}");
        assert!(result.source.attributes.is_empty());
        assert_eq!(
            result
                .source
                .local_digests
                .iter()
                .map(|change| change.key.as_str())
                .collect::<Vec<_>>(),
            ["document_local_sha256"]
        );
    }

    /// Carve-out keys by `_local_sha256` suffix, not a fixed key list.
    #[test]
    fn the_carve_out_follows_the_suffix_convention() {
        let key = format!("future_codec_thing{}", compare::LOCAL_DIGEST_SUFFIX);
        let left = with_source(&[(&key, "a"), ("footer_fingerprint", "f")]);
        let right = with_source(&[(&key, "b"), ("footer_fingerprint", "f")]);
        let result = diff(&left, &right);
        assert!(result.is_empty(), "{result:?}");
        assert_eq!(result.source.local_digests[0].key, key);

        // Digests over retained source bytes have no suffix; a change stays a difference.
        let right = with_source(&[(&key, "b"), ("footer_fingerprint", "g")]);
        let result = diff(&left, &right);
        assert!(!result.is_empty());
        assert_eq!(result.source.attributes[0].key, "footer_fingerprint");
    }

    /// A document with no source metadata compares against one that has some
    /// without panicking, in either order, and an absent source equals an empty
    /// one.
    #[test]
    fn absent_source_metadata_compares_without_panicking() {
        let mut bare = unit_cube();
        bare.source = None;
        let populated = with_source(&[("object_count", "3")]);

        for (left, right) in [(&bare, &populated), (&populated, &bare)] {
            let result = diff(left, right);
            assert!(!result.is_empty());
            assert!(result.source.format_change.is_some());
            assert_eq!(result.source.attributes.len(), 1);
        }

        let mut empty_source = unit_cube();
        empty_source.source = Some(crate::document::SourceMeta::default());
        assert!(diff(&bare, &empty_source).is_empty());
    }

    #[test]
    fn identical_documents_have_empty_diff() {
        let ir = unit_cube();
        assert!(diff(&ir, &ir).is_empty());
    }
}