ferrocad_core 0.1.0

FerroCAD core: quantities, properties, documents, recompute DAG
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
//! The document object model: objects, a dependency graph, observers, and
//! expression-driven recompute.
//!
//! Transactional editing (undo/redo, the reversible change set, FreeCAD's
//! transaction metadata) lives in the [`transaction`] submodule, which adds a
//! second `impl Document`; this module owns the object store the engine mutates.

use std::collections::{BTreeMap, BTreeSet};

use petgraph::graph::NodeIndex;
use petgraph::stable_graph::StableGraph;
use serde::{Deserialize, Serialize};

use crate::expr;
use crate::observer::Observer;
use crate::property::{Property, PropertyContainer};

mod transaction;

use transaction::{Change, PropertyChange, TransactionManager};

pub type ObjectId = usize;

#[derive(Debug)]
pub struct DocumentObject {
    pub id: ObjectId,
    pub name: String,
    /// Display name; defaults to `name` and is mutable.
    pub label: String,
    pub type_id: String,
    pub properties: PropertyContainer,
    /// Property name → expression source (evaluated on recompute).
    pub expressions: BTreeMap<String, String>,
    /// Dynamic extension type ids added via `addExtension`.
    pub extensions: BTreeSet<String>,
    /// Set by `enforceRecompute`; cleared when the object recomputes.
    pub must_execute: bool,
    /// Set when the object's last recompute failed (reported as `Invalid`).
    pub invalid: bool,
    /// Opaque base64-pickled Python state (instance `__dict__` + `Proxy`).
    pub python_state: Option<String>,
}

#[derive(Default)]
pub struct Document {
    objects: BTreeMap<ObjectId, DocumentObject>,
    next_id: ObjectId,
    /// Edges point dependency → dependant (dependency recomputes first).
    graph: StableGraph<ObjectId, ()>,
    index: BTreeMap<ObjectId, NodeIndex>,
    tx: TransactionManager,
    observers: Vec<Box<dyn Observer>>,
    /// The object made active by the last `addObject` (FreeCAD `ActiveObject`).
    active_object: Option<ObjectId>,
}

impl Document {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add_object(&mut self, name: &str, type_id: &str) -> ObjectId {
        self.add_object_with(name, type_id, false)
    }

    /// Add an object, applying FreeCAD's name/label rules.
    ///
    /// The internal `name` is always sanitized and made unique. The `label` is the
    /// requested name when `duplicate_labels` is set, otherwise a unique label.
    pub fn add_object_with(
        &mut self,
        name: &str,
        type_id: &str,
        duplicate_labels: bool,
    ) -> ObjectId {
        let id = self.next_id;
        self.next_id += 1;

        let (name, label) = if name.is_empty() {
            // FreeCAD-style default naming: last segment of the type id + counter.
            let base = type_id.rsplit("::").next().unwrap_or("Object");
            let base = if base.is_empty() { "Object" } else { base };
            let unique = self.unique_name(base);
            (unique.clone(), unique)
        } else {
            let base = sanitize_name(name);
            let unique = self.unique_name(&base);
            let label = if duplicate_labels {
                base
            } else {
                self.unique_label(&base)
            };
            (unique, label)
        };

        let node = self.graph.add_node(id);
        self.index.insert(id, node);

        // Initialize default properties for known types (FeatureTest, …).
        let mut properties = PropertyContainer::new();
        for (prop_name, prop) in crate::typeregistry::default_properties(type_id) {
            properties.set(prop_name.to_string(), prop);
        }

        self.objects.insert(
            id,
            DocumentObject {
                id,
                label,
                name,
                type_id: type_id.to_string(),
                properties,
                expressions: BTreeMap::new(),
                extensions: BTreeSet::new(),
                must_execute: false,
                invalid: false,
                python_state: None,
            },
        );
        self.active_object = Some(id);
        {
            let saved = SavedObject::from_object(&self.objects[&id]);
            self.record_change(Change::AddObject { id, saved });
        }
        for observer in &mut self.observers {
            observer.on_object_added(id);
        }
        id
    }

