Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions crates/engine/src/cmd/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ 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.
/// The fewest and the most rows a table shows.
const TABLE_ROWS: (f64, f64) = (1.0, 500.0);

/// A table's "rows", when it's given (and not null), held to [`TABLE_ROWS`]: an error when it's not
/// a whole number of 0 or more.
pub(in crate::cmd::layout) fn table_rows(p: &Value) -> Result<Option<usize>, 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(n) if n >= 0.0 && n.fract() == 0.0 => Ok(Some(n.clamp(TABLE_ROWS.0, TABLE_ROWS.1) as usize)),
Some(_) => Err(format!("rows is {what}")),
None => Ok(None),
}
Expand Down Expand Up @@ -223,6 +226,15 @@ pub(in crate::cmd::layout) fn held_width(p: &Value, what: &str, range: (f64, f64
(w != kept).then(|| format!("{what} {w} held to {kept} ({} to {} points)", range.0, range.1))
}

/// A table's "rows" in `p` held to [`TABLE_ROWS`], said as [`held_to_limits`] says it. Only for a
/// table, and after [`table_rows`], which refuses a value that isn't a number of rows.
pub(in crate::cmd::layout) fn held_rows(p: &Value) -> Option<String> {
let (least, most) = TABLE_ROWS;
let n = f(p, "rows")?;
let kept = n.clamp(least, most);
(n != kept).then(|| format!("rows {n} held to {kept} ({least} to {most})"))
}

/// Says what [`held_to_limits`] found, in one notice for the command `title`.
pub(in crate::cmd::layout) fn say_held(s: &mut Session, title: &str, held: Vec<String>) {
if !held.is_empty() {
Expand Down Expand Up @@ -683,7 +695,11 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu
if let Some(said) = empty_colorbar {
s.warn(said);
}
say_held(s, "Add Element", held_to_limits(p, Shape::of_name(kind)));
let mut held = held_to_limits(p, Shape::of_name(kind));
if kind == "table" {
held.extend(held_rows(p));
}
say_held(s, "Add Element", held);
say_name_ignored(s, p, kind);
let mut out = json!({"element": id});
if let Some(w) = warning {
Expand Down
3 changes: 3 additions & 0 deletions crates/engine/src/cmd/layout/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult {
_ => None,
};
let mut said = held_to_limits(p, Shape::of_kind(kind));
if matches!(kind, ElementKind::Table { .. }) {
said.extend(held_rows(p));
}
if let (Some((_, Some(_))), Some(v)) = (&sections, p.get("section")) {
said.extend(held_width(v, "section width", rustgis_map::layout::SECTION_WIDTHS));
}
Expand Down
43 changes: 43 additions & 0 deletions crates/engine/src/tests/layout_typed_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,46 @@ fn a_null_or_valid_table_or_chart_value_is_taken() {
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'");
}

#[test]
fn a_table_s_rows_held_to_their_range_are_said() {
let (mut s, table, chart) = with_data();
let said = |s: &Session, mark: u64| -> Vec<String> {
s.notices_since(mark).iter().filter(|n| n.text.contains("held to")).map(|n| n.text.clone()).collect()
};
let cases = [
(json!(900), 500, Some("rows 900 held to 500 (1 to 500)")),
(json!("900"), 500, Some("rows 900 held to 500 (1 to 500)")),
(json!(501), 500, Some("rows 501 held to 500 (1 to 500)")),
(json!(0), 1, Some("rows 0 held to 1 (1 to 500)")),
(json!(1), 1, None),
(json!(500), 500, None),
(json!("7"), 7, None),
];
for (given, taken, held) in cases {
let mark = s.notice_mark();
s.run("layout.addElement", &json!({"kind": "table", "rows": given})).unwrap();
assert_eq!(rows(&s), taken, "add {given}");
let expected: Vec<String> = held.iter().map(|h| format!("Add Element: {h}")).collect();
assert_eq!(said(&s, mark), expected, "add {given}");
let table = id(&s, |k| matches!(k, ElementKind::Table { .. }));
s.run("layout.update", &json!({"element": table, "rows": 3})).unwrap();
let mark = s.notice_mark();
s.run("layout.update", &json!({"element": table, "rows": given})).unwrap();
assert_eq!(rows(&s), taken, "update {given}");
let expected: Vec<String> = held.iter().map(|h| format!("Element Properties: {h}")).collect();
assert_eq!(said(&s, mark), expected, "update {given}");
}
// With another held value, both are said in the one notice.
let mark = s.notice_mark();
s.run("layout.addElement", &json!({"kind": "table", "rows": 900, "background": {"padding": 999}})).unwrap();
let notices = said(&s, mark);
assert_eq!(notices.len(), 1, "{notices:?}");
assert!(notices[0].contains("background padding 999 held to") && notices[0].contains("rows 900 held to 500"), "{notices:?}");
// Rows not given, or given to an element that has none: nothing to say.
let mark = s.notice_mark();
s.run("layout.addElement", &json!({"kind": "table", "rows": null})).unwrap();
s.run("layout.update", &json!({"element": table, "rows": null})).unwrap();
s.run("layout.update", &json!({"element": chart, "rows": 900})).unwrap();
assert_eq!(said(&s, mark), Vec::<String>::new());
}
Loading