diff --git a/crates/doc/src/persistent_name.rs b/crates/doc/src/persistent_name.rs index dec333ee2..bbfaadf08 100644 --- a/crates/doc/src/persistent_name.rs +++ b/crates/doc/src/persistent_name.rs @@ -152,6 +152,27 @@ pub enum NameError { }, } +/// Whether `text` can be written into a name whole. +/// +/// A curve's id goes into the name of the faces the curve makes (`F2:side:`), and a +/// name reads some characters as its own: `|` separates the faces of an edge, `,` the faces +/// around a piece, `~` numbers the edges between two faces, `#` numbers a piece and `]` +/// closes one. An id holding one of them splits every name it lands in: the id `c|1` gave +/// the edges of an extrusion the names `F2:side:c|1#1|F2:side:c|1#2~1`, three separators +/// where an edge names two faces and separates them once (#5858). +/// +/// The test is the name's own round trip rather than a list of characters: the text is +/// written into a name and read back, and it must read back as itself. A list would have +/// to follow every change of the grammar (#5861), and would miss an id that reads back as +/// something else (`s1.l4` reads as profile 1 of curve `l4`). +pub fn text_fits_name(text: &str) -> bool { + let name = format!("F0:side:{text}"); + match FaceName::parse(&name) { + Ok(f) => f.to_string() == name && matches!(&f.base, Base::Made { role: Role::Side { profile: None, curve }, .. } if curve == text), + Err(_) => false, + } +} + impl FaceName { /// The feature that made the face: its base's, a copy's when it is one. pub fn feature(&self) -> Option { diff --git a/crates/doc/src/persistent_name/tests.rs b/crates/doc/src/persistent_name/tests.rs index 899f06dbb..8149d3499 100644 --- a/crates/doc/src/persistent_name/tests.rs +++ b/crates/doc/src/persistent_name/tests.rs @@ -236,3 +236,15 @@ fn hostile_names_never_panic() { // A fillet on an open boundary's edge. assert!(matches!(face("F1:blend:F10:start").base, Base::Blend { rounds: Rounds::Edge(ref e), .. } if e.faces.len() == 1)); } + +#[test] +fn a_text_that_cannot_be_written_into_a_name_whole_is_told() { + // A curve's id goes into the name of the faces the curve makes, so it has to come + // back out of the name as itself (#5858). + for good in ["c1", "l4", "s01.l4", "c-1", "c_1", "a.b", "x"] { + assert!(text_fits_name(good), "`{good}` fits a name"); + } + for bad in ["c|1", "c,1", "c~1", "c]1", "c#1", "", "s1.l4"] { + assert!(!text_fits_name(bad), "`{bad}` does not fit a name"); + } +} diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index e2c5342d0..5668c7144 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -956,6 +956,20 @@ fn finish_sketch(s: &mut Session, _p: &Value) -> Result { // --------------------------------------------------------------------------------------------- // Drawing +/// The id a drawing command gives its geometry, checked against the names that geometry +/// will carry: a curve's id goes into the name of the faces the curve makes +/// (`F2:side:`), and a name reads some characters as its own. An id holding one of +/// them would split every name it lands in, so it is refused here (#5858). +fn named_id(cmd: &str, id: Option<&str>) -> Result> { + match id { + Some(i) if !solidcraft_doc::persistent_name::text_fits_name(i) => { + Err(bad(cmd, format!("the id `{i}` cannot be written into a name whole; give an id without `|`, `,`, `~`, `#` or `]`"))) + } + Some(i) => Ok(Some(i.to_string())), + None => Ok(None), + } +} + fn draw_line(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.line"; let p = &super::sketch_project::snap_points(s, p, cmd)?; @@ -966,6 +980,9 @@ fn draw_line(s: &mut Session, p: &Value) -> Result { let closed = bool_(p, "closed").unwrap_or(false); let infer = bool_(p, "infer").unwrap_or(false); let ids = string_list(cmd, p, "ids")?; + for id in &ids { + named_id(cmd, Some(id))?; + } let ((curves, cons), info) = draw(s, p, cmd, |sk, doc| { let args: Vec = list.iter().map(|v| parg(sk, doc, v, cmd)).collect::>()?; let n = args.len(); @@ -1308,7 +1325,7 @@ fn circle_center(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.circle.center"; let p = &super::sketch_project::snap_points(s, p, cmd)?; let r = radius_arg(p, cmd)?; - let id = str_(p, "id").map(str::to_string); + let id = named_id(cmd, str_(p, "id"))?; let (out, info) = draw(s, p, cmd, |sk, doc| { let c = req_parg(sk, doc, p, "center", cmd)?; // The origin is the sketch's frame, not a hold on the geometry: a circle centred there @@ -1375,7 +1392,7 @@ fn circle_three(s: &mut Session, p: &Value) -> Result { fn arc_three(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.arc.three_point"; let p = &super::sketch_project::snap_points(s, p, cmd)?; - let id = str_(p, "id").map(str::to_string); + let id = named_id(cmd, str_(p, "id"))?; let (out, info) = draw(s, p, cmd, |sk, doc| { let (a, b) = (req_parg(sk, doc, p, "start", cmd)?, req_parg(sk, doc, p, "end", cmd)?); let t = req_parg(sk, doc, p, "through", cmd)?; @@ -1402,7 +1419,7 @@ fn arc_three(s: &mut Session, p: &Value) -> Result { fn arc_center(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.arc.center_point"; let p = &super::sketch_project::snap_points(s, p, cmd)?; - let id = str_(p, "id").map(str::to_string); + let id = named_id(cmd, str_(p, "id"))?; let sweep = num(p, "sweep"); let (out, info) = draw(s, p, cmd, |sk, doc| { let c = req_parg(sk, doc, p, "center", cmd)?; @@ -1597,7 +1614,7 @@ fn slot_overall(s: &mut Session, p: &Value) -> Result { fn draw_point(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.point"; - let id = str_(p, "id").map(str::to_string); + let id = named_id(cmd, str_(p, "id"))?; // A point snapped while drawing (`"mid:"`, `"on::x,y"`, a model vertex or edge): // the snap makes it, held by its constraint, as it does for a line's end. Taken as bare // coordinates it was free to move off the midpoint it was put on (#4691). diff --git a/crates/engine/src/cmd/sketch_ids_tests.rs b/crates/engine/src/cmd/sketch_ids_tests.rs index cb3d61cb5..608eb1fe0 100644 --- a/crates/engine/src/cmd/sketch_ids_tests.rs +++ b/crates/engine/src/cmd/sketch_ids_tests.rs @@ -353,3 +353,32 @@ fn conic_curves_take_the_n_prefix_not_the_constraints_k() { assert!(id.starts_with('n'), "a pattern copy keeps the conic prefix: {id}"); } } + +/// A curve id goes into the face names of the features that use the curve, so an id with a +/// character the name grammar reads (`|`, `,`, `~`, `]`, `#`) would split every name it +/// lands in: an id of `c|1` gave the edges of an extrusion the names +/// `F2:side:c|1#1|F2:side:c|1#2~1`, three separators where an edge has two faces and one +/// separator. An id that cannot be written into a name whole is refused instead (#5858). +#[test] +fn a_curve_id_that_cannot_be_written_into_a_name_is_refused() { + let mut s = new_sketch(); + for id in ["c|1", "c,1", "c~1", "c]1", "c#1"] { + let p = json!({"center": [0, 0], "radius": 5, "id": id}); + let e = match s.execute("sketch.circle.center", &p) { + Ok(v) => panic!("the id `{id}` was taken: {v:?}"), + Err(e) => e.to_string(), + }; + assert!(e.contains(id), "the error names the id: {e}"); + } + // The id that can be written into a name is still taken, and the faces it makes are + // named with it whole. + run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 5, "id": "c1"})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"distance": 10})); + let edges = run(&mut s, "model.edges", json!({"body": "Body1"})); + let names: Vec = edges["edges"].as_array().unwrap().iter().filter_map(|e| e["name"].as_str().map(str::to_string)).collect(); + assert!(!names.is_empty(), "{edges}"); + for name in &names { + assert_eq!(name.matches('|').count(), 1, "an edge names two faces and separates them once: {name}"); + } +}