    /// Make `base` a unique object *name* (base, base001, base002, …).
    pub fn unique_name(&self, base: &str) -> String {
        if self.get_by_name(base).is_none() {
            return base.to_string();
        }
        let stem = strip_trailing_digits(base);
        let mut i = 1;
        loop {
            let candidate = format!("{stem}{i:03}");
            if self.get_by_name(&candidate).is_none() {
                return candidate;
            }
            i += 1;
        }
    }

    /// Make `base` a unique object *label* (base, base001, base002, …).
    pub fn unique_label(&self, base: &str) -> String {
        if !self.objects.values().any(|o| o.label == base) {
            return base.to_string();
        }
        let stem = strip_trailing_digits(base);
        let mut i = 1;
        loop {
            let candidate = format!("{stem}{i:03}");
            if !self.objects.values().any(|o| o.label == candidate) {
                return candidate;
            }
            i += 1;
        }
    }

    pub fn object(&self, id: ObjectId) -> Option<&DocumentObject> {
        self.objects.get(&id)
    }

    pub fn object_mut(&mut self, id: ObjectId) -> Option<&mut DocumentObject> {
        self.objects.get_mut(&id)
    }

    pub fn object_ids(&self) -> Vec<ObjectId> {
        self.objects.keys().copied().collect()
    }

    pub fn get_by_name(&self, name: &str) -> Option<ObjectId> {
        self.objects.values().find(|o| o.name == name).map(|o| o.id)
    }

    /// Remove an object (and its graph node/edges), recording the change so it
    /// can be undone. Returns false if the object is absent.
    pub fn remove_object(&mut self, id: ObjectId) -> bool {
        let saved = match self.objects.get(&id) {
            Some(o) => SavedObject::from_object(o),
            None => return false,
        };
        let name = saved.name.clone();
        // Group link lists that reference the object, before it is stripped.
        let group_before: Vec<(ObjectId, Vec<String>)> = self
            .objects
            .values()
            .filter(|o| o.id != id)
            .filter_map(|o| match o.properties.get("Group") {
                Some(Property::LinkList(links)) if links.contains(&name) => {
                    Some((o.id, links.clone()))
                }
                _ => None,
            })
            .collect();

        self.record_change(Change::RemoveObject { id, saved });
        if !self.remove_object_raw(id, true) {
            return false;
        }
        // Record the link-list edits so undo restores group membership.
        for (group, old) in group_before {
            let new = match self.objects.get(&group).and_then(|o| o.properties.get("Group")) {
                Some(Property::LinkList(links)) => links.clone(),
                _ => continue,
            };
            self.record_change(Change::Property(PropertyChange {
                object: group,
                name: "Group".to_string(),
                old: Some(Property::LinkList(old)),
                new: Property::LinkList(new),
            }));
        }
        true
    }

    /// Remove an object without recording anything. `strip_groups` also drops it
    /// from every group's `Group` link list (used by the public, recording path).
    fn remove_object_raw(&mut self, id: ObjectId, strip_groups: bool) -> bool {
        let name = match self.objects.get(&id) {
            Some(o) => o.name.clone(),
            None => return false,
        };
        if self.objects.remove(&id).is_none() {
            return false;
        }
        if let Some(node) = self.index.remove(&id) {
            self.graph.remove_node(node);
        }
        if strip_groups {
            for other in self.objects.values_mut() {
                if let Some(Property::LinkList(links)) = other.properties.get_mut("Group") {
                    links.retain(|n| n != &name);
                }
            }
        }
        if self.active_object == Some(id) {
            self.active_object = None;
        }
        true
    }

    pub fn set_label(&mut self, id: ObjectId, label: &str) -> bool {
        match self.objects.get_mut(&id) {
            Some(obj) => {
                obj.label = label.to_string();
                true
            }
            None => false,
        }
    }

