* Sketch modify: name the public command in the split and trim errors (#4712) Part of #4712. The split inside the sketch modify tools refused in the name of the internal `split` step, and the free-form trim in the name of the internal `trim` one: `invalid parameters for `split`: a circle needs two points to split at` names a command nobody can write, so an agent or script reading the message has nowhere to go. Both now name the public command the caller used: `sketch.modify` for the split shared by trim and break, `sketch.trim` for the trim. The descriptions are unchanged. Co-authored-by: Hermes Agent <noreply@anthropic.com> * Sketch modify: the split names the command the caller used (#4712) `sketch.modify` is not a command a caller can write: the registered sketch commands are `sketch.trim`, `sketch.trim.path` and `sketch.break`. The split shared by trim and break therefore refused in the name of a command that does not exist, and a script reading the message had nowhere to go. `split_at` takes the command of its caller and refuses in that name: `sketch.trim` when the trim splits the curve, `sketch.break` when the break does. Both callers already had the command in hand. The test now states the decision: the refusal of `sketch.break` carries `sketch.break`, and the rendered text names no other command. Co-authored-by: Hermes Agent <noreply@anthropic.com> --------- Co-authored-by: Hermes Agent <noreply@anthropic.com>
This commit is contained in:
1 parent
796b7c40d3
commit
51f4074506
3 files changed
+30
-8
No files matched your search
@@ -176,7 +176,7 @@ fn add_pieces(sk: &mut Sketch, segs: &[Seg2], start: Option<usize>, end: Option<
|
||||
pts.push(to);
|
||||
CurveKind::Spline { pts, control: true, degree: n, periodic: false, nurbs: None }
|
||||
}
|
||||
Seg2::Arc { .. } => return Err(bad("trim", "unexpected arc piece")),
|
||||
Seg2::Arc { .. } => return Err(bad("sketch.trim", "unexpected arc piece")),
|
||||
};
|
||||
let c = sk.add_curve(kind, None)?;
|
||||
if let Some(cu) = sk.curves.get_mut(c) {
|
||||
|
||||
@@ -233,9 +233,12 @@ fn pin(sk: &mut Sketch, p: usize, cutter: usize) -> Result<()> {
|
||||
|
||||
/// Split curve `ci` at interior parameters `ts` (sorted). Returns the pieces in parameter
|
||||
/// order (the first keeps the original index) and the new points.
|
||||
fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec<usize>, Vec<usize>)> {
|
||||
let sh = sk.shape(ci).ok_or_else(|| bad("split", "curve"))?;
|
||||
let kind = sk.curves.get(ci).map(|c| c.kind.clone()).ok_or_else(|| bad("split", "curve"))?;
|
||||
///
|
||||
/// `cmd` is the command the caller used (`sketch.trim`, `sketch.trim.path`, `sketch.break`):
|
||||
/// a refusal names the command the caller can write, never an internal step.
|
||||
fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64], cmd: &str) -> Result<(Vec<usize>, Vec<usize>)> {
|
||||
let sh = sk.shape(ci).ok_or_else(|| bad(cmd, "curve"))?;
|
||||
let kind = sk.curves.get(ci).map(|c| c.kind.clone()).ok_or_else(|| bad(cmd, "curve"))?;
|
||||
let construction = sk.curves.get(ci).is_some_and(|c| c.construction);
|
||||
let mut pts = Vec::new();
|
||||
for t in ts {
|
||||
@@ -273,7 +276,7 @@ fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec<usize>, Vec<u
|
||||
}
|
||||
CurveKind::Circle { c, .. } => {
|
||||
if pts.len() < 2 {
|
||||
return Err(bad("split", "a circle needs two points to split at"));
|
||||
return Err(bad(cmd, "a circle needs two points to split at"));
|
||||
}
|
||||
let mut chain = pts.clone();
|
||||
if let Some(f) = pts.first() {
|
||||
@@ -287,7 +290,7 @@ fn split_at(sk: &mut Sketch, ci: usize, ts: &[f64]) -> Result<(Vec<usize>, Vec<u
|
||||
push(sk, CurveKind::Arc { c, a: w[0], b: w[1] }, "a")?;
|
||||
}
|
||||
}
|
||||
_ => return Err(bad("split", "not supported yet for ellipses, splines and conics")),
|
||||
_ => return Err(bad(cmd, "not supported yet for ellipses, splines and conics")),
|
||||
}
|
||||
Ok((pieces, pts))
|
||||
}
|
||||
@@ -668,7 +671,7 @@ fn remove_piece(sk: &mut Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<(Valu
|
||||
release_extent(sk, ci);
|
||||
let tangencies = endpoint_tangencies(sk, ci);
|
||||
let ts: Vec<f64> = cuts.iter().map(|c| c.0).collect();
|
||||
let (pieces, pts) = split_at(sk, ci, &ts)?;
|
||||
let (pieces, pts) = split_at(sk, ci, &ts, cmd)?;
|
||||
// The piece containing tq.
|
||||
let k = if full {
|
||||
let (a, b) = (ts[0], ts[1]);
|
||||
@@ -938,7 +941,7 @@ fn break_cmd(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// Its length (and its midpoint, an equal length) measured the whole curve: left on the
|
||||
// first piece, it would stretch that piece back to the old size and skew the sketch.
|
||||
release_extent(sk, ci);
|
||||
let (pieces, pts) = split_at(sk, ci, &ts)?;
|
||||
let (pieces, pts) = split_at(sk, ci, &ts, cmd)?;
|
||||
for (pi, c) in pts.iter().zip(&cutters) {
|
||||
if let Some(c) = c {
|
||||
pin(sk, *pi, *c)?;
|
||||
|
||||
@@ -5545,3 +5545,22 @@ fn breaking_again_where_a_curve_was_broken_adds_nothing() {
|
||||
}
|
||||
assert!(k.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Distance { value, .. } if close(value, 20.0))), "{:?}", k.constraints);
|
||||
}
|
||||
|
||||
/// #4712: the split inside the sketch modify tools refuses in the name of the public
|
||||
/// command the caller used, not an internal step: an error must never name a command the
|
||||
/// caller cannot write.
|
||||
#[test]
|
||||
fn split_failures_name_the_public_command() {
|
||||
let mut s = new_sketch();
|
||||
// A circle, split at one point: the circle needs two points to split at.
|
||||
let circle = ids(&run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 10}))["curves"])[0].clone();
|
||||
let error = s.execute("sketch.break", &json!({"curve": circle, "points": [[10, 0]]})).expect_err("a circle needs two points to split at");
|
||||
let crate::EngineError::BadParams { cmd, msg } = &error else { panic!("a parameter error: {error}") };
|
||||
assert_eq!(cmd, "sketch.break", "{error}");
|
||||
assert_eq!(msg, "a circle needs two points to split at");
|
||||
// The message the caller reads names the public command, never an internal step.
|
||||
let text = error.to_string();
|
||||
assert!(text.contains("invalid parameters for `sketch.break`"), "{text}");
|
||||
assert!(!text.contains("invalid parameters for `split`"), "{text}");
|
||||
assert!(!text.contains("sketch.modify"), "{text}");
|
||||
}
|
||||
Reference in new issue
Block a user