diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index f3b023190b..758c0ab113 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -353,7 +353,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu } "legend" => { let frame = frame_of(l, p)?; - let title = crate::cmd::within("title", "a legend's title", ps(p, "title").unwrap_or("Legend"), 200)?; + let title = crate::cmd::within("title", "a legend's title", given_name(p, "title", "text")?.unwrap_or("Legend"), 200)?; let all_classes = b(p, "allClasses").unwrap_or(false); let hide_other = b(p, "hideOther").unwrap_or(false); let show_no_data = b(p, "showNoData").unwrap_or(false); @@ -388,8 +388,9 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu } "colorbar" => { let frame = frame_of(l, p)?; - let layer = colorbar_layer(s, l, frame, ps(p, "layer"))?; - let title = match ps(p, "title") { + let named = given_name(p, "layer", "a raster layer's name")?; + let layer = colorbar_layer(s, l, frame, named)?; + let title = match given_name(p, "title", "text")? { Some(t) => crate::cmd::within("title", "a colorbar's title", t, 200)?, None => layer.clone().unwrap_or_default(), }; @@ -402,10 +403,10 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu frame, // Kept only when named: otherwise (none, or "") it follows the map's top shown raster with a // colour scale, not drawn by classes. - layer: ps(p, "layer").filter(|n| !n.trim().is_empty()).and(layer), + layer: named.filter(|n| !n.trim().is_empty()).and(layer), title, orientation, - ticks: ticks(p).unwrap_or(5), + ticks: ticks(p)?.unwrap_or(5), tick_side: tick_side(p)?.unwrap_or_default(), reverse: b(p, "reverse").unwrap_or(false), color: ink, @@ -689,14 +690,14 @@ pub(in crate::cmd::layout) fn orientation_of(p: &Value, key: &str) -> Result Result, String> { - match ps(p, "tickSide") { + match given_name(p, "tickSide", "below, above, right or left")? { None => Ok(None), Some(t) => TickSide::from_name(t).map(Some).ok_or_else(|| format!("unknown tickSide '{t}' (below, above, right or left)")), } } -pub(in crate::cmd::layout) fn ticks(p: &Value) -> Option { - f(p, "ticks").filter(|v| v.is_finite()).map(|v| v.round().clamp(2.0, MAX_COLORBAR_TICKS as f64) as usize) +pub(in crate::cmd::layout) fn ticks(p: &Value) -> Result, String> { + Ok(given_num(p, "ticks", "a number")?.map(|v| v.round().clamp(2.0, MAX_COLORBAR_TICKS as f64) as usize)) } /// An extent indicator's outline colour and width (points) from `color` and `width` in `p`, each diff --git a/crates/engine/src/cmd/layout/frame.rs b/crates/engine/src/cmd/layout/frame.rs index 34a2691285..7309d321ce 100644 --- a/crates/engine/src/cmd/layout/frame.rs +++ b/crates/engine/src/cmd/layout/frame.rs @@ -3,7 +3,7 @@ use super::*; pub(in crate::cmd::layout) fn add_frame(s: &mut Session, p: &Value) -> CmdResult { - let shape = match ps(p, "shape").unwrap_or("rectangle") { + let shape = match given_name(p, "shape", "rectangle, circle, ellipse, polygon or lasso")?.unwrap_or("rectangle") { "rectangle" => FrameShape::Rectangle, "circle" => FrameShape::Circle, "ellipse" => FrameShape::Ellipse, diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index e1bee2c673..ef1410915b 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -183,6 +183,10 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { // ([`ElementKind::null_resets`]), so they are changes too. let current = s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind); let bar = matches!(current, Some(ElementKind::ScaleBar { .. })); + // A chart's title is read with its options, which refuse one that isn't text. + if matches!(current, Some(ElementKind::Legend { .. } | ElementKind::Colorbar { .. })) { + given_name(p, "title", "text")?; + } let null_changes = |k: &str| matches!(k, "dash" | "background" | "border") || current.is_some_and(|kind| kind.null_resets(k)); let changes = p.as_object().is_some_and(|o| { o.iter().any(|(k, v)| (!v.is_null() || null_changes(k.as_str())) && !matches!(k.as_str(), "layout" | "element" | "locked")) @@ -203,18 +207,18 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { let l = s.project.layouts.get(li).ok_or("no layout")?; match l.element(id).map(|e| &e.kind) { Some(ElementKind::Colorbar { frame, .. }) => { - let layer = match ps(p, "layer") { + let layer = match given_name(p, "layer", "a raster layer's name")? { Some(n) if n.trim().is_empty() => Some(None), Some(n) => Some(colorbar_layer(s, l, *frame, Some(n))?), None => None, }; - Some((layer, orientation(p)?, tick_side(p)?)) + Some((layer, orientation(p)?, tick_side(p)?, ticks(p)?)) } _ => None, } }; // Set back to no layer: it follows the map's default, which may have nothing to show (#1679). - let emptied_colorbar = matches!(&colorbar, Some((Some(None), _, _))); + let emptied_colorbar = matches!(&colorbar, Some((Some(None), ..))); // A north arrow's or scale bar's new look, and a map frame's grid: `"grid": false` removes it, // `true` adds the default one, an object changes the options it names, and null (not set) // leaves it as it is. @@ -495,7 +499,7 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { *ink = col; } // A colorbar turned keeps its size: its length and thickness swap, unless given. - if let (ElementKind::Colorbar { orientation, .. }, Some((_, Some(o), _))) = (&e.kind, &colorbar) + if let (ElementKind::Colorbar { orientation, .. }, Some((_, Some(o), ..))) = (&e.kind, &colorbar) && o != orientation && f(p, "w").is_none() && f(p, "h").is_none() @@ -602,7 +606,7 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { if let Some(t) = new_title { *title = t; } - if let Some((new_layer, new_orientation, new_side)) = colorbar { + if let Some((new_layer, new_orientation, new_side, new_ticks)) = colorbar { if let Some(nl) = new_layer { *layer = nl; } @@ -612,9 +616,9 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { if let Some(t) = new_side { *tick_side = t; } - } - if let Some(v) = ticks(p) { - *n = v; + if let Some(v) = new_ticks { + *n = v; + } } if let Some(v) = b(p, "reverse") { *reverse = v; diff --git a/crates/engine/src/tests/layout_typed_values.rs b/crates/engine/src/tests/layout_typed_values.rs index 97bdf5da3c..518efd44b6 100644 --- a/crates/engine/src/tests/layout_typed_values.rs +++ b/crates/engine/src/tests/layout_typed_values.rs @@ -1,7 +1,7 @@ -//! A grid, scale bar or north arrow value of the wrong type is refused, not ignored (#3110). +//! A layout value of the wrong type is refused, not ignored (#3110, #3169). use super::*; -use rustgis_map::layout::{Element, ElementKind, Grid}; +use rustgis_map::layout::{Element, ElementKind, Grid, TickSide}; /// A layout with a map frame, a grid on it, a scale bar and a north arrow. fn laid_out() -> Session { @@ -142,3 +142,71 @@ 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 "); } + +/// [`laid_out`], with a legend and a colorbar too. +fn with_keys() -> Session { + let mut s = laid_out(); + for kind in ["legend", "colorbar"] { + s.run("layout.addElement", &json!({"kind": kind})).unwrap(); + } + s +} + +#[test] +fn a_colorbar_or_legend_value_of_the_wrong_type_is_refused() { + let mut s = with_keys(); + let bar = id(&s, |k| matches!(k, ElementKind::Colorbar { .. })); + let legend = id(&s, |k| matches!(k, ElementKind::Legend { .. })); + for v in wrong() { + let keys = [("colorbar", bar, "tickSide"), ("colorbar", bar, "ticks"), ("colorbar", bar, "layer")]; + for (kind, element, key) in keys.into_iter().chain([("colorbar", bar, "title"), ("legend", legend, "title")]) { + // A number is what ticks takes: text that isn't one stands in for it. + let v = if key == "ticks" && v.is_number() { json!("many") } else { v.clone() }; + 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 ")); + } + } + // The title beside it isn't changed either. + refused(&mut s, "layout.update", json!({"element": bar, "title": "Height", "ticks": "many"}), "ticks is "); +} + +#[test] +fn a_frame_shape_of_the_wrong_type_is_refused() { + let mut s = laid_out(); + for v in wrong() { + refused(&mut s, "layout.addFrame", json!({"shape": v}), "shape is "); + } + refused(&mut s, "layout.addFrame", json!({"shape": "hex"}), "unknown frame shape 'hex'"); +} + +#[test] +fn a_null_or_valid_colorbar_legend_or_shape_value_is_read_as_before() { + let mut s = with_keys(); + let bar = id(&s, |k| matches!(k, ElementKind::Colorbar { .. })); + let legend = id(&s, |k| matches!(k, ElementKind::Legend { .. })); + let kept = serde_json::to_value(elements(&s)).unwrap(); + s.run("layout.update", &json!({"element": bar, "tickSide": null, "ticks": null, "layer": null, "title": null})).unwrap(); + s.run("layout.update", &json!({"element": legend, "title": null})).unwrap(); + assert_eq!(serde_json::to_value(elements(&s)).unwrap(), kept); + s.run("layout.addElement", &json!({"kind": "colorbar", "tickSide": null, "ticks": null, "layer": null, "title": null})).unwrap(); + s.run("layout.addElement", &json!({"kind": "legend", "title": null})).unwrap(); + assert!(matches!(&elements(&s).last().unwrap().kind, ElementKind::Legend { title, .. } if title == "Legend")); + s.run("layout.update", &json!({"element": bar, "tickSide": "above", "ticks": "7", "layer": "", "title": "Height"})).unwrap(); + let e = elements(&s).iter().find(|e| u64::from(e.id) == bar).unwrap(); + assert!( + matches!(&e.kind, ElementKind::Colorbar { tick_side: TickSide::Start, ticks: 7, layer: None, title, .. } if title == "Height"), + "{:?}", + e.kind + ); + s.run("layout.addFrame", &json!({"shape": null})).unwrap(); + s.run("layout.addFrame", &json!({"shape": "circle"})).unwrap(); + refused(&mut s, "layout.update", json!({"element": bar, "tickSide": "sideways"}), "unknown tickSide 'sideways'"); + // A chart says what its own title takes, as before. + s.run("layout.addElement", &json!({"kind": "chart", "layer": "Countries"})).unwrap(); + let chart = id(&s, |k| matches!(k, ElementKind::Chart { .. })); + refused(&mut s, "layout.update", json!({"element": chart, "title": 17}), "a chart's title is a text"); +}