    pub fn remove_property(&mut self, object: ObjectId, name: &str) -> bool {
        match self.objects.get_mut(&object) {
            Some(obj) => obj.properties.remove(name).is_some(),
            None => false,
        }
    }

    // -- extensions ---------------------------------------------------------

    /// Record a dynamic extension on an object.
    pub fn add_extension(&mut self, id: ObjectId, ext: &str) -> bool {
        match self.objects.get_mut(&id) {
            Some(obj) => obj.extensions.insert(ext.to_string()),
            None => false,
        }
    }

    /// Whether an object has `ext`, considering extension inheritance (e.g.
    /// `App::GroupExtensionPython` derives from `App::GroupExtension`).
    pub fn has_extension(&self, id: ObjectId, ext: &str) -> bool {
        self.object(id)
            .map(|o| o.extensions.iter().any(|e| extension_is_or_derives(e, ext)))
            .unwrap_or(false)
    }

    pub fn remove_extension(&mut self, id: ObjectId, ext: &str) -> bool {
        match self.objects.get_mut(&id) {
            Some(obj) => obj.extensions.remove(ext),
            None => false,
        }
    }

    /// Whether the object acts as a group (document group, part, or has a
    /// group extension) and therefore carries a `Group` link list.
    pub fn is_group_like(&self, id: ObjectId) -> bool {
        self.object(id)
            .map(|o| {
                o.type_id == "App::DocumentObjectGroup"
                    || o.type_id == "App::Part"
                    || o.extensions
                        .iter()
                        .any(|e| extension_is_or_derives(e, "App::GroupExtension"))
            })
            .unwrap_or(false)
    }

    /// Remove `member` from any group that lists it (single-group enforcement).
    pub fn unlink_from_groups(&mut self, member: ObjectId) {
        let name = match self.object(member) {
            Some(o) => o.name.clone(),
            None => return,
        };
        for other in self.objects.values_mut() {
            if let Some(Property::LinkList(links)) = other.properties.get_mut("Group") {
                links.retain(|n| n != &name);
            }
        }
    }

    /// Declare that `object` depends on `depends_on` (the latter recomputes first).
    pub fn add_dependency(&mut self, object: ObjectId, depends_on: ObjectId) {
        if let (Some(&a), Some(&b)) = (self.index.get(&depends_on), self.index.get(&object)) {
            self.graph.add_edge(a, b, ());
        }
    }

    /// Object ids in dependency-first order. `Err` if the graph has a cycle.
    pub fn recompute_order(&self) -> Result<Vec<ObjectId>, String> {
        let nodes = petgraph::algo::toposort(&self.graph, None)
            .map_err(|_| "dependency cycle".to_string())?;
        Ok(nodes.into_iter().map(|n| self.graph[n]).collect())
    }

    // -- properties (transactional + observable) ----------------------------

    pub fn set_property(&mut self, object: ObjectId, name: &str, value: Property) -> Result<(), String> {
        let old = {
            let obj = self
                .objects
                .get_mut(&object)
                .ok_or_else(|| format!("no object {object}"))?;
            obj.properties.get(name).cloned()
        };
        let status = self
            .objects
            .get(&object)
            .and_then(|o| o.properties.status(name))
            .unwrap_or(crate::prop_status::NONE);
        {
            let obj = self.objects.get_mut(&object).unwrap();
            obj.properties.set(name.to_string(), value.clone());
            // Assigning a property touches the object, unless it is an output /
            // no-recompute property (FreeCAD `Property::touch`).
            if status & crate::prop_status::NO_TOUCH == 0 {
                obj.must_execute = true;
            }
        }

        self.record_change(Change::Property(PropertyChange {
            object,
            name: name.to_string(),
            old: old.clone(),
            new: value.clone(),
        }));
        for observer in &mut self.observers {
            observer.on_property_changed(object, name, old.as_ref(), &value);
        }
        Ok(())
    }

