diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index 758c0ab113..9f2160f75f 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -51,6 +51,17 @@ pub(in crate::cmd::layout) fn given_num(p: &Value, k: &str, what: &str) -> Resul } } +/// A table's "rows", when it's given (and not null), held to 1 to 500: an error when it's not a +/// whole number of 0 or more. +pub(in crate::cmd::layout) fn table_rows(p: &Value) -> Result, String> { + let what = "a whole number of 0 or more"; + match given_num(p, "rows", what)? { + Some(n) if n >= 0.0 && n.fract() == 0.0 => Ok(Some(n.clamp(1.0, 500.0) as usize)), + Some(_) => Err(format!("rows is {what}")), + None => Ok(None), + } +} + /// A line's "dir": `default` when it's not given (or null), an error when it's not a direction's /// name. pub(in crate::cmd::layout) fn line_dir(p: &Value, default: LineDir) -> Result { @@ -448,7 +459,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu "table" => { let frame = frame_of(l, p)?; let layer = frame_layer_name(s, l, frame, p)?; - let rows = p.get("rows").and_then(Value::as_u64).unwrap_or(12).clamp(1, 500) as usize; + let rows = table_rows(p)?.unwrap_or(12); ("Table Frame", place(p, l, (15.0, ph * 0.6, pw * 0.5, ph * 0.35))?, ElementKind::Table { frame, layer, rows, color: ink }) } "chart" => { @@ -461,7 +472,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu return Err(said); } // Not given: the first numeric field, or for a count the field it groups by. - let field = match ps(p, "field") { + let field = match given_name(p, "field", "a field's name")? { Some(fl) => ds.field_index(fl).and_then(|i| ds.fields.get(i)).ok_or_else(|| format!("no field '{fl}'"))?, None => ds .fields diff --git a/crates/engine/src/cmd/layout/inputs.rs b/crates/engine/src/cmd/layout/inputs.rs index 8c23837a49..8cb22cd764 100644 --- a/crates/engine/src/cmd/layout/inputs.rs +++ b/crates/engine/src/cmd/layout/inputs.rs @@ -16,7 +16,7 @@ pub(in crate::cmd::layout) fn frame_layer_name( _ => return Err("no such frame".into()), }; let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?; - let found = match ps(p, "layer") { + let found = match given_name(p, "layer", "a feature layer's name")? { Some(n) => m.layer_by_name(n), None => m.layers.iter().find(|l| !l.is_raster() && !l.graphics), }; diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index ef1410915b..961e2588d6 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -196,6 +196,11 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { } // A new layer (tables and charts) or field (charts) must exist in the frame's map. let checked = checked_update(s, p, li, id)?; + // Only tables have rows: read before anything changes. + let new_rows = match s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind) { + Some(ElementKind::Table { .. }) => table_rows(p)?, + _ => None, + }; // A chart's options, read over its own on the layer it will chart. let (new_chart, chart_field, chart_said) = match chart_update(s, p, li, id, checked.as_ref())? { Some((o, f, said)) => (Some(o), f, said), @@ -670,8 +675,8 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { } } ElementKind::Table { rows, layer, .. } => { - if let Some(v) = p.get("rows").and_then(Value::as_u64) { - *rows = v.clamp(1, 500) as usize; + if let Some(v) = new_rows { + *rows = v; } if let Some((l, _)) = &checked { *layer = l.clone(); @@ -826,7 +831,15 @@ pub(in crate::cmd::layout) fn checked_update( Ok({ let l = s.project.layouts.get(li).ok_or("no layout")?; let e = l.element(id).ok_or_else(|| format!("no element {id}"))?; - match (&e.kind, ps(p, "layer"), ps(p, "field")) { + let new_layer = match &e.kind { + ElementKind::Table { .. } | ElementKind::Chart { .. } => given_name(p, "layer", "a feature layer's name")?, + _ => None, + }; + let new_field = match &e.kind { + ElementKind::Chart { .. } => given_name(p, "field", "a field's name")?, + _ => ps(p, "field"), + }; + match (&e.kind, new_layer, new_field) { (ElementKind::Table { frame, layer, .. } | ElementKind::Chart { frame, layer, .. }, nl, nf) if nl.is_some() || nf.is_some() => { let map = match l.element(*frame).map(|f| &f.kind) { Some(ElementKind::MapFrame { map, .. }) => map.clone(), diff --git a/crates/engine/src/tests/layout_typed_values.rs b/crates/engine/src/tests/layout_typed_values.rs index 518efd44b6..72749f8b74 100644 --- a/crates/engine/src/tests/layout_typed_values.rs +++ b/crates/engine/src/tests/layout_typed_values.rs @@ -210,3 +210,71 @@ fn a_null_or_valid_colorbar_legend_or_shape_value_is_read_as_before() { 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"); } + +/// [`laid_out`], with a table and a chart too: their ids. +fn with_data() -> (Session, u64, u64) { + let mut s = laid_out(); + for kind in ["table", "chart"] { + s.run("layout.addElement", &json!({"kind": kind, "layer": "Countries"})).unwrap(); + } + let table = id(&s, |k| matches!(k, ElementKind::Table { .. })); + let chart = id(&s, |k| matches!(k, ElementKind::Chart { .. })); + (s, table, chart) +} + +fn rows(s: &Session) -> usize { + elements(s) + .iter() + .rev() + .find_map(|e| match &e.kind { + ElementKind::Table { rows, .. } => Some(*rows), + _ => None, + }) + .unwrap() +} + +#[test] +fn a_table_or_chart_layer_field_or_rows_of_the_wrong_type_is_refused() { + let (mut s, table, chart) = with_data(); + for (kind, element, key) in [("table", table, "layer"), ("table", table, "rows"), ("chart", chart, "layer"), ("chart", chart, "field")] + { + // A number is what rows takes: ones that aren't whole or are below 0 stand in for it, and so + // does text. + let bad = + if key == "rows" { vec![json!(2.5), json!(-3), json!("many"), json!(true), json!([]), json!({})] } else { wrong().to_vec() }; + for v in bad { + 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; + refused(&mut s, "layout.update", update, &format!("{key} is ")); + } + } +} + +#[test] +fn a_null_or_valid_table_or_chart_value_is_taken() { + let (mut s, table, chart) = with_data(); + let kept = serde_json::to_value(elements(&s)).unwrap(); + s.run("layout.update", &json!({"element": table, "layer": null, "rows": null})).unwrap(); + s.run("layout.update", &json!({"element": chart, "layer": null, "field": null})).unwrap(); + assert_eq!(serde_json::to_value(elements(&s)).unwrap(), kept); + // A table has no field: one given to it is ignored, as before. + s.run("layout.update", &json!({"element": table, "field": 17})).unwrap(); + assert_eq!(serde_json::to_value(elements(&s)).unwrap(), kept); + s.run("layout.addElement", &json!({"kind": "table", "layer": null, "rows": null})).unwrap(); + assert_eq!(rows(&s), 12); + s.run("layout.addElement", &json!({"kind": "chart", "layer": null, "field": null})).unwrap(); + for (given, taken) in [(json!(5), 5), (json!("7"), 7), (json!(0), 1), (json!(900), 500)] { + s.run("layout.addElement", &json!({"kind": "table", "rows": given})).unwrap(); + assert_eq!(rows(&s), taken, "add {given}"); + let table = id(&s, |k| matches!(k, ElementKind::Table { .. })); + s.run("layout.update", &json!({"element": table, "rows": 3})).unwrap(); + s.run("layout.update", &json!({"element": table, "rows": given})).unwrap(); + assert_eq!(rows(&s), taken, "update {given}"); + } + // A chart keeps its own messages for a name the layer doesn't have. + refused(&mut s, "layout.addElement", json!({"kind": "chart", "field": "nope"}), "no field 'nope'"); + refused(&mut s, "layout.update", json!({"element": chart, "field": "nope"}), "no numeric field 'nope'"); +}