From ad43722a5679f377a77f8f84f85ad4e74eb68d66 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Tue, 27 Oct 2026 10:37:07 +0100 Subject: [PATCH] Accept whole-number floats as grid divider counts Found while testing crates/engine/src/cmd/create.rs:584 (dividers) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/engine/src/cmd/create.rs | 5 +++-- crates/engine/src/tests/draw2.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/create.rs b/crates/engine/src/cmd/create.rs index 11c479023..9249a48f4 100644 --- a/crates/engine/src/cmd/create.rs +++ b/crates/engine/src/cmd/create.rs @@ -591,11 +591,12 @@ fn grid_group(s: &mut Session, label: &str, (paths, bbox_angle): (Vec, } /// A grid's divider count `count` (an error unless it is a number or null; 5 unless it is a JSON -/// integer of 0 or more, so 4.0 and 2.5 give 5; at most 999) with its skew `key` in percent (0 when +/// whole number of 0 or more, so 4.0 gives 4 and 2.5 gives 5; at most 999) with its skew `key` in percent (0 when /// absent, clamped to ±[`shapes::GRID_SKEW_MAX`]). fn dividers(p: &Value, count: &str, key: &str, cmd: &str) -> Result<(u32, f64)> { check_numbers(p, &[count], cmd)?; - let n = p.get(count).and_then(Value::as_u64).unwrap_or(5).min(999) as u32; + let whole = |v: &Value| v.as_u64().or_else(|| v.as_f64().filter(|f| f.is_finite() && *f >= 0.0 && f.fract() == 0.0).map(|f| f as u64)); + let n = p.get(count).and_then(whole).unwrap_or(5).min(999) as u32; let skew = match p.get(key) { None | Some(Value::Null) => 0.0, Some(v) => v.as_f64().filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, format!("`{key}` must be a number")))?, diff --git a/crates/engine/src/tests/draw2.rs b/crates/engine/src/tests/draw2.rs index 7bb0a3edf..c43362c0b 100644 --- a/crates/engine/src/tests/draw2.rs +++ b/crates/engine/src/tests/draw2.rs @@ -1076,6 +1076,22 @@ fn polar_grid_options_compound_fill_and_skew() { assert!(tips(&inward)[1..].iter().all(|t| t.x < 50.0), "counterclockwise of the top: {:?}", tips(&inward)); } +/// A divider count written as a whole float (`3.0`) is the same as `3`; a fraction gives the default 5. +#[test] +fn grid_divider_counts_accept_whole_floats() { + let mut s = session(); + let size = json!({"x": 0, "y": 0, "width": 100, "height": 100}); + let float = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 3.0, "columns": 3.0}))); + let int = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 3, "columns": 3}))); + assert_eq!(float.len(), int.len()); + assert_eq!(float.len(), 7); + let float = grid(&mut s, "shape.polarGrid", with(&size, json!({"concentric": 2.0, "radial": 3.0}))); + let int = grid(&mut s, "shape.polarGrid", with(&size, json!({"concentric": 2, "radial": 3}))); + assert_eq!(float.len(), int.len()); + let frac = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 2.5, "columns": 2.5}))); + assert_eq!(frac.len(), 11); +} + /// The grid options are checked: a divider count or a skew is a number and a switch true or false, /// or the command fails without an undo step. #[test]