    /// The object made active by the most recent `addObject` (or `None`).
    pub fn active_object(&self) -> Option<ObjectId> {
        self.active_object
    }

    /// Add a property with an explicit status mask. Unlike `set_property`, this
    /// does not touch the object (FreeCAD `addProperty`).
    pub fn add_property(
        &mut self,
        object: ObjectId,
        name: &str,
        value: Property,
        status: u32,
    ) -> Result<(), String> {
        let obj = self
            .objects
            .get_mut(&object)
            .ok_or_else(|| format!("no object {object}"))?;
        obj.properties.set_with_status(name.to_string(), value, status);
        Ok(())
    }

    /// The status mask of a property (`getPropertyStatus`), if it exists.
    pub fn property_status(&self, object: ObjectId, name: &str) -> Option<u32> {
        self.objects.get(&object).and_then(|o| o.properties.status(name))
    }

    /// Replace a property's status mask (`setPropertyStatus`).
    pub fn set_property_status(&mut self, object: ObjectId, name: &str, status: u32) -> bool {
        match self.objects.get_mut(&object) {
            Some(obj) => obj.properties.set_status(name, status).is_some(),
            None => false,
        }
    }

    /// Mark an object as unchanged (FreeCAD `purgeTouched`).
    pub fn purge_touched(&mut self, object: ObjectId) {
        if let Some(obj) = self.objects.get_mut(&object) {
            obj.must_execute = false;
        }
    }

    // -- recompute flags -----------------------------------------------------

    pub fn enforce_recompute(&mut self, id: ObjectId) {
        if let Some(obj) = self.objects.get_mut(&id) {
            obj.must_execute = true;
        }
    }

    /// Take (and clear) the `must_execute` flag for one object.
    pub fn take_must_execute_one(&mut self, id: ObjectId) -> bool {
        match self.objects.get_mut(&id) {
            Some(obj) => std::mem::replace(&mut obj.must_execute, false),
            None => false,
        }
    }

    /// Take (and clear) all `must_execute` flags, in object order.
    pub fn take_must_execute(&mut self) -> Vec<ObjectId> {
        let flagged: Vec<ObjectId> = self
            .objects
            .values()
            .filter(|o| o.must_execute)
            .map(|o| o.id)
            .collect();
        for id in &flagged {
            if let Some(obj) = self.objects.get_mut(id) {
                obj.must_execute = false;
            }
        }
        flagged
    }

    // -- expressions --------------------------------------------------------

    pub fn set_expression(&mut self, object: ObjectId, prop: &str, source: &str) -> Result<(), String> {
        let parsed = expr::parse(source)?; // validate early
        let target = normalize_path(prop);
        let mut new_deps = BTreeSet::new();
        collect_self_deps(&parsed, &mut new_deps);

        // Build this object's self-dependency graph (existing + new) and reject cycles.
        let mut graph: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
        {
            let obj = self
                .objects
                .get(&object)
                .ok_or_else(|| format!("no object {object}"))?;
            for (p, src) in &obj.expressions {
                let mut deps = BTreeSet::new();
                if let Ok(e) = expr::parse(src) {
                    collect_self_deps(&e, &mut deps);
                }
                graph.insert(normalize_path(p), deps);
            }
        }
        graph.insert(target.clone(), new_deps);
        if graph_reaches(&graph, &target, &target) {
            return Err(format!("cyclic dependency detected for '{prop}'"));
        }

        let old_source = self
            .objects
            .get(&object)
            .and_then(|o| o.expressions.get(prop).cloned());
        let obj = self.objects.get_mut(&object).unwrap();
        obj.expressions.insert(prop.to_string(), source.to_string());
        self.record_change(Change::AddExpression {
            object,
            prop: prop.to_string(),
            old: old_source,
            new: source.to_string(),
        });
        Ok(())
    }

