diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 201f64f145..27ab800dea 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -399,7 +399,9 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool { "layout.update" => { params.get("textSize").is_some_and(|v| !v.is_null()) || params.get("grid").is_some_and(|g| sized(g, "labelSize", GRID_LABEL_PT)) } - "layout.import" => imported_layout_uses(params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()), + "layout.import" | "report.existingTemplate" => { + imported_layout_uses(params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()) + } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_size)), _ => false, } @@ -421,8 +423,8 @@ fn legends_fitting(e: &rustgis_map::layout::Element) -> bool { /// fewer and `null` keep a legend as an older reader draws it; a scale bar's fit names aren't a /// legend's, and a value the command refuses changes nothing, so neither needs it. A key given to /// another kind counts too, though its command refuses it: an over-count that asks for the -/// capability sooner, never a missing one. A `layout.import` requires it for such a legend once -/// its layout is read into range, so a fit or layer-names value that isn't one, read as the +/// capability sooner, never a missing one. An imported layout file requires it for such a legend +/// once its layout is read into range, so a fit or layer-names value that isn't one, read as the /// default, needs nothing. fn command_uses_legend_fit(id: &str, params: &Value) -> bool { use crate::cmd::layout::{legend_columns_param, legend_fit_param, legend_names_param}; @@ -470,7 +472,7 @@ fn legend_labels_classes(e: &rustgis_map::layout::Element) -> bool { fn command_uses_frame_border(id: &str, params: &Value) -> bool { match id { "layout.update" => params.get("border").is_some_and(|b| !b.is_null()), - "layout.import" => imported_layout_uses(params, &["\"border_"], |e| e.kind.has_custom_border()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"border_"], |e| e.kind.has_custom_border()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_frame_border)), _ => false, } @@ -490,13 +492,13 @@ fn command_uses_section(id: &str, params: &Value) -> bool { } /// A `layout.page` command that gives the page a colour other than white (#2855), read as the -/// command reads it: an older reader ignores `color` and opens the page white. A `layout.import` -/// whose layout has one needs it too. +/// command reads it: an older reader ignores `color` and opens the page white. An imported layout +/// file that has one needs it too. fn command_colours_the_page(id: &str, params: &Value) -> bool { match id { "layout.page" => crate::cmd::opaque_color(params, "color") .is_ok_and(|c| c.flatten().is_some_and(|c| c != rustgis_map::Rgba::rgb(0xFF, 0xFF, 0xFF))), - "layout.import" => imported_layout(params, &["\"paper\""]).is_some_and(|mut l| { + "layout.import" | "report.existingTemplate" => imported_layout(params, &["\"paper\""]).is_some_and(|mut l| { l.clamp(); l.paper.is_some() }), @@ -524,7 +526,7 @@ fn command_uses_blank_paper(id: &str, params: &Value) -> bool { fn command_uses_element_background(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("background").is_some_and(|b| !b.is_null()), - "layout.import" => imported_layout_uses(params, &["\"background\""], |e| e.background.is_some()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"background\""], |e| e.background.is_some()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_background)), _ => false, } @@ -535,7 +537,7 @@ fn command_uses_element_background(id: &str, params: &Value) -> bool { /// and `layout.update`, so such a command fails there instead of losing the ink. fn command_uses_element_ink(id: &str, params: &Value) -> bool { match id { - "layout.import" => imported_layout_uses(params, &["\"color\""], |e| e.kind.ink().is_some()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"color\""], |e| e.kind.ink().is_some()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_ink)), _ => false, } @@ -552,7 +554,7 @@ fn wraps(e: &rustgis_map::layout::Element) -> bool { fn command_uses_element_wrap(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("wrap").and_then(crate::cmd::bool_value) == Some(true), - "layout.import" => imported_layout_uses(params, &["\"wrap\""], wraps), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"wrap\""], wraps), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_wrap)), _ => false, } @@ -572,9 +574,9 @@ fn text_blocks(e: &rustgis_map::layout::Element) -> bool { /// in any case) and `null`, which leaves a text as it is, keep a text as an older reader draws it. /// A value the command refuses on a text needs nothing either: the command changes nothing. The /// keys are read here alone, so a block key given to another kind counts too, though its command -/// refuses it: an over-count that asks for the capability sooner, never a missing one. A -/// `layout.import` requires it for such a text once its layout is read into range, so a name it -/// doesn't know or a spacing that isn't a number, read as the default, needs nothing. +/// refuses it: an over-count that asks for the capability sooner, never a missing one. An +/// imported layout file requires it for such a text once its layout is read into range, so a name +/// it doesn't know or a spacing that isn't a number, read as the default, needs nothing. fn command_uses_text_block(id: &str, params: &Value) -> bool { use rustgis_map::layout::{LINE_SPACING, TextCase, TextVAlign}; match id { @@ -586,7 +588,9 @@ fn command_uses_text_block(id: &str, params: &Value) -> bool { crate::cmd::layout::text_block_settings(&one, usual).is_ok_and(|set| set != usual) }) } - "layout.import" => imported_layout_uses(params, &["\"valign\"", "\"line_spacing\"", "\"case\""], text_blocks), + "layout.import" | "report.existingTemplate" => { + imported_layout_uses(params, &["\"valign\"", "\"line_spacing\"", "\"case\""], text_blocks) + } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_block)), _ => false, } @@ -616,7 +620,9 @@ fn command_uses_chart_data(id: &str, params: &Value) -> bool { || params.get("bars").and_then(Value::as_u64).is_some_and(|n| n != CHART_BARS as u64) || params.get("other").and_then(crate::cmd::bool_value) == Some(true) } - "layout.import" => imported_layout_uses(params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data), + "layout.import" | "report.existingTemplate" => { + imported_layout_uses(params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data) + } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_data)), _ => false, } @@ -647,7 +653,7 @@ fn command_uses_chart_look(id: &str, params: &Value) -> bool { && (only_chart() || text("orientation").as_deref() == Some("vertical") || params.get("title").is_some_and(|t| !t.is_null())) } "layout.update" => only_chart(), - "layout.import" => imported_layout_uses( + "layout.import" | "report.existingTemplate" => imported_layout_uses( params, &["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""], charts_look, @@ -672,7 +678,7 @@ fn command_uses_chart_classes(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && classes() } "layout.update" => classes(), - "layout.import" => imported_layout_uses(params, &["\"classes\""], charts_classes), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"classes\""], charts_classes), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_classes)), _ => false, } @@ -692,7 +698,7 @@ fn command_uses_chart_stack(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && stack() } "layout.update" => stack(), - "layout.import" => imported_layout_uses(params, &["\"stack\""], charts_stack), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"stack\""], charts_stack), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_stack)), _ => false, } @@ -730,7 +736,7 @@ fn command_uses_scale_bar_details(id: &str, params: &Value) -> bool { || fixed_decimals(id) || name("fit").and_then(ScaleBarFit::from_name).is_some_and(|v| v != ScaleBarFit::default()) } - "layout.import" => imported_layout_uses( + "layout.import" | "report.existingTemplate" => imported_layout_uses( params, &[ "\"subdivisions\"", @@ -768,7 +774,9 @@ fn command_uses_chart_facing(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && facing() } "layout.update" => facing(), - "layout.import" => imported_layout_uses(params, &["\"mirror\"", "\"max\"", "\"hide_labels\""], charts_facing), + "layout.import" | "report.existingTemplate" => { + imported_layout_uses(params, &["\"mirror\"", "\"max\"", "\"hide_labels\""], charts_facing) + } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_facing)), _ => false, } @@ -794,18 +802,18 @@ fn command_uses_chart_outline(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && outlined() } "layout.update" => outlined(), - "layout.import" => imported_layout_uses(params, &["\"bar_outline\""], charts_outlined), + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"bar_outline\""], charts_outlined), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_outline)), _ => false, } } -/// Whether a command carries its layout inline with an element `uses`: an older -/// reader reads it without the keys it doesn't know. One read from a `"path"` is a layout file, -/// whose own header requires what it uses (#2277). Every save asks, so a text with none of the -/// feature's `keys` (a saved layout writes them only when set) isn't read through, unless it has a -/// backslash: JSON may spell a key with escapes (`"\u0063olor"`). It is read once, whatever its -/// keys. +/// Whether a command carries its layout inline with an element `uses`, as `layout.import` and +/// `report.existingTemplate` do: an older reader reads it without the keys it doesn't know. One +/// read from a `"path"` is a layout file, whose own header requires what it uses (#2277). Every +/// save asks, so a text with none of the feature's `keys` (a saved layout writes them only when +/// set) isn't read through, unless it has a backslash: JSON may spell a key with escapes +/// (`"\u0063olor"`). It is read once, whatever its keys. fn imported_layout_uses(params: &Value, keys: &[&str], uses: fn(&rustgis_map::layout::Element) -> bool) -> bool { imported_layout(params, keys).is_some_and(|l| l.elements.iter().any(uses)) } @@ -1623,6 +1631,59 @@ mod tests { assert!(missing.is_empty(), "add a sample for each command capability, missing: {missing:?}"); } + /// `report.existingTemplate` imports a layout file as `layout.import` does, so a step of it + /// requires what a `layout.import` of the same file does (#3226), by `text` or by `data`. Every + /// `layout_*` fixture is walked, so a capability added with one and read for `layout.import` + /// alone fails here. + #[test] + fn a_report_template_requires_what_layout_import_does() { + use base64::Engine; + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../schema/fixtures"); + let needs = |id: &str, params: Value| -> Vec<&str> { + let step = [ModelStep { tool: id.into(), params }]; + model_capabilities(&step, None) + }; + let mut imported = std::collections::BTreeSet::new(); + for entry in std::fs::read_dir(&dir).unwrap() { + let path = entry.unwrap().path(); + let name = path.file_name().unwrap().to_string_lossy().into_owned(); + if !name.starts_with("layout_") || !name.ends_with(".rgis") { + continue; + } + let project = Project::from_json(&std::fs::read_to_string(&path).unwrap()).unwrap(); + for layout in serde_json::to_value(&project).unwrap()["layouts"].as_array().unwrap() { + let text = layout.to_string(); + let data = base64::engine::general_purpose::STANDARD.encode(&text); + let required = needs("layout.import", json!({"text": text})); + assert_eq!(needs("report.existingTemplate", json!({"text": text})), required, "{name} as text"); + assert_eq!(needs("report.existingTemplate", json!({"data": data})), required, "{name} as data"); + imported.extend(required); + } + } + // The fixtures reach each capability an imported layout asks for. + for capability in [ + "layout-element-background", + "layout-element-ink", + "layout-element-wrap", + "layout-text-block", + "layout-chart-data", + "layout-chart-look", + "layout-chart-classes", + "layout-chart-stack", + "layout-chart-facing", + "layout-chart-outline", + "layout-frame-border", + "layout-text-size", + "layout-scale-bar-details", + "layout-page-color", + ] { + assert!(imported.contains(capability), "no layout fixture asks for {capability}"); + } + // A layout named, or read from a file with its own header (#2277), needs nothing. + assert!(needs("report.existingTemplate", json!({"layout": "Layout"})).is_empty()); + assert!(needs("report.existingTemplate", json!({"path": "regions.rgislayout"})).is_empty()); + } + /// An inline graph is read as the run reads it: only an object with `nodes`, only nodes of kind /// `tool` with an object for `params`; one it can't read it drops, not the rest. #[test] diff --git a/docs/maintaining.md b/docs/maintaining.md index f8e490dde3..0c54a7d534 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -154,6 +154,10 @@ Every capability a saved command can need, except `table-export-order` (no tool script line, notebook cell or history entry: each of the graph's tool nodes needs what the same tool as a step needs. +`report.existingTemplate` imports a layout file as `layout.import` does. Wherever this section says +a saved `layout.import` whose `text` has something requires a capability, so does a saved +`report.existingTemplate` step that passes it as `text` or as base64 `data`. + Projects with a background on a layout element, or a saved task step, script, notebook command cell or history entry that gives one (`layout.addElement` or `layout.update` with a `background` other than `null`), require `layout-element-background`, so an older reader @@ -347,7 +351,7 @@ saved presentation with one in a map page's title requires it too, and so does a `presentation.mapPage` or `presentation.updatePage` whose `title` holds one, or a saved `presentation.import` whose `text` has such a map page (a blank page's text is drawn with no map frame, so no figure fills in there). Other braces (`{scale}`, `{foo}`, `{count:}`) need nothing. -Files read by `path` or `data` are not looked into, as for every other layout capability, and a +Files read by `path` are not looked into, as for every other layout capability, and a presentation file (`.rgispres`) has no header to list a capability in, so an older reader opens one with the token printed as written.