cadmpeg-ir 0.6.1

CAD data model, validation, diffing, and codec API for cadmpeg.
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
// SPDX-License-Identifier: Apache-2.0
//! Neutral persisted document and view presentation state.

use cadmpeg_core::text::NonBlankString;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

crate::ids::id_type!(
    /// Stable presentation-document identity.
    PresentationId, compose
);

/// Persisted camera pose.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct CameraState {
    /// Camera position in document coordinates.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(deserialize_with = "deserialize_position")]
    pub position: Option<crate::units::FiniteVector<3>>,
    /// Persisted Inventor axis-angle orientation as X, Y, Z, angle.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(deserialize_with = "deserialize_orientation")]
    pub orientation: Option<crate::units::NonzeroVector<4>>,
    /// Other camera fields retained by exact source name.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    #[serde(deserialize_with = "cadmpeg_core::distinct_keys::btree_map")]
    pub properties: BTreeMap<NonBlankString, String>,
}

/// Closed set of document GUI state families.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(tag = "type", content = "value")]
#[serde(deny_unknown_fields)]
pub enum PresentationStateKind {
    /// Persisted camera pose.
    Camera(CameraState),
    /// Any other persisted GUI state element.
    Native(String),
}

/// Ordered non-provider GUI state such as clipping or section state.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PresentationState {
    /// Persisted state element family.
    pub kind: PresentationStateKind,
    /// Source order among document GUI state elements.
    pub order: u32,
    /// Exact root attributes.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    #[serde(deserialize_with = "cadmpeg_core::distinct_keys::btree_map")]
    pub attributes: BTreeMap<NonBlankString, String>,
    /// Referenced display assets as global native entry ids.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub assets: Vec<String>,
}

/// Document-wide persisted GUI state.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    try_from = "PresentationDocumentWire",
    into = "PresentationDocumentWire"
)]
pub struct PresentationDocument {
    /// Globally unique presentation identity.
    pub id: PresentationId,
    /// Persisted GUI schema version.
    pub schema_version: Option<u32>,
    /// Active view name or identity.
    pub active_view: Option<String>,
    /// Ordered document-level GUI states.
    states: Vec<PresentationState>,
    /// Native GUI document record supplying this state.
    pub native_ref: Option<String>,
}

#[derive(Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
struct PresentationDocumentWire {
    /// Globally unique presentation identity.
    id: PresentationId,
    /// Persisted GUI schema version.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_schema_version"
    )]
    schema_version: Option<u32>,
    /// Active view name or identity.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_active_view"
    )]
    active_view: Option<String>,
    /// Ordered document-level GUI states.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    states: Vec<PresentationState>,
    /// Native GUI document record supplying this state.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_native_ref"
    )]
    native_ref: Option<String>,
}

impl PresentationDocument {
    /// Construct document presentation with no persisted states.
    #[must_use]
    pub fn new(id: PresentationId) -> Self {
        Self {
            id,
            schema_version: None,
            active_view: None,
            states: Vec::new(),
            native_ref: None,
        }
    }

    /// Return the persisted states in source order.
    #[must_use]
    pub fn states(&self) -> &[PresentationState] {
        &self.states
    }

    /// Replace persisted states after checking that their orders are distinct.
    pub fn set_states(&mut self, states: Vec<PresentationState>) -> Result<(), String> {
        if states.windows(2).all(|pair| pair[0].order < pair[1].order) {
            self.states = states;
            return Ok(());
        }
        let mut orders = std::collections::HashSet::new();
        if states.iter().any(|state| !orders.insert(state.order)) {
            return Err("states must have distinct order values".into());
        }
        self.states = states;
        Ok(())
    }
}

impl From<PresentationDocument> for PresentationDocumentWire {
    fn from(document: PresentationDocument) -> Self {
        Self {
            id: document.id,
            schema_version: document.schema_version,
            active_view: document.active_view,
            states: document.states,
            native_ref: document.native_ref,
        }
    }
}

impl TryFrom<PresentationDocumentWire> for PresentationDocument {
    type Error = String;

