From 7f0d4f4737466586f97681d2c2e694764ea43f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 2 Dec 2026 08:41:21 -0500 Subject: [PATCH] fix(engine): project.openText and project.fromTemplate steps require layout-legend-counts and layout-legend-subtitle (#3598) --- crates/engine/src/project_capabilities.rs | 64 ++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index e817be5fff..04c1fee947 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -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)) + && 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() @@ -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), + "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, } @@ -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() @@ -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 { @@ -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", ¶ms).unwrap(); + } "layout-north-arrow-font" if wanted => { s.run("layout.addElement", &json!({"kind": "northArrow", "font": "serif"})).unwrap(); } @@ -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),