Skip to content
Closed
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
64 changes: 63 additions & 1 deletion crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,22 @@ fn command_uses_legend_counts(id: &str, params: &Value) -> bool {
match id {
"layout.addElement" | "layout.update" => crate::cmd::layout::legend_counts_param(params) == Ok(Some(true)),
"layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"show_counts\""], legend_counts_features),
// It opens its `text` only, whatever else the step holds.
"project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_shows_legend_counts),
"project.fromTemplate" => crate::cmd::items::inline_text(params).ok().flatten().is_some_and(|t| text_shows_legend_counts(&t)),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_counts)),
_ => false,
}
}

/// Whether a project's saved text requires `layout-legend-counts`: for its layouts, its steps
/// or its notebooks. Parsed only when the text names a key, either way it is written, may hide it
/// ([`may_hide_a_key`]), or holds a step's base64 `data` ([`names_data`]).
fn text_shows_legend_counts(text: &str) -> bool {
(["show_counts", "showCounts"].iter().any(|key| text.contains(key)) || may_hide_a_key(text) || names_data(text))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low-confidence performance note, not a correctness problem. names_data is true for any text with a quoted data key, and may_hide_a_key is true for any \uXXXX escape. When either holds, every project.openText and project.fromTemplate step now gets one more full Project::from_peer_raw parse from this check. The subtitle check (text_gives_legend_subtitle) has the same shape, so a text that passes the pre-filter gets two more full parses per step. The siblings already do this, so the cost follows the existing pattern. If large saved projects with embedded data make this slow, parse once per text and share the result across the capability checks, rather than adding a parse per check.

Category: performance

&& inline_project_requires(text, "layout-legend-counts") == Some(true)
}

/// Whether a layout element is a legend that shows feature counts.
fn legend_counts_features(e: &rustgis_map::layout::Element) -> bool {
e.kind.legend_counts_features()
Expand All @@ -812,6 +823,9 @@ fn command_uses_legend_subtitle(id: &str, params: &Value) -> bool {
crate::cmd::layout::legend_subtitle_param(params).is_ok_and(|t| t.is_some_and(|t| !t.is_empty()))
}
"layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"subtitle\""], legend_subtitle_features),
// It opens its `text` only, whatever else the step holds.
"project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_gives_legend_subtitle),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Twin gap in the same family: five legend checks in this file still have no project.openText / project.fromTemplate arm, so an openText step holding a legend with any of these fields passes the older-reader check and the older reader drops the field silently:

  • command_uses_legend_fit (layout-legend-fit, around line 680)
  • command_uses_legend_class_labels (layout-legend-class-labels, around line 701)
  • command_uses_legend_order (layout-legend-order, around line 723)
  • command_uses_legend_column_breaks (layout-legend-column-breaks, around line 740)
  • command_uses_legend_nested_sizes (layout-legend-nested-sizes, around line 922)

This PR fixes counts, subtitle and (already) one-row and patch-size, so the same two arms are missing from those five. This PR doesn't have to fix them, but a follow-up should, with a test like the new a_saved_step_that_counts_or_subtitles_... for each. I checked the dispatch table (COMMAND_CAPABILITIES, lines 163–223) and command_uses: nothing else routes an openText step to those checks.

Category: bug

"project.fromTemplate" => crate::cmd::items::inline_text(params).ok().flatten().is_some_and(|t| text_gives_legend_subtitle(&t)),
"script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_subtitle)),
_ => false,
}
Expand Down Expand Up @@ -921,6 +935,14 @@ fn legend_nested_sizes_features(e: &rustgis_map::layout::Element) -> bool {
e.kind.legend_nested_sizes_features()
}

/// Whether a project's saved text requires `layout-legend-subtitle`: for its layouts, its steps
/// or its notebooks. Parsed only when the text names the key, either way it is written, may hide
/// it ([`may_hide_a_key`]), or holds a step's base64 `data` ([`names_data`]).
fn text_gives_legend_subtitle(text: &str) -> bool {
(text.contains("subtitle") || may_hide_a_key(text) || names_data(text))
&& inline_project_requires(text, "layout-legend-subtitle") == Some(true)
}