    fn try_from(wire: PresentationDocumentWire) -> Result<Self, Self::Error> {
        let mut document = Self {
            schema_version: wire.schema_version,
            active_view: wire.active_view,
            native_ref: wire.native_ref,
            ..Self::new(wire.id)
        };
        document.set_states(wire.states)?;
        Ok(document)
    }
}

#[cfg(feature = "schema")]
impl JsonSchema for PresentationDocument {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "PresentationDocument".into()
    }

    fn schema_id() -> std::borrow::Cow<'static, str> {
        concat!(module_path!(), "::PresentationDocument").into()
    }

    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        PresentationDocumentWire::json_schema(generator)
    }
}

/// Presentation state owned by one persisted view provider.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct ViewPresentation {
    /// Globally unique view-provider identity.
    pub id: PresentationId,
    /// Owning application object identity, if resolved.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_object"
    )]
    pub object: Option<String>,
    /// Source order in the provider table.
    pub order: u32,
    /// Persisted tree expansion state.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_expanded"
    )]
    pub expanded: Option<bool>,
    /// Persisted object visibility.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_visible"
    )]
    pub visible: Option<bool>,
    /// Display mode name or numeric code.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_display_mode"
    )]
    pub display_mode: Option<String>,
    /// Selection rendering mode.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_selection_style"
    )]
    pub selection_style: Option<String>,
    /// Line width in persisted display units.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(deserialize_with = "deserialize_line_width")]
    pub line_width: Option<crate::scalar::NonNegativeReal>,
    /// Point size in persisted display units.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(deserialize_with = "deserialize_point_size")]
    pub point_size: Option<crate::scalar::NonNegativeReal>,
    /// Remaining view properties by exact source property name.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    #[serde(deserialize_with = "cadmpeg_core::distinct_keys::btree_map")]
    pub properties: BTreeMap<NonBlankString, String>,
    /// Native view-provider record supplying this state.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_native_ref"
    )]
    pub native_ref: Option<String>,
}

// Named presentation layers and their model-item membership.

use crate::ids::{
    BodyId, CurveId, EdgeId, FaceId, LayerId, OccurrenceId, PmiId, PointId, ProductDefinitionId,
    SurfaceId, VertexId,
};

/// A model or presentation object assigned to a layer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(tag = "kind", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum PresentationItem {
    /// Shape body.
    Body {
        /// Assigned body.
        body: BodyId,
    },
    /// Topological face.
    Face {
        /// Assigned face.
        face: FaceId,
    },
    /// Topological edge.
    Edge {
        /// Assigned edge.
        edge: EdgeId,
    },
    /// Topological vertex.
    Vertex {
        /// Assigned vertex.
        vertex: VertexId,
    },
    /// Point carrier.
    Point {
        /// Assigned point.
        point: PointId,
    },
    /// Curve carrier.
    Curve {
        /// Assigned curve.
        curve: CurveId,
    },
    /// Surface carrier.
    Surface {
        /// Assigned surface.
        surface: SurfaceId,
    },
    /// Product prototype.
    Product {
        /// Assigned product.
        product: ProductDefinitionId,
    },
    /// Product occurrence.
    Occurrence {
        /// Assigned occurrence.
        occurrence: OccurrenceId,
    },
    /// PMI annotation.
    Pmi {
        /// Assigned PMI annotation.
        annotation: PmiId,
    },
    /// Tessellation identity.
    Tessellation {
        /// Assigned tessellation identity.
        tessellation: String,
    },
    /// Source item whose neutral target type is not modeled.
    Source {
        /// Stable source item identity.
        #[serde(deserialize_with = "deserialize_source_id")]
        source_id: cadmpeg_core::text::NonBlankString,
    },
}

/// One presentation layer.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PresentationLayer {
    /// Stable layer identity.
    pub id: LayerId,
    /// Layer name.
    pub name: String,
    /// Optional layer description.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_description"
    )]
    pub description: Option<String>,
    /// Explicit layer visibility; `false` means the layer is hidden.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        deserialize_with = "deserialize_visible"
    )]
    pub visible: Option<bool>,
    /// Assigned items in deterministic projection order; order has no semantic meaning.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub items: Vec<PresentationItem>,
}