    pub fn remove_expression(&mut self, object: ObjectId, prop: &str) -> bool {
        let old = match self.objects.get_mut(&object) {
            Some(obj) => obj.expressions.remove(prop),
            None => return false,
        };
        if let Some(old) = &old {
            self.record_change(Change::RemoveExpression {
                object,
                prop: prop.to_string(),
                old: old.clone(),
            });
        }
        old.is_some()
    }

    /// The object's expressions as `(property, source)` pairs.
    pub fn expressions(&self, object: ObjectId) -> Vec<(String, String)> {
        self.object(object)
            .map(|o| o.expressions.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
            .unwrap_or_default()
    }

    /// Evaluate an expression source in the context of `object`.
    pub fn eval_expression(&self, object: ObjectId, source: &str) -> Result<f64, String> {
        let parsed = expr::parse(source)?;
        parsed.eval(&|name| self.resolve(&object, name))
    }

    /// Evaluate every expression (in dependency order) and write the results.
    /// Returns the number of expressions evaluated.
    pub fn recompute(&mut self) -> Result<usize, String> {
        let order = self.recompute_order()?;
        let mut count = 0;
        for id in order {
            let expressions: Vec<(String, String)> = {
                let obj = &self.objects[&id];
                obj.expressions
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect()
            };
            for (prop, source) in expressions {
                let parsed = match expr::parse(&source) {
                    Ok(p) => p,
                    Err(e) => {
                        self.mark_invalid(id);
                        return Err(e);
                    }
                };
                let value = match parsed.eval(&|name| self.resolve(&id, name)) {
                    Ok(v) => v,
                    Err(e) => {
                        self.mark_invalid(id);
                        return Err(e);
                    }
                };
                if let Some(obj) = self.objects.get_mut(&id) {
                    obj.properties.set(prop.clone(), Property::Float(value));
                    obj.invalid = false;
                }
                count += 1;
            }
        }
        Ok(count)
    }

    fn mark_invalid(&mut self, id: ObjectId) {
        if let Some(obj) = self.objects.get_mut(&id) {
            obj.invalid = true;
        }
    }

    fn resolve(&self, current: &ObjectId, name: &str) -> Option<f64> {
        if let Some((object_name, prop_name)) = name.split_once('.') {
            let obj = self.objects.values().find(|o| o.name == object_name)?;
            return numeric(obj, prop_name);
        }
        let obj = &self.objects[current];
        numeric(obj, name).or_else(|| self.objects.values().find_map(|o| numeric(o, name)))
    }

    // -- observers ----------------------------------------------------------

    pub fn add_observer(&mut self, observer: Box<dyn Observer>) {
        self.observers.push(observer);
    }

    // -- persistence --------------------------------------------------------

    /// Serialize this document's objects (with `name`) to JSON.
    pub fn to_saved(&self, name: &str) -> SavedDocument {
        let objects = self
            .object_ids()
            .into_iter()
            .filter_map(|id| self.object(id).map(SavedObject::from_object))
            .collect();
        SavedDocument { name: name.to_string(), objects }
    }

    /// Build a fresh document from a `SavedDocument`.
    pub fn from_saved(saved: &SavedDocument) -> Document {
        let mut doc = Document::new();
        for obj in &saved.objects {
            let id = doc.add_object(&obj.name, &obj.type_id);
            doc.set_label(id, &obj.label);
            if let Some(o) = doc.objects.get_mut(&id) {
                o.properties.clear();
                for (k, v) in &obj.properties {
                    let status = obj.property_status.get(k).copied().unwrap_or(crate::prop_status::NONE);
                    o.properties.set_with_status(k.clone(), v.clone(), status);
                }
                o.expressions = obj.expressions.clone();
                o.extensions = obj.extensions.iter().cloned().collect();
                o.python_state = obj.python_state.clone();
                o.must_execute = false;
                o.invalid = false;
            }
        }
        doc
    }

    pub fn save_to_file(&self, name: &str, path: &str) -> Result<(), String> {
        let json = serde_json::to_string(&self.to_saved(name)).map_err(|e| e.to_string())?;
        std::fs::write(path, json).map_err(|e| e.to_string())
    }

    pub fn load_from_file(path: &str) -> Result<SavedDocument, String> {
        let text = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
        serde_json::from_str(&text).map_err(|e| e.to_string())
    }

    /// Serialize the whole document to bytes (`dumpContent`).
    pub fn dump(&self, name: &str) -> Result<Vec<u8>, String> {
        serde_json::to_vec(&self.to_saved(name)).map_err(|e| e.to_string())
    }

    /// Replace this document's content from a `dump` payload (`restoreContent`).
    pub fn restore_from_bytes(&mut self, data: &[u8]) -> Result<(), String> {
        let saved: SavedDocument = serde_json::from_slice(data).map_err(|e| e.to_string())?;
        *self = Document::from_saved(&saved);
        Ok(())
    }

    fn saved_object(&self, id: ObjectId) -> Result<SavedObject, String> {
        let o = self.object(id).ok_or_else(|| format!("no object {id}"))?;
        Ok(SavedObject::from_object(o))
    }

    /// Serialize one object to bytes (`dumpContent`).
    pub fn dump_object(&self, id: ObjectId) -> Result<Vec<u8>, String> {
        serde_json::to_vec(&self.saved_object(id)?).map_err(|e| e.to_string())
    }

    /// Restore one object's content from bytes (`restoreContent`).
    pub fn restore_object(&mut self, id: ObjectId, data: &[u8]) -> Result<(), String> {
        let saved: SavedObject = serde_json::from_slice(data).map_err(|e| e.to_string())?;
        let o = self.objects.get_mut(&id).ok_or_else(|| format!("no object {id}"))?;
        o.label = saved.label;
        o.properties.clear();
        for (k, v) in saved.properties {
            let status = saved.property_status.get(&k).copied().unwrap_or(crate::prop_status::NONE);
            o.properties.set_with_status(k, v, status);
        }
        o.expressions = saved.expressions;
        o.extensions = saved.extensions.into_iter().collect();
        o.python_state = saved.python_state;
        Ok(())
    }

    /// Serialize a single property to bytes (`dumpPropertyContent`).
    pub fn dump_property(&self, id: ObjectId, name: &str) -> Result<Vec<u8>, String> {
        let p = self
            .object(id)
            .and_then(|o| o.properties.get(name))
            .ok_or_else(|| format!("no property '{name}'"))?;
        serde_json::to_vec(p).map_err(|e| e.to_string())
    }

    /// Restore a single property from bytes (`restorePropertyContent`).
    pub fn restore_property(&mut self, id: ObjectId, name: &str, data: &[u8]) -> Result<(), String> {
        let p: Property = serde_json::from_slice(data).map_err(|e| e.to_string())?;
        self.set_property(id, name, p)
    }
}

/// A document as persisted to disk (JSON).
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SavedDocument {
    pub name: String,
    pub objects: Vec<SavedObject>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SavedObject {
    pub name: String,
    pub label: String,
    pub type_id: String,
    pub properties: BTreeMap<String, Property>,
    /// Property name → `PropertyType` status mask.
    #[serde(default)]
    pub property_status: BTreeMap<String, u32>,
    pub expressions: BTreeMap<String, String>,
    pub extensions: BTreeSet<String>,
    #[serde(default)]
    pub python_state: Option<String>,
}

impl SavedObject {
    /// Build the persisted form of an object, dropping transient / no-persist
    /// properties (`Prop_Transient`, `Prop_NoPersist`).
    pub(crate) fn from_object(o: &DocumentObject) -> SavedObject {
        let mut properties = BTreeMap::new();
        let mut property_status = BTreeMap::new();
        for (k, v) in o.properties.iter() {
            let status = o.properties.status(k).unwrap_or(crate::prop_status::NONE);
            if status & crate::prop_status::NOT_PERSISTED != 0 {
                continue;
            }
            properties.insert(k.clone(), v.clone());
            property_status.insert(k.clone(), status);
        }
        SavedObject {
            name: o.name.clone(),
            label: o.label.clone(),
            type_id: o.type_id.clone(),
            properties,
            property_status,
            expressions: o.expressions.clone(),
            extensions: o.extensions.iter().cloned().collect(),
            python_state: o.python_state.clone(),
        }
    }
}

/// Extension inheritance: true if `ext` is `base` or derives from it.
///
/// Covers the Python-extension → C++-extension pairs relevant to the POC
/// (the `*Python` extension derives from its C++ counterpart).
fn extension_is_or_derives(ext: &str, base: &str) -> bool {
    if ext == base {
        return true;
    }
    matches!(
        (ext, base),
        ("App::GroupExtensionPython", "App::GroupExtension")
            | ("Gui::ViewProviderGroupExtensionPython", "Gui::ViewProviderGroupExtension")
    )
}

/// Strip a leading '.' from a self-relative expression path.
/// FreeCAD `Base::Tools::getIdentifier`: keep identifier characters, replace every
/// other code point with `_`, and prepend `_` if the first character is not a
/// valid identifier start (e.g. a digit).
pub fn sanitize_name(name: &str) -> String {
    if name.is_empty() {
        return "_".to_string();
    }
    let is_subsequent = |c: char| c == '_' || c.is_alphanumeric();
    let is_first = |c: char| c == '_' || c.is_alphabetic();
    let mut out = String::with_capacity(name.len() + 1);
    let mut chars = name.chars();
    if let Some(first) = chars.next() {
        let first_ok = is_first(first);
        if !first_ok {
            out.push('_');
        }
        if first_ok || is_subsequent(first) {
            out.push(first);
        }
    }
    for c in chars {
        if is_subsequent(c) {
            out.push(c);
        } else {
            out.push('_');
        }
    }
    out
}

/// Strip a trailing run of ASCII digits so `Label001` and `Label` share a stem;
/// an all-digit name is returned unchanged.
fn strip_trailing_digits(name: &str) -> &str {
    let trimmed = name.trim_end_matches(|c: char| c.is_ascii_digit());
    if trimmed.is_empty() {
        name
    } else {
        trimmed
    }
}

fn normalize_path(path: &str) -> String {
    path.trim_start_matches('.').to_string()
}

/// Collect the self-relative paths an expression depends on (leading '.', or a
/// bare name without a `.`).
fn collect_self_deps(e: &expr::Expr, out: &mut BTreeSet<String>) {
    match e {
        expr::Expr::Number(_) => {}
        expr::Expr::Var(name) => {
            if name.starts_with('.') || !name.contains('.') {
                out.insert(normalize_path(name));
            }
        }
        expr::Expr::UnaryNeg(x) => collect_self_deps(x, out),
        expr::Expr::Binary(l, _, r) => {
            collect_self_deps(l, out);
            collect_self_deps(r, out);
        }
    }
}

/// Whether `goal` is reachable from `start` in the property dependency graph.
fn graph_reaches(
    graph: &BTreeMap<String, BTreeSet<String>>,
    start: &str,
    goal: &str,
) -> bool {
    let mut stack: Vec<String> = graph.get(start).cloned().unwrap_or_default().into_iter().collect();
    let mut seen = BTreeSet::new();
    while let Some(node) = stack.pop() {
        if node == goal {
            return true;
        }
        if !seen.insert(node.clone()) {
            continue;
        }
        if let Some(deps) = graph.get(&node) {
            stack.extend(deps.iter().cloned());
        }
    }
    false
}

fn numeric(obj: &DocumentObject, name: &str) -> Option<f64> {
    match obj.properties.get(name)? {
        Property::Float(f) => Some(*f),
        Property::Quantity(q) => Some(q.value_mm()),
        _ => None,
    }
}