From 5120dced9e7a6d872cfdd77d1168f89f3001068d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Fri, 27 Nov 2026 17:38:50 -0500 Subject: [PATCH 1/2] fix(layout): refuse a grid, scale bar or north arrow value of the wrong type --- crates/engine/src/cmd/layout/chart.rs | 9 ++ crates/engine/src/cmd/layout/elements.rs | 2 +- crates/engine/src/cmd/layout/frame.rs | 12 +- crates/engine/src/cmd/layout/update.rs | 6 +- crates/engine/src/tests.rs | 3 + .../engine/src/tests/layout_typed_values.rs | 144 ++++++++++++++++++ 6 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 crates/engine/src/tests/layout_typed_values.rs diff --git a/crates/engine/src/cmd/layout/chart.rs b/crates/engine/src/cmd/layout/chart.rs index dc66ab8cf1..034fdae722 100644 --- a/crates/engine/src/cmd/layout/chart.rs +++ b/crates/engine/src/cmd/layout/chart.rs @@ -60,6 +60,15 @@ pub(in crate::cmd::layout) fn given_count(p: &Value, k: &str, hi: usize) -> Resu } } +/// The name given as `k`, when it's given (and not null): an error when it's not text. The caller +/// says which name it doesn't know, in its own words. +pub(in crate::cmd::layout) fn given_name<'a>(p: &'a Value, k: &str, names: &str) -> Result, String> { + match p.get(k).filter(|v| !v.is_null()) { + Some(v) => v.as_str().map(Some).ok_or_else(|| format!("{k} is {names}")), + None => Ok(None), + } +} + /// A choice given as `k` by name, when it's given (and not null): an error when it isn't one of /// `names`. pub(in crate::cmd::layout) fn given_choice(p: &Value, k: &str, from: fn(&str) -> Option, names: &str) -> Result, String> { diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index 0cdcdd3fcf..f3b023190b 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -680,7 +680,7 @@ pub(in crate::cmd::layout) fn orientation(p: &Value) -> Result Result, String> { - match ps(p, key).map(|o| o.trim().to_ascii_lowercase()) { + match given_name(p, key, "horizontal or vertical")?.map(|o| o.trim().to_ascii_lowercase()) { None => Ok(None), Some(o) if o == "horizontal" => Ok(Some(Orientation::Horizontal)), Some(o) if o == "vertical" => Ok(Some(Orientation::Vertical)), diff --git a/crates/engine/src/cmd/layout/frame.rs b/crates/engine/src/cmd/layout/frame.rs index 69bb0f7ec5..34a2691285 100644 --- a/crates/engine/src/cmd/layout/frame.rs +++ b/crates/engine/src/cmd/layout/frame.rs @@ -64,7 +64,7 @@ pub(in crate::cmd::layout) fn grid_params(p: &Value, base: Grid) -> Result Result v.clamp(0.0, rustgis_map::layout::MAX_GRATICULE_STEP), // (0 picks one; a step below a metre is a metre.) @@ -128,16 +128,16 @@ pub(in crate::cmd::layout) fn grid_params(p: &Value, base: Grid) -> Result Result ArrowStyle::from_name(v) .ok_or_else(|| format!("unknown north arrow style '{v}' (classic, compass, triangle or star)"))?, None => *style, @@ -44,13 +44,13 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result { ScaleBarStyle::from_name(v).ok_or_else(|| format!("unknown scale bar style '{v}' (alternating, stepped or line)"))? } None => *style, }; - let units = match ps(p, "units") { + let units = match given_name(p, "units", "auto, kilometers, meters, miles or feet")? { Some(v) => { ScaleUnits::from_name(v).ok_or_else(|| format!("unknown units '{v}' (auto, kilometers, meters, miles or feet)"))? } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f945c219a1..614f81b71e 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -114,6 +114,9 @@ mod layout_edge; #[path = "tests/layout_publication_parts.rs"] mod layout_publication_parts; +#[path = "tests/layout_typed_values.rs"] +mod layout_typed_values; + #[path = "tests/sde_connections.rs"] mod sde_connections; diff --git a/crates/engine/src/tests/layout_typed_values.rs b/crates/engine/src/tests/layout_typed_values.rs new file mode 100644 index 0000000000..84dc226451 --- /dev/null +++ b/crates/engine/src/tests/layout_typed_values.rs @@ -0,0 +1,144 @@ +//! A grid, scale bar or north arrow value of the wrong type is refused, not ignored (#3110). + +use super::*; +use rustgis_map::layout::{Element, ElementKind, Grid}; + +/// A layout with a map frame, a grid on it, a scale bar, a north arrow and a colorbar's layer. +fn laid_out() -> Session { + let mut s = world(); + s.run("layout.new", &json!({"template": "map"})).unwrap(); + for kind in ["grid", "scaleBar", "northArrow"] { + s.run("layout.addElement", &json!({"kind": kind})).unwrap(); + } + s +} + +fn elements(s: &Session) -> &[Element] { + &s.project.layouts[0].elements +} + +/// The id of the last element of the kind `is` picks. +fn id(s: &Session, is: fn(&ElementKind) -> bool) -> u64 { + elements(s).iter().rev().find(|e| is(&e.kind)).unwrap().id.into() +} + +fn frame(s: &Session) -> u64 { + id(s, |k| matches!(k, ElementKind::MapFrame { .. })) +} + +fn grid(s: &Session) -> Grid { + elements(s) + .iter() + .find_map(|e| match &e.kind { + ElementKind::MapFrame { grid, .. } => grid.clone(), + _ => None, + }) + .unwrap() +} + +/// Runs `command` with `params` and expects it refused naming `said`, with nothing changed and no +/// undo step. +fn refused(s: &mut Session, command: &str, params: Value, said: &str) { + let before = serde_json::to_value(&s.project).unwrap(); + let undo = s.undo_label(); + let err = s.run(command, ¶ms).expect_err(¶ms.to_string()); + assert!(err.contains(said), "{params}: {err}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "{params}"); + assert_eq!(s.undo_label(), undo, "{params}"); +} + +fn wrong() -> [Value; 4] { + [json!(17), json!(true), json!([]), json!({})] +} + +#[test] +fn a_grid_name_of_the_wrong_type_is_refused() { + let mut s = laid_out(); + let frame = frame(&s); + for key in ["type", "style", "format", "labelOrientation"] { + for v in wrong() { + let mut add = json!({"kind": "grid"}); + add[key] = v.clone(); + refused(&mut s, "layout.addElement", add, &format!("{key} is ")); + let mut options = json!({}); + options[key] = v; + refused(&mut s, "layout.update", json!({"element": frame, "grid": options}), &format!("{key} is ")); + } + } +} + +#[test] +fn a_grid_number_of_the_wrong_type_is_refused() { + let mut s = laid_out(); + let frame = frame(&s); + for key in ["step", "tickLength", "width"] { + for v in [json!("bad"), json!(true), json!([]), json!({})] { + let mut add = json!({"kind": "grid"}); + add[key] = v.clone(); + refused(&mut s, "layout.addElement", add, &format!("{key} is ")); + let mut options = json!({}); + options[key] = v; + refused(&mut s, "layout.update", json!({"element": frame, "grid": options}), &format!("{key} is ")); + } + } +} + +#[test] +fn a_scale_bar_or_north_arrow_value_of_the_wrong_type_is_refused() { + let mut s = laid_out(); + let bar = id(&s, |k| matches!(k, ElementKind::ScaleBar { .. })); + let arrow = id(&s, |k| matches!(k, ElementKind::NorthArrow { .. })); + for v in wrong() { + for (kind, element, key) in [("scaleBar", bar, "style"), ("scaleBar", bar, "units"), ("northArrow", arrow, "style")] { + let mut add = json!({"kind": kind}); + add[key] = v.clone(); + refused(&mut s, "layout.addElement", add, &format!("{key} is ")); + let mut update = json!({"element": element}); + update[key] = v.clone(); + refused(&mut s, "layout.update", update, &format!("{key} is ")); + } + } +} + +#[test] +fn a_publication_part_value_of_the_wrong_type_is_refused_and_nothing_is_made() { + for (part, key) in [("grid", "type"), ("grid", "step"), ("scaleBar", "units"), ("scaleBar", "style"), ("northArrow", "style")] { + let mut s = world(); + let mut options = json!({}); + options[key] = json!([17]); + let mut p = json!({"profile": "a4-portrait", "extent": [-30.0, -30.0, -20.0, -22.0]}); + p[part] = options; + refused(&mut s, "layout.publication", p, &format!("{key} is ")); + } +} + +#[test] +fn a_null_value_is_not_given() { + let mut s = laid_out(); + let (frame, before) = (frame(&s), grid(&s)); + let nulls = + json!({"type": null, "style": null, "format": null, "labelOrientation": null, "step": null, "tickLength": null, "width": null}); + s.run("layout.update", &json!({"element": frame, "grid": nulls})).unwrap(); + assert_eq!(grid(&s), before); + let bar = id(&s, |k| matches!(k, ElementKind::ScaleBar { .. })); + let kept = serde_json::to_value(elements(&s)).unwrap(); + s.run("layout.update", &json!({"element": bar, "style": null, "units": null})).unwrap(); + assert_eq!(serde_json::to_value(elements(&s)).unwrap(), kept); +} + +#[test] +fn a_number_given_as_text_and_a_known_name_are_read_as_before() { + let mut s = laid_out(); + let frame = frame(&s); + s.run("layout.update", &json!({"element": frame, "grid": {"step": " 5 ", "style": "ticks", "tickLength": "3"}})).unwrap(); + let g = grid(&s); + assert_eq!((g.step, g.tick_length, g.style.code()), (5.0, 3.0, "ticks")); + let mut s = laid_out(); + refused(&mut s, "layout.addElement", json!({"kind": "grid", "style": "wavy"}), "unknown grid style 'wavy'"); +} + +#[test] +fn a_colorbar_orientation_of_the_wrong_type_is_refused() { + let mut s = laid_out(); + refused(&mut s, "layout.addElement", json!({"kind": "colorbar", "orientation": 17}), "orientation is "); +} From 30dadf2efba4cc81c415eb0ec5401bbf1e6d9929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Fri, 27 Nov 2026 17:52:59 -0500 Subject: [PATCH 2/2] fix(layout): take a null scale bar divisions as not given --- crates/engine/src/cmd/layout/chart.rs | 5 +---- crates/engine/src/cmd/layout/update.rs | 6 ++---- crates/engine/src/tests/layout_typed_values.rs | 6 +++--- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/layout/chart.rs b/crates/engine/src/cmd/layout/chart.rs index 034fdae722..eedeb2e74f 100644 --- a/crates/engine/src/cmd/layout/chart.rs +++ b/crates/engine/src/cmd/layout/chart.rs @@ -72,10 +72,7 @@ pub(in crate::cmd::layout) fn given_name<'a>(p: &'a Value, k: &str, names: &str) /// A choice given as `k` by name, when it's given (and not null): an error when it isn't one of /// `names`. pub(in crate::cmd::layout) fn given_choice(p: &Value, k: &str, from: fn(&str) -> Option, names: &str) -> Result, String> { - match p.get(k).filter(|v| !v.is_null()) { - Some(v) => v.as_str().and_then(from).map(Some).ok_or_else(|| format!("{k} is {names}")), - None => Ok(None), - } + given_name(p, k, names)?.map(|v| from(v).ok_or_else(|| format!("{k} is {names}"))).transpose() } /// A chart's options beyond its layer and field ([`ElementKind::Chart`], #2487). diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index f6ef5d1b92..e1bee2c673 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -56,10 +56,8 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result *units, }; - let divisions = match p.get("divisions") { - Some(_) => f(p, "divisions").ok_or("divisions is a number")?.round().clamp(1.0, MAX_SCALE_DIVISIONS as f64) as usize, - None => *divisions, - }; + let divisions = + given_num(p, "divisions", "a number")?.map_or(*divisions, |n| n.round().clamp(1.0, MAX_SCALE_DIVISIONS as f64) as usize); let (color, fill) = (color(p, "color", *c)?, color(p, "fill", *fill)?); let text_size = text_size_param(p, "textSize", SCALE_BAR_TEXT_PT)?.unwrap_or(*text_size); // Its details, each read before any is set: one part is the division whole. diff --git a/crates/engine/src/tests/layout_typed_values.rs b/crates/engine/src/tests/layout_typed_values.rs index 84dc226451..97bdf5da3c 100644 --- a/crates/engine/src/tests/layout_typed_values.rs +++ b/crates/engine/src/tests/layout_typed_values.rs @@ -3,7 +3,7 @@ use super::*; use rustgis_map::layout::{Element, ElementKind, Grid}; -/// A layout with a map frame, a grid on it, a scale bar, a north arrow and a colorbar's layer. +/// A layout with a map frame, a grid on it, a scale bar and a north arrow. fn laid_out() -> Session { let mut s = world(); s.run("layout.new", &json!({"template": "map"})).unwrap(); @@ -72,7 +72,7 @@ fn a_grid_number_of_the_wrong_type_is_refused() { let mut s = laid_out(); let frame = frame(&s); for key in ["step", "tickLength", "width"] { - for v in [json!("bad"), json!(true), json!([]), json!({})] { + for v in [json!("bad"), json!(""), json!(true), json!([]), json!({})] { let mut add = json!({"kind": "grid"}); add[key] = v.clone(); refused(&mut s, "layout.addElement", add, &format!("{key} is ")); @@ -122,7 +122,7 @@ fn a_null_value_is_not_given() { assert_eq!(grid(&s), before); let bar = id(&s, |k| matches!(k, ElementKind::ScaleBar { .. })); let kept = serde_json::to_value(elements(&s)).unwrap(); - s.run("layout.update", &json!({"element": bar, "style": null, "units": null})).unwrap(); + s.run("layout.update", &json!({"element": bar, "style": null, "units": null, "divisions": null})).unwrap(); assert_eq!(serde_json::to_value(elements(&s)).unwrap(), kept); }