cadmpeg_core::named_optional_field!(
    deserialize_position,
    crate::units::FiniteVector<3>,
    "position"
);

cadmpeg_core::named_optional_field!(
    deserialize_orientation,
    crate::units::NonzeroVector<4>,
    "orientation"
);

cadmpeg_core::named_optional_field!(
    deserialize_line_width,
    crate::scalar::NonNegativeReal,
    "line_width"
);

cadmpeg_core::named_optional_field!(
    deserialize_point_size,
    crate::scalar::NonNegativeReal,
    "point_size"
);

crate::units::named_field!(
    deserialize_source_id,
    cadmpeg_core::text::NonBlankString,
    "source_id"
);

#[cfg(test)]
mod tests {
    #[test]
    fn a_named_optional_key_states_a_value_or_is_left_out() {
        use super::ViewPresentation;

        let refusal = serde_json::from_value::<ViewPresentation>(serde_json::json!({
            "id": "freecad:presentation:view#1",
            "order": 0,
            "line_width": null
        }))
        .expect_err("null is not a spelling of an absent line width");
        let text = refusal.to_string();
        assert!(
            text.contains("line_width")
                && text.contains("this key states a value or is left out; it does not state null"),
            "{text}"
        );
        let absent = serde_json::from_value::<ViewPresentation>(serde_json::json!({
            "id": "freecad:presentation:view#1",
            "order": 0
        }))
        .expect("an absent key reaches the field default");
        assert!(absent.line_width.is_none());
    }

    #[test]
    fn camera_kinds_and_states_round_trip_without_payload_or_tag_loss() {
        let kinds = [
            PresentationStateKind::Camera(CameraState {
                position: Some(
                    crate::units::FiniteVector::new([1.0, 2.0, 3.0]).expect("finite position"),
                ),
                orientation: None,
                properties: BTreeMap::new(),
            }),
            PresentationStateKind::Camera(CameraState {
                position: Some(
                    crate::units::FiniteVector::new([4.0, 5.0, 6.0]).expect("finite position"),
                ),
                orientation: None,
                properties: BTreeMap::new(),
            }),
            PresentationStateKind::Native("Camera".to_owned()),
        ];
        let mut states = Vec::new();
        for (order, kind) in kinds.into_iter().enumerate() {
            let json = serde_json::to_string(&kind).unwrap();
            assert_eq!(
                serde_json::from_str::<PresentationStateKind>(&json).unwrap(),
                kind
            );
            let state = PresentationState {
                kind,
                order: u32::try_from(order).expect("test order fits u32"),
                attributes: BTreeMap::new(),
                assets: Vec::new(),
            };
            let json = serde_json::to_string(&state).unwrap();
            assert_eq!(
                serde_json::from_str::<PresentationState>(&json).unwrap(),
                state
            );
            states.push(state);
        }
        let mut document = PresentationDocument::new(
            PresentationId::mint("synthetic:test:presentation#presentation").unwrap(),
        );
        document.set_states(states).expect("distinct orders");
        let json = serde_json::to_string(&document).unwrap();
        assert_eq!(
            serde_json::from_str::<PresentationDocument>(&json).unwrap(),
            document
        );
    }

    use std::collections::BTreeMap;

    use super::{
        CameraState, PresentationDocument, PresentationId, PresentationItem, PresentationLayer,
        PresentationState, PresentationStateKind,
    };
    use crate::document::CadIr;
    use crate::ids::{FaceId, LayerId};
    use crate::report::check::Check;
    use crate::validate::validate_neutral;

    #[test]
    fn source_layer_items_validate_without_fabricated_geometry() {
        let mut ir = CadIr::empty();
        ir.model.presentation_layers.push(PresentationLayer {
            id: LayerId::mint("test:presentation:layer#construction").expect("valid identity"),
            name: "construction".into(),
            description: None,
            visible: None,
            items: vec![PresentationItem::Source {
                source_id: cadmpeg_core::nonblank_literal!("#{}", 42),
            }],
        });

        assert!(validate_neutral(&ir, Vec::new())
            .expect("resource allocation did not fail")
            .is_ok());
    }

