From 6eeabcd74268bd021d33f19a008764d46487552c Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 11 Oct 2026 01:45:18 +0700 Subject: [PATCH] Review #986: the rect through finite_numbers; rustfmt Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/cut.rs | 8 +++----- crates/engine/src/tests_cut.rs | 7 +------ 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/cut.rs b/crates/engine/src/cmd/cut.rs index 1aafe245..a7a2e47f 100644 --- a/crates/engine/src/cmd/cut.rs +++ b/crates/engine/src/cmd/cut.rs @@ -221,11 +221,9 @@ fn rect_cut(s: &mut Session, p: &Value) -> Result { const C: &str = "path.rectCut"; let rect = match p.get("rect") { Some(v) => { - // Every component must be present and numeric. Dropping invalid elements would - // silently cut the wrong region when a malformed five-element array is supplied. - let values = v.as_array().filter(|a| a.len() == 4).and_then(|a| a.iter().map(Value::as_f64).collect::>>()); - let Some([x, y, w, h]) = values.as_deref() else { return Err(bad(C, "rect must be [x, y, width, height]")) }; - Rect::new(*x, *y, x + w, y + h).abs() + // All four or nothing: a malformed entry dropped would cut a different region. + let [x, y, w, h] = finite_numbers(v).ok_or_else(|| bad(C, "rect must be [x, y, width, height]"))?; + Rect::new(x, y, x + w, y + h).abs() } None => Rect::from_points(point_req(p, "from", C)?, point_req(p, "to", C)?), }; diff --git a/crates/engine/src/tests_cut.rs b/crates/engine/src/tests_cut.rs index 575e489b..a0a0d2a7 100644 --- a/crates/engine/src/tests_cut.rs +++ b/crates/engine/src/tests_cut.rs @@ -118,7 +118,6 @@ fn line_cut_that_misses_changes_nothing() { assert!(s.execute("path.lineCut", &json!({"from": [5, 5]})).is_err()); } - #[test] fn rect_cut_rejects_non_numeric_rectangle_elements_without_modifying_art() { let mut s = session(); @@ -127,11 +126,7 @@ fn rect_cut_rejects_non_numeric_rectangle_elements_without_modifying_art() { let original = bounds(&s, id); let history = undo_depth(&s); // The old parser dropped the string and used the four remaining values. - for params in [ - json!([0, "ignored", 10, 50, 50]), - json!([0, 10, 50, null, 50]), - json!([0, 10, 50, 50, 5]), - ] { + for params in [json!([0, "ignored", 10, 50, 50]), json!([0, 10, 50, null, 50]), json!([0, 10, 50, 50, 5])] { assert!(s.execute("path.rectCut", &json!({"rect": params})).is_err()); assert!(near(bounds(&s, id), original), "failed cut must not change geometry"); assert_eq!(undo_depth(&s), history, "failed cut must not add undo history");