/// Whether a layout element is a legend with a subtitle.
fn legend_subtitle_features(e: &rustgis_map::layout::Element) -> bool {
e.kind.legend_subtitle_features()
Expand Down Expand Up @@ -2187,6 +2209,38 @@ mod tests {
assert!(!opens(json!({"element": 1, "subtitle": "a"})));
}

/// A project's saved step that counts a legend's features or gives it a subtitle needs the
/// capability, whichever way it spells the key; one that asks for what it draws doesn't.
#[test]
fn a_saved_step_that_counts_or_subtitles_a_legend_is_found_in_text_a_project_opens() {
let opens = |check: fn(&str, &Value) -> bool, params: Value| {
let mut project = serde_json::to_value(Project::default()).unwrap();
project["tasks"] = json!([{"name": "Legend", "steps": [{"title": "Legend", "command": "layout.update", "params": params}]}]);
check("project.openText", &json!({"text": project.to_string()}))
};
assert!(opens(command_uses_legend_counts, json!({"element": 1, "showCounts": true})));
assert!(!opens(command_uses_legend_counts, json!({"element": 1, "showCounts": false})));
assert!(!opens(command_uses_legend_counts, json!({"element": 1, "subtitle": "a"})));
assert!(opens(command_uses_legend_subtitle, json!({"element": 1, "subtitle": "a"})));
assert!(!opens(command_uses_legend_subtitle, json!({"element": 1, "subtitle": ""})));
assert!(!opens(command_uses_legend_subtitle, json!({"element": 1, "showCounts": true})));
}

/// A key written with a `\u` escape is the same key to a JSON reader, so the checks parse
/// the text whenever it holds one.
#[test]
fn a_legend_key_written_with_an_escape_is_found_in_text_a_project_opens() {
let opens = |check: fn(&str, &Value) -> bool, params: Value, key: &str, escaped: &str| {
let mut project = serde_json::to_value(Project::default()).unwrap();
project["tasks"] = json!([{"name": "Legend", "steps": [{"title": "Legend", "command": "layout.update", "params": params}]}]);
let text = project.to_string().replace(&format!("\"{key}\""), &format!("\"{escaped}\""));
assert!(text.contains(escaped), "{key} was not escaped");
check("project.openText", &json!({"text": text}))
};
assert!(opens(command_uses_legend_counts, json!({"element": 1, "showCounts": true}), "showCounts", r"\u0073howCounts"));
assert!(opens(command_uses_legend_subtitle, json!({"element": 1, "subtitle": "a"}), "subtitle", r"\u0073ubtitle"));
}

/// A session holding what `check` asks for, when `wanted`, beside a raster read in place
/// from `scene`. Its name passes every check's text pre-filter, so each one loads it.
fn inline_project_with(capability: &str, wanted: bool, scene: &str) -> Project {
Expand All @@ -2210,6 +2264,12 @@ mod tests {
let legend = s.project.layouts[0].elements.iter().find(|e| e.kind.code() == "legend").unwrap().id;
s.run("layout.update", &json!({"element": legend, "patchWidth": 20})).unwrap();
}
"layout-legend-counts" | "layout-legend-subtitle" if wanted => {
let legend = element(&s, "legend");
let mut params = if capability == "layout-legend-counts" { json!({"showCounts": true}) } else { json!({"subtitle": "a"}) };
params["element"] = json!(legend);
s.run("layout.update", &params).unwrap();
}
"layout-north-arrow-font" if wanted => {
s.run("layout.addElement", &json!({"kind": "northArrow", "font": "serif"})).unwrap();
}
Expand All @@ -2230,10 +2290,12 @@ mod tests {
/// as saved, without its header, from a script and from a step of a step (#3530).
#[test]
fn the_checks_of_an_inline_project_read_nothing_it_names() {
let checks: [(&str, CommandCheck); 6] = [
let checks: [(&str, CommandCheck); 8] = [
("label-obstacles", command_sets_obstacles),
("shaded-relief-generalize", command_sets_generalize),
("layout-legend-patch-size", command_uses_legend_patch_size),
("layout-legend-counts", command_uses_legend_counts),
("layout-legend-subtitle", command_uses_legend_subtitle),
("layout-extent-numbers", command_numbers_extents),
("layout-north-arrow-font", command_uses_north_arrow_font),
("layout-scale-bar-font", command_uses_scale_bar_font),
Expand Down
Loading