    #[test]
    fn empty_layer_name_is_valid() {
        let mut ir = CadIr::empty();
        ir.model.presentation_layers.push(PresentationLayer {
            id: LayerId::mint("test:presentation:layer#unnamed").expect("valid identity"),
            name: String::new(),
            description: None,
            visible: None,
            items: vec![PresentationItem::Source {
                source_id: cadmpeg_core::nonblank_literal!("#{}", 42),
            }],
        });

        assert!(validate_neutral(&ir, Vec::new())
            .expect("resource allocation did not fail")
            .is_ok());
    }

    #[test]
    fn missing_typed_layer_item_is_invalid() {
        let mut ir = CadIr::empty();
        ir.model.presentation_layers.push(PresentationLayer {
            id: LayerId::mint("test:presentation:layer#missing").expect("valid identity"),
            name: "missing".into(),
            description: None,
            visible: None,
            items: vec![PresentationItem::Face {
                face: FaceId::mint("test:model:face#missing").expect("valid identity"),
            }],
        });

        assert!(validate_neutral(&ir, Vec::new())
            .expect("resource allocation did not fail")
            .findings
            .iter()
            .any(|finding| finding.check == Check::Presentation));
    }

    #[test]
    fn source_identity_admission_preserves_nonempty_wire() {
        let wire = serde_json::json!({"kind": "source", "source_id": "#42"});
        let value: PresentationItem =
            serde_json::from_value(wire.clone()).expect("nonempty source_id");
        assert_eq!(serde_json::to_value(value).expect("serialize"), wire);
        let error = serde_json::from_value::<PresentationItem>(
            serde_json::json!({"kind": "source", "source_id": ""}),
        )
        .expect_err("empty source_id");
        assert!(error.to_string().contains("source_id"));
        assert!(cadmpeg_core::text::NonBlankString::new("").is_none());
    }

    #[test]
    fn document_state_orders_are_checked_without_reordering_or_partial_updates() {
        let state = |order| PresentationState {
            kind: PresentationStateKind::Native("View".into()),
            order,
            attributes: BTreeMap::new(),
            assets: Vec::new(),
        };
        let mut document = PresentationDocument::new(
            PresentationId::mint("synthetic:test:presentation#presentation")
                .expect("valid identity"),
        );
        assert!(document.states().is_empty());
        let states = vec![state(9), state(2)];
        document
            .set_states(states.clone())
            .expect("distinct orders");
        assert_eq!(document.states(), states);
        let wire = serde_json::to_value(&document).expect("serialize");
        assert_eq!(
            serde_json::from_value::<PresentationDocument>(wire.clone()).expect("valid wire"),
            document
        );
        assert!(document.set_states(vec![state(2), state(2)]).is_err());
        assert_eq!(document.states(), states);
        let mut invalid = wire;
        invalid["states"] =
            serde_json::to_value(vec![state(2), state(2)]).expect("serialize states");
        let error = serde_json::from_value::<PresentationDocument>(invalid)
            .expect_err("duplicate state order");
        assert!(error.to_string().contains("states"));
        document.set_states(Vec::new()).expect("empty states");
        assert!(document.states().is_empty());
    }
}

// Each optional key below names itself in whatever it refuses.
cadmpeg_core::named_optional_field!(deserialize_schema_version, u32, "schema_version");
cadmpeg_core::named_optional_field!(deserialize_active_view, String, "active_view");
cadmpeg_core::named_optional_field!(deserialize_native_ref, String, "native_ref");
cadmpeg_core::named_optional_field!(deserialize_object, String, "object");
cadmpeg_core::named_optional_field!(deserialize_expanded, bool, "expanded");
cadmpeg_core::named_optional_field!(deserialize_visible, bool, "visible");
cadmpeg_core::named_optional_field!(deserialize_display_mode, String, "display_mode");
cadmpeg_core::named_optional_field!(deserialize_selection_style, String, "selection_style");
cadmpeg_core::named_optional_field!(deserialize_description, String, "description");

mod identity_rewrite;