Repository navigation
fix(engine): project.openText and project.fromTemplate steps require layout-legend-counts and layout-legend-subtitle #3611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 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, | ||
| } | ||
|
|
@@ -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), | ||
|
|
||
There was a problem hiding this comment.
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_datais true for any text with a quoteddatakey, andmay_hide_a_keyis true for any\uXXXXescape. When either holds, everyproject.openTextandproject.fromTemplatestep now gets one more fullProject::from_peer_rawparse 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