diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index d9612c68cb..f1aa746a56 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -73,7 +73,7 @@ const PDF_BUDGET: usize = if cfg!(target_arch = "wasm32") { 256 << 20 } else { 1 /// Layout files: JSON with this extension. pub const LAYOUT_EXTENSION: &str = "rgislayout"; /// Largest layout file read (64 MiB, pictures included). -const MAX_LAYOUT: u64 = 64 << 20; +pub(crate) const MAX_LAYOUT: u64 = 64 << 20; pub fn commands() -> Vec { vec![ diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index ba63ebfd65..73bc820c98 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -4,6 +4,7 @@ use rustgis_map::{Layer, Map, raster::RasterRenderer}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use std::borrow::Cow; use super::{FORMAT, ModelGraph, ModelNodeKind, ModelStep, Project}; use crate::cmd::exports::Output; @@ -497,8 +498,8 @@ fn command_uses_text_statistics(id: &str, params: &Value) -> bool { // A map page's title, which the page draws over its map frame; `updatePage` doesn't say the // kind of the page it changes, so a title with a token counts. "presentation.mapPage" | "presentation.updatePage" => params.get("title").is_some_and(holds_statistic), - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &STATISTIC_KEYS, text_statistics), + // A layout file, read as each of these commands reads its own (`imported_layout_text`). + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &STATISTIC_KEYS, text_statistics), "presentation.import" => crate::cmd::items::inline_text(params) .ok() .flatten() @@ -594,7 +595,7 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool { 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" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()) + imported_layout_uses(id, 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, @@ -629,9 +630,9 @@ fn command_uses_legend_fit(id: &str, params: &Value) -> bool { || legend_names_param(params) == Ok(Some(false)) || legend_columns_param(params).is_ok_and(|n| n.is_some_and(|n| n > LEGEND_COLUMNS_BEFORE_FIT)) } - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + // A layout file, read as each of these commands reads its own (`imported_layout_text`). "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"fit\"", "\"show_layer_names\"", "\"columns\""], legends_fitting) + imported_layout_uses(id, params, &["\"fit\"", "\"show_layer_names\"", "\"columns\""], legends_fitting) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_fit)), _ => false, @@ -649,7 +650,7 @@ fn command_uses_legend_class_labels(id: &str, params: &Value) -> bool { .filter_map(Value::as_object) .any(|classes| classes.values().any(|t| t.as_str().is_some_and(|t| !t.trim().is_empty()))) }), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"class_labels\""], legend_labels_classes), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"class_labels\""], legend_labels_classes), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_class_labels)), _ => false, } @@ -666,7 +667,7 @@ fn legend_labels_classes(e: &rustgis_map::layout::Element) -> bool { fn command_uses_legend_order(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("layerOrder").and_then(Value::as_array).is_some_and(|names| !names.is_empty()), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"layer_order\""], legend_orders_layers), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"layer_order\""], legend_orders_layers), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_order)), _ => false, } @@ -685,7 +686,7 @@ fn command_uses_legend_column_breaks(id: &str, params: &Value) -> bool { "layout.addElement" | "layout.update" => { params.get("columnBreaks").and_then(Value::as_array).is_some_and(|names| !names.is_empty()) } - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"column_breaks\""], legend_breaks_columns), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"column_breaks\""], legend_breaks_columns), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_column_breaks)), _ => false, } @@ -701,7 +702,7 @@ fn legend_breaks_columns(e: &rustgis_map::layout::Element) -> bool { fn command_uses_legend_one_row(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => matches!(crate::cmd::layout::legend_one_row_param(params), Ok(Some(true))), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"one_row\""], legend_in_one_row), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"one_row\""], legend_in_one_row), // It opens its `text` only, whatever else the step holds. "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_lays_a_legend_in_one_row), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_one_row)), @@ -735,7 +736,7 @@ fn legend_in_one_row(e: &rustgis_map::layout::Element) -> bool { 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(params, &["\"show_counts\""], legend_counts_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"show_counts\""], legend_counts_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_counts)), _ => false, } @@ -753,7 +754,7 @@ fn command_uses_legend_subtitle(id: &str, params: &Value) -> bool { "layout.addElement" | "layout.update" => { 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(params, &["\"subtitle\""], legend_subtitle_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"subtitle\""], legend_subtitle_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_subtitle)), _ => false, } @@ -769,7 +770,7 @@ fn command_uses_extent_indicators(id: &str, params: &Value) -> bool { "layout.update" => params .get("extentIndicator") .is_some_and(|v| v.get("of").is_some_and(|of| !of.is_null()) || v.get("remove") == Some(&Value::Bool(true))), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"more_extents\""], extent_indicators_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"more_extents\""], extent_indicators_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_extent_indicators)), _ => false, } @@ -786,7 +787,7 @@ fn extent_indicators_features(e: &rustgis_map::layout::Element) -> bool { fn command_numbers_extents(id: &str, params: &Value) -> bool { match id { "layout.update" => params.get("extentNumbers") == Some(&Value::Bool(true)), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"extent_numbers\""], extent_numbers_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"extent_numbers\""], extent_numbers_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_numbers_extents), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_numbers_extents)), @@ -814,7 +815,7 @@ fn command_uses_legend_nested_sizes(id: &str, params: &Value) -> bool { "layout.addElement" | "layout.update" => { crate::cmd::layout::legend_sizes_param(params) == Ok(Some(rustgis_map::layout::LegendSizes::Nested)) } - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"sizes\""], legend_nested_sizes_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"sizes\""], legend_nested_sizes_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_nested_sizes)), _ => false, } @@ -848,8 +849,8 @@ fn pictures_with_files(e: &rustgis_map::layout::Element) -> bool { fn command_uses_picture_source(id: &str, params: &Value) -> bool { match id { "layout.update" => crate::cmd::layout::gives_picture(params), - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"source\""], pictures_with_files), + // A layout file, read as each of these commands reads its own (`imported_layout_text`). + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"source\""], pictures_with_files), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_picture_source)), _ => false, } @@ -861,7 +862,7 @@ fn command_uses_picture_source(id: &str, params: &Value) -> 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" | "report.existingTemplate" => imported_layout_uses(params, &["\"border_"], |e| e.kind.has_custom_border()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -873,8 +874,8 @@ fn command_uses_section(id: &str, params: &Value) -> bool { match id { "layout.addElement" => params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("section")), "layout.update" => params.get("section").is_some_and(|v| !v.is_null()), - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"sections\""], |e| e.kind.has_sections()), + // A layout file, read as each of these commands reads its own (`imported_layout_text`). + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"sections\""], |e| e.kind.has_sections()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_section)), _ => false, } @@ -894,8 +895,8 @@ fn command_uses_profile(id: &str, params: &Value) -> bool { PROFILE_ONLY_KEYS.iter().any(|k| params.get(*k).is_some_and(|v| !v.is_null())) || params.get("section").is_some_and(Value::is_string) } - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"Profile\""], |e| e.kind.is_profile()), + // A layout file, read as each of these commands reads its own (`imported_layout_text`). + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"Profile\""], |e| e.kind.is_profile()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_profile)), _ => false, } @@ -908,7 +909,7 @@ 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" | "report.existingTemplate" => imported_layout(params, &["\"paper\""]).is_some_and(|mut l| { + "layout.import" | "report.existingTemplate" => imported_layout(id, params, &["\"paper\""]).is_some_and(|mut l| { l.clamp(); l.paper.is_some() }), @@ -936,7 +937,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" | "report.existingTemplate" => imported_layout_uses(params, &["\"background\""], |e| e.background.is_some()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -947,7 +948,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" | "report.existingTemplate" => imported_layout_uses(params, &["\"color\""], |e| e.kind.ink().is_some()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -964,7 +965,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" | "report.existingTemplate" => imported_layout_uses(params, &["\"wrap\""], wraps), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -999,7 +1000,7 @@ fn command_uses_text_block(id: &str, params: &Value) -> bool { }) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"valign\"", "\"line_spacing\"", "\"case\""], text_blocks) + imported_layout_uses(id, 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, @@ -1031,7 +1032,7 @@ fn command_uses_chart_data(id: &str, params: &Value) -> bool { || params.get("other").and_then(crate::cmd::bool_value) == Some(true) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data) + imported_layout_uses(id, 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, @@ -1064,6 +1065,7 @@ fn command_uses_chart_look(id: &str, params: &Value) -> bool { } "layout.update" => only_chart(), "layout.import" | "report.existingTemplate" => imported_layout_uses( + id, params, &["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""], charts_look, @@ -1088,7 +1090,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" | "report.existingTemplate" => imported_layout_uses(params, &["\"classes\""], charts_classes), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -1108,7 +1110,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" | "report.existingTemplate" => imported_layout_uses(params, &["\"stack\""], charts_stack), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -1147,6 +1149,7 @@ fn command_uses_scale_bar_details(id: &str, params: &Value) -> bool { || name("fit").and_then(ScaleBarFit::from_name).is_some_and(|v| v != ScaleBarFit::default()) } "layout.import" | "report.existingTemplate" => imported_layout_uses( + id, params, &[ "\"subdivisions\"", @@ -1185,7 +1188,7 @@ fn command_uses_chart_facing(id: &str, params: &Value) -> bool { } "layout.update" => facing(), "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"mirror\"", "\"max\"", "\"hide_labels\""], charts_facing) + imported_layout_uses(id, 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, @@ -1212,7 +1215,7 @@ 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" | "report.existingTemplate" => imported_layout_uses(params, &["\"bar_outline\""], charts_outlined), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, 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, } @@ -1238,7 +1241,7 @@ fn command_uses_table_look(id: &str, params: &Value) -> bool { } "layout.update" => given("fields") || given("header"), "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"fields\"", "\"text_size\"", "\"header\""], tables_with_a_look) + imported_layout_uses(id, params, &["\"fields\"", "\"text_size\"", "\"header\""], tables_with_a_look) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_table_look)), _ => false, @@ -1262,7 +1265,7 @@ fn command_uses_table_rows(id: &str, params: &Value) -> bool { } "layout.update" => given("sort") || given("descending") || given("selectedOnly"), "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"sort\"", "\"descending\"", "\"selected_only\""], tables_with_rows) + imported_layout_uses(id, params, &["\"sort\"", "\"descending\"", "\"selected_only\""], tables_with_rows) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_table_rows)), _ => false, @@ -1287,7 +1290,7 @@ fn command_uses_colorbar_labels(id: &str, params: &Value) -> bool { .is_some_and(|v| !v.is_null() && !v.as_str().is_some_and(|t| t.trim().eq_ignore_ascii_case("auto")))) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"text_size\"", "\"decimals\""], colorbars_with_labels) + imported_layout_uses(id, params, &["\"text_size\"", "\"decimals\""], colorbars_with_labels) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_colorbar_labels)), _ => false, @@ -1304,7 +1307,7 @@ fn north_arrows_with_font(e: &rustgis_map::layout::Element) -> bool { fn command_uses_north_arrow_font(id: &str, params: &Value) -> bool { match id { "layout.addElement" => adds_with_font(params, "northArrow"), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"font"], north_arrows_with_font), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"font"], north_arrows_with_font), // It opens its `text` only, whatever else the step holds. "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(|t| text_gives_font(t, "layout-north-arrow-font")), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_north_arrow_font)), @@ -1328,7 +1331,7 @@ fn scale_bars_with_font(e: &rustgis_map::layout::Element) -> bool { fn command_uses_scale_bar_font(id: &str, params: &Value) -> bool { match id { "layout.addElement" => adds_with_font(params, "scaleBar"), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"font"], scale_bars_with_font), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"font"], scale_bars_with_font), "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(|t| text_gives_font(t, "layout-scale-bar-font")), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_scale_bar_font)), _ => false, @@ -1350,22 +1353,33 @@ fn adds_with_font(params: &Value, kind: &str) -> bool { /// 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)) -} - -/// The layout a command carries inline, read only when its text names one of `keys` (or escapes, -/// which could hide one). `data` (base64) goes before `text`, as `report.existingTemplate` reads -/// them; one it refuses is no layout. `data` is decoded for each check, rather than keeping it: -/// only a step that carries it pays, and only on a save. -fn imported_layout(params: &Value, keys: &[&str]) -> Option { - crate::cmd::items::inline_text(params) - .ok() - .flatten() +fn imported_layout_uses(id: &str, params: &Value, keys: &[&str], uses: fn(&rustgis_map::layout::Element) -> bool) -> bool { + imported_layout(id, params, keys).is_some_and(|l| l.elements.iter().any(uses)) +} + +/// The layout the command `id` carries inline, read only when its text names one of `keys` (or +/// escapes, which could hide one). One the command refuses is no layout. +fn imported_layout(id: &str, params: &Value, keys: &[&str]) -> Option { + imported_layout_text(id, params) + // Both commands refuse a larger layout file, so it needs nothing. + .filter(|t| t.len() as u64 <= crate::cmd::layout::MAX_LAYOUT) .filter(|t| keys.iter().any(|key| t.contains(key)) || may_hide_a_key(t)) .and_then(|t| serde_json::from_str::(&t).ok()) } +/// The layout text the command `id` imports, read as that command reads it (#3496). `data` is +/// decoded for each check, rather than keeping it: only a step that carries it pays, and only on +/// a save. +fn imported_layout_text<'a>(id: &str, params: &'a Value) -> Option> { + match id { + // Import Layout reads `text` only, never `data`; a `path` names a file this doesn't open. + "layout.import" => crate::cmd::ps(params, "text").map(Cow::Borrowed), + // Existing Template adds a layout it is given by name, reading no file. + "report.existingTemplate" if crate::cmd::ps(params, "layout").is_some() => None, + _ => crate::cmd::items::inline_text(params).ok().flatten(), + } +} + /// Require only executable payloads being saved: graph files omit steps, web tools omit graphs. pub(crate) fn model_capabilities(steps: &[ModelStep], graph: Option<&ModelGraph>) -> Vec<&'static str> { COMMAND_CAPABILITIES.iter().filter_map(|&(name, uses)| model_uses(steps, graph, uses).then_some(name)).collect() diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 1dacd58ac3..eefb664cde 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -172,6 +172,9 @@ mod layout_legend_row; #[path = "tests/layout_legend_subtitle.rs"] mod layout_legend_subtitle; +#[path = "tests/layout_import_reads.rs"] +mod layout_import_reads; + #[path = "tests/layout_legend_sizes.rs"] mod layout_legend_sizes; diff --git a/crates/engine/src/tests/layout_import_reads.rs b/crates/engine/src/tests/layout_import_reads.rs new file mode 100644 index 0000000000..d3963dbf04 --- /dev/null +++ b/crates/engine/src/tests/layout_import_reads.rs @@ -0,0 +1,61 @@ +//! A saved step that imports a layout requires what the layout its command reads uses (#3496). + +use super::*; +use base64::Engine; + +/// A world layout as a layout file, without and with the legend's column breaks. +fn layouts() -> (String, String) { + let mut s = world(); + s.run("layout.new", &json!({"template": "map"})).unwrap(); + let legend = s.project.layouts[0].elements.iter().find(|e| e.kind.code() == "legend").unwrap().id; + let plain = serde_json::to_string(&s.project.layouts[0]).unwrap(); + s.run("layout.update", &json!({"element": legend, "columnBreaks": ["Rivers"]})).unwrap(); + (plain, serde_json::to_string(&s.project.layouts[0]).unwrap()) +} + +fn lists(p: &Project) -> bool { + let v = serde_json::to_value(p).unwrap(); + v["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-legend-column-breaks"))) +} + +#[test] +fn a_saved_import_step_requires_what_its_command_reads() { + let (plain, broken) = layouts(); + let base64 = |t: &str| base64::engine::general_purpose::STANDARD.encode(t); + let line = |params: &Value| json!({"script": format!("layout.import {params}")}); + let ignored_data = json!({"data": base64(&plain), "text": broken}); + // Over the 64 MiB a layout file may take: both commands refuse it. + let too_large = format!("{broken}{}", " ".repeat(64 << 20)); + let mut wrong = Vec::new(); + for (row, (id, params, required)) in [ + ("layout.import", json!({"text": broken}), true), + // Import Layout never reads `data`. + ("layout.import", ignored_data.clone(), true), + ("layout.import", json!({"data": "not base64", "text": broken}), true), + ("layout.import", json!({"data": base64(&broken), "text": plain}), false), + ("layout.import", json!({"data": base64(&broken)}), false), + // Existing Template reads a named layout over any file, else `data` before `text`. + ("report.existingTemplate", json!({"data": base64(&broken), "text": plain}), true), + ("report.existingTemplate", json!({"data": base64(&plain), "text": broken}), false), + ("report.existingTemplate", json!({"layout": "Layout", "text": broken}), false), + ("script.run", line(&ignored_data), true), + ("layout.import", json!({"text": too_large}), false), + ("report.existingTemplate", json!({"text": too_large}), false), + ] + .into_iter() + .enumerate() + { + let mut task = Project::default(); + task.tasks.push( + serde_json::from_value(json!({"name": "t", "title": "t", "steps": [{"title": "s", "command": id, "params": params}]})).unwrap(), + ); + let mut model = Project::default(); + model.models.push(serde_json::from_value(json!({"name": "m", "steps": [{"tool": id, "params": params}]})).unwrap()); + for (held, p) in [("task", &task), ("model", &model)] { + if lists(p) != required { + wrong.push(format!("row {row}: {held} step {id}")); + } + } + } + assert_eq!(wrong, Vec::::new()); +} diff --git a/docs/maintaining.md b/docs/maintaining.md index e8c7f32108..2690209f5d 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -176,7 +176,9 @@ 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`. +`report.existingTemplate` step that passes it as `text` or as base64 `data`. Each step is read as +its command reads it: `layout.import` reads `text`, never `data`; `report.existingTemplate` reads +`data` before `text`, and neither when `layout` names a layout. 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