Reject invalid entries in vector.create points instead of dropping them

Found while testing crates/engine/src/cmd/create.rs:~286 (create, the `points` branch using filter_map)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilverandClaude Haiku 5.5 committed 2026-10-31 17:23:54 +01:00
1 parent e04c5d2274
commit 1aa6b81d01
2 files changed
+17 -1

No files matched your search

+8 -1
View File
@@ -302,7 +302,14 @@ pub fn create(s: &mut Session, p: &Value) -> Result<Value> {
}
(w, h, offset) = (shape.width, shape.height, shape.offset);
} else if let Some(Value::Array(pts)) = get(p, "points") {
let pts: Vec<Point> = pts.iter().filter_map(|v| Some(Point::new(f64_of(v.get(0)?)?, f64_of(v.get(1)?)?))).collect();
let pts: Vec<Point> = pts
.iter()
.map(|v| {
let xy = v.as_array().filter(|a| a.len() == 2)?;
Some(Point::new(f64_of(&xy[0])?, f64_of(&xy[1])?))
})
.collect::<Option<_>>()
.ok_or_else(|| bad(cmd, "`points` entries must be numeric [x, y] pairs"))?;
if pts.len() < 2 {
return Err(bad(cmd, "`points` needs at least two [x, y] pairs"));
}
+9
View File
@@ -378,3 +378,12 @@ fn the_text_on_a_path_tool_carries_a_fill_bound_through_fills() {
let t = editing(&s).expect("typing starts on the path");
assert_eq!(s.doc().unwrap().node(t).unwrap().bound, bound, "the fill keeps following its variable");
}
#[test]
fn bad_point_is_rejected() {
let mut s = Session::new();
assert!(s.execute("vector.create", &json!({"points": [[0, 0], ["abc", 5], [100, 100]], "closed": false})).is_err());
assert!(s.execute("vector.create", &json!({"points": [[0, 0], [1], [100, 100]]})).is_err());
assert!(s.execute("vector.create", &json!({"points": [[0, 0], 7, [100, 100]]})).is_err());
assert!(s.execute("vector.create", &json!({"points": [[0, 0], [50, 5], [100, 100]]})).is_ok());
}