mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 09:46:29 +01:00
fix(stencilcraft): validate unique document memberships
This commit is contained in:
1 parent
ca9dc38c38
commit
24886f858c
2 files changed
+30
-1
No files matched your search
@@ -532,7 +532,7 @@ impl Document {
|
||||
}
|
||||
|
||||
/// Model-level invariants: a sane board and kerf, sane images, unique object ids with
|
||||
/// `next_shape_id` above them all, memberships referencing existing shapes, and valid
|
||||
/// `next_shape_id` above them all, unique memberships referencing existing shapes, and valid
|
||||
/// operation settings on every operation.
|
||||
pub fn validate_model(&self) -> Result<(), DocError> {
|
||||
check_finite_mm(self.board.width_mm, "board width")?;
|
||||
@@ -566,10 +566,14 @@ impl Document {
|
||||
return Err(DocError::Model(format!("next_shape_id {}: not above the highest object id {max}", self.next_shape_id)));
|
||||
}
|
||||
for (i, layer) in self.layers.iter().enumerate() {
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for id in &layer.members {
|
||||
if !ids.contains(id) {
|
||||
return Err(DocError::Model(format!("layer {i} ({}): member {id} is not a shape", layer.name)));
|
||||
}
|
||||
if !seen.insert(*id) {
|
||||
return Err(DocError::Model(format!("layer {i} ({}): duplicate member {id}", layer.name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut op_ids = std::collections::BTreeSet::new();
|
||||
@@ -577,10 +581,14 @@ impl Document {
|
||||
if !op_ids.insert(op.id) {
|
||||
return Err(DocError::Model(format!("operation {}: duplicate id {}", i, op.id)));
|
||||
}
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for id in &op.members {
|
||||
if !ids.contains(id) {
|
||||
return Err(DocError::Model(format!("operation {i} ({}): member {id} is not a shape", op.name)));
|
||||
}
|
||||
if !seen.insert(*id) {
|
||||
return Err(DocError::Model(format!("operation {i} ({}): duplicate member {id}", op.name)));
|
||||
}
|
||||
}
|
||||
op.validate().map_err(|e| match e {
|
||||
DocError::Model(m) => DocError::Model(format!("operation {i} ({}): {m}", op.name)),
|
||||
|
||||
@@ -193,3 +193,24 @@ fn operation_id_allocation_avoids_a_duplicate_at_the_upper_limit() -> Result<(),
|
||||
doc.validate_model()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_memberships_fail_validation_but_save_repairs_them() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut doc = fixture()?;
|
||||
doc.layers[0].members.push(1);
|
||||
assert!(matches!(doc.validate_model(), Err(DocError::Model(message)) if message.contains("layer 0") && message.contains("duplicate member 1")));
|
||||
doc.layers[0].members.pop();
|
||||
doc.operations[0].members.push(1);
|
||||
assert!(
|
||||
matches!(doc.validate_model(), Err(DocError::Model(message)) if message.contains("operation 0") && message.contains("duplicate member 1"))
|
||||
);
|
||||
doc.layers[0].members.push(1);
|
||||
let back = Document::load_json(&doc.save_json()?)?;
|
||||
assert_eq!(back.layers[0].members, vec![1, 2]);
|
||||
assert_eq!(back.operations[0].members, vec![1, 2]);
|
||||
// Membership in two different operations is intentional, not a duplicate.
|
||||
let mut shared = back;
|
||||
shared.operations[1].members.push(1);
|
||||
shared.validate_model()?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user