From 39d0db0004095840b9919df040f6981ae5f0c60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 06:39:10 -0500 Subject: [PATCH 1/3] fix(engine): read nothing an inline project names when asking what it requires (#3530) --- crates/engine/src/project_capabilities.rs | 105 ++++++++++++++++++++-- 1 file changed, 99 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 5e08273621..f00e8429e4 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -341,9 +341,9 @@ fn text_generalizes(text: &str) -> bool { let hidden = ["generalize", "\\", "\"data\"", "\"notebooks\""].iter().any(|k| text.contains(k)); let Some(v) = hidden.then(|| serde_json::from_str::(text).ok()).flatten() else { return false }; if v.get("maps").is_some() - && let Ok(project) = Project::from_json(text) + && let Some(requires) = inline_project_requires(text, "shaded-relief-generalize") { - return project.required_capabilities().contains(&"shaded-relief-generalize"); + return requires; } holds_generalize(&v) } @@ -712,10 +712,16 @@ fn command_uses_legend_one_row(id: &str, params: &Value) -> bool { /// Whether a project's saved text requires `layout-legend-one-row`, as one saved would: its /// layouts', its steps' and 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`]). -/// Read without repair, so asking reads nothing the text names. fn text_lays_a_legend_in_one_row(text: &str) -> bool { (["one_row", "oneRow"].iter().any(|key| text.contains(key)) || may_hide_a_key(text) || names_data(text)) - && Project::from_peer_raw(text).is_ok_and(|p| p.required_capabilities().contains(&"layout-legend-one-row")) + && inline_project_requires(text, "layout-legend-one-row") == Some(true) +} + +/// Whether a project given as text requires `capability`, as one saved would; `None` for text +/// that isn't a project. Read without repair, so asking reads nothing the text names (#3530), +/// and what loading it would cut still counts. +fn inline_project_requires(text: &str, capability: &str) -> Option { + Project::from_peer_raw(text).ok().map(|p| p.required_capabilities().contains(&capability)) } /// Whether a layout element is a legend in one row. @@ -793,7 +799,7 @@ fn command_numbers_extents(id: &str, params: &Value) -> bool { /// is written, may hide it ([`may_hide_a_key`]), or holds a step's base64 `data` ([`names_data`]). fn text_numbers_extents(text: &str) -> bool { (["extent_numbers", "extentNumbers"].iter().any(|key| text.contains(key)) || may_hide_a_key(text) || names_data(text)) - && Project::from_json(text).is_ok_and(|p| p.required_capabilities().contains(&"layout-extent-numbers")) + && inline_project_requires(text, "layout-extent-numbers") == Some(true) } /// Whether a layout element is a map frame that numbers its extent indicators. @@ -1311,7 +1317,7 @@ fn command_uses_north_arrow_font(id: &str, params: &Value) -> bool { /// hide the key ([`may_hide_a_key`]) or holds a step's base64 `data` ([`names_data`]). fn text_gives_font(text: &str, capability: &str) -> bool { (text.contains("font") || may_hide_a_key(text) || names_data(text)) - && Project::from_json(text).is_ok_and(|p| p.required_capabilities().contains(&capability)) + && inline_project_requires(text, capability) == Some(true) } fn scale_bars_with_font(e: &rustgis_map::layout::Element) -> bool { @@ -2030,6 +2036,93 @@ mod tests { } } + /// 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 { + let base = if wanted && capability == "shaded-relief-generalize" { + Project::from_json(include_str!("../../../schema/fixtures/shaded_relief_generalize.rgis")).unwrap() + } else { + Project::default() + }; + let mut s = crate::Session::new(base); + s.project.name = r"font generalize extent_numbers one_row \u0041".to_string(); + s.map_mut().add_raster("Scene".to_string(), rustgis_map::raster::RasterLayer::unread_in_place(scene.to_string())); + s.run("layout.new", &json!({"template": "map"})).unwrap(); + let element = |s: &crate::Session, code: &str| s.project.layouts[0].elements.iter().find(|e| e.kind.code() == code).unwrap().id; + match capability { + "layout-extent-numbers" if wanted => { + let frame = element(&s, "mapFrame"); + s.run("layout.update", &json!({"element": frame, "extentNumbers": true})).unwrap(); + } + "layout-north-arrow-font" if wanted => { + s.run("layout.addElement", &json!({"kind": "northArrow", "font": "serif"})).unwrap(); + } + "layout-scale-bar-font" if wanted => { + s.run("layout.addElement", &json!({"kind": "scaleBar", "font": "serif"})).unwrap(); + } + _ => {} + } + s.project + } + + /// Asking what a step that opens an inline project needs reads nothing that project names, + /// 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); 4] = [ + ("shaded-relief-generalize", command_sets_generalize), + ("layout-extent-numbers", command_numbers_extents), + ("layout-north-arrow-font", command_uses_north_arrow_font), + ("layout-scale-bar-font", command_uses_scale_bar_font), + ]; + let mut listeners = Vec::new(); + for (capability, check) in checks { + for wanted in [true, false] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let scene = format!("http://{}/{capability}-{wanted}/scene.tif", listener.local_addr().unwrap()); + let project = inline_project_with(capability, wanted, &scene); + let saved = project.clone().saved_text(None).unwrap().0; + let mut headerless: Value = serde_json::from_str(&saved).unwrap(); + headerless.as_object_mut().unwrap().remove("requires"); + let mut outer = serde_json::to_value(Project::default()).unwrap(); + outer["tasks"] = json!([{"name": "Open", "steps": [{"title": "Open", "command": "project.openText", "params": {"text": saved}}]}]); + crate::render::rustgis_tiles::cache::allow_named(&scene); + crate::Session::new(project).wait_in_place_reads(); + while listener.accept().is_ok() {} + for text in [saved.clone(), headerless.to_string(), outer.to_string()] { + assert_eq!(check("project.openText", &json!({"text": text})), wanted, "{capability}"); + let script = format!("project.openText {}", json!({"text": text})); + assert_eq!(check("script.run", &json!({"script": script})), wanted, "{capability} in a script"); + } + assert!(!check("project.openText", &json!({"text": "{\"maps\": \"font generalize extent_numbers\""}))); + listeners.push((capability, wanted, listener)); + } + } + for _ in 0..20 { + for (capability, wanted, listener) in &listeners { + let nothing = listener.accept().map(|_| ()).unwrap_err().kind(); + assert_eq!(nothing, std::io::ErrorKind::WouldBlock, "{capability} {wanted}: the raster was read"); + } + std::thread::sleep(std::time::Duration::from_millis(50)); // test-wait: a read that must not happen signals nothing, so a stray one gets time to arrive + } + } + + /// An inline project is asked as written, so what loading it would cut still counts: a layout + /// past the most a project keeps (#3530). + #[test] + fn an_inline_project_requires_what_loading_it_would_cut() { + let project = inline_project_with("layout-north-arrow-font", true, "scene.tif"); + let mut text: Value = serde_json::from_str(&project.clone().saved_text(None).unwrap().0).unwrap(); + let with_font = text["layouts"][0].clone(); + let mut plain = with_font.clone(); + plain["elements"] = json!([]); + let mut layouts = vec![plain; crate::project::MAX_LAYOUTS]; + layouts.push(with_font); + text["layouts"] = Value::Array(layouts); + assert!(command_uses_north_arrow_font("project.openText", &json!({"text": text.to_string()}))); + } + /// A coordinate system saved with its WKT requires `crs-wkt` only when its PROJ definition, /// which an older release reads, doesn't place points as the WKT does: one in grads (NTF /// (Paris), EPSG:4807 as GDAL writes it) does; the fixture's, whose definition does, doesn't, From 3b6135d1a67b5c9df2e7fe8e739dac44ecbb6fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 07:05:27 -0500 Subject: [PATCH 2/3] docs(engine): say what an inline project is asked for, and format --- crates/engine/src/project_capabilities.rs | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index f00e8429e4..b0b5168ab9 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -336,7 +336,7 @@ fn command_sets_generalize(id: &str, params: &Value) -> bool { /// Whether saved JSON text holds a generalised Shaded Relief. Read only when the text names /// `generalize`, or could hide it: escapes, base64 `data` of a step, a notebook. A project is read -/// as one and requires what it would saved, its steps' and notebooks' included (#3434 review). +/// as one ([`inline_project_requires`]), its steps' and notebooks' included (#3434 review). fn text_generalizes(text: &str) -> bool { let hidden = ["generalize", "\\", "\"data\"", "\"notebooks\""].iter().any(|k| text.contains(k)); let Some(v) = hidden.then(|| serde_json::from_str::(text).ok()).flatten() else { return false }; @@ -709,16 +709,16 @@ fn command_uses_legend_one_row(id: &str, params: &Value) -> bool { } } -/// Whether a project's saved text requires `layout-legend-one-row`, as one saved would: its -/// layouts', its steps' and its notebooks'. Parsed only when the text names the key, either way it +/// Whether a project's saved text requires `layout-legend-one-row`: 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_lays_a_legend_in_one_row(text: &str) -> bool { (["one_row", "oneRow"].iter().any(|key| text.contains(key)) || may_hide_a_key(text) || names_data(text)) && inline_project_requires(text, "layout-legend-one-row") == Some(true) } -/// Whether a project given as text requires `capability`, as one saved would; `None` for text -/// that isn't a project. Read without repair, so asking reads nothing the text names (#3530), +/// Whether a project given as text requires `capability` for what it holds as written; `None` +/// for text that isn't a project. Read without repair, so asking reads nothing the text names (#3530), /// and what loading it would cut still counts. fn inline_project_requires(text: &str, capability: &str) -> Option { Project::from_peer_raw(text).ok().map(|p| p.required_capabilities().contains(&capability)) @@ -794,8 +794,8 @@ fn command_numbers_extents(id: &str, params: &Value) -> bool { } } -/// Whether a project's saved text requires `layout-extent-numbers`, as one saved would: its -/// frames', its steps' and its notebooks'. Parsed only when the text names the key, either way it +/// Whether a project's saved text requires `layout-extent-numbers`: for its frames, 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_numbers_extents(text: &str) -> bool { (["extent_numbers", "extentNumbers"].iter().any(|key| text.contains(key)) || may_hide_a_key(text) || names_data(text)) @@ -1312,12 +1312,11 @@ fn command_uses_north_arrow_font(id: &str, params: &Value) -> bool { } } -/// Whether a project's saved text requires the font `capability`, as one saved would: its -/// elements', its steps' and its notebooks'. Parsed only when the text may name a font, may +/// Whether a project's saved text requires the font `capability`: for its elements, its steps +/// or its notebooks. Parsed only when the text may name a font, may /// hide the key ([`may_hide_a_key`]) or holds a step's base64 `data` ([`names_data`]). fn text_gives_font(text: &str, capability: &str) -> bool { - (text.contains("font") || may_hide_a_key(text) || names_data(text)) - && inline_project_requires(text, capability) == Some(true) + (text.contains("font") || may_hide_a_key(text) || names_data(text)) && inline_project_requires(text, capability) == Some(true) } fn scale_bars_with_font(e: &rustgis_map::layout::Element) -> bool { @@ -2086,7 +2085,8 @@ mod tests { let mut headerless: Value = serde_json::from_str(&saved).unwrap(); headerless.as_object_mut().unwrap().remove("requires"); let mut outer = serde_json::to_value(Project::default()).unwrap(); - outer["tasks"] = json!([{"name": "Open", "steps": [{"title": "Open", "command": "project.openText", "params": {"text": saved}}]}]); + outer["tasks"] = + json!([{"name": "Open", "steps": [{"title": "Open", "command": "project.openText", "params": {"text": saved}}]}]); crate::render::rustgis_tiles::cache::allow_named(&scene); crate::Session::new(project).wait_in_place_reads(); while listener.accept().is_ok() {} From 69a7597dfcaf64551b76b2d703a190dccea287c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 07:30:34 -0500 Subject: [PATCH 3/3] test(engine): keep the not-read message and show the cut an inline project check counts --- crates/engine/src/project_capabilities.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index b0b5168ab9..ba63ebfd65 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -2030,7 +2030,8 @@ mod tests { while listener.accept().is_ok() {} assert!(command_uses_legend_one_row("project.openText", &json!({"text": text}))); for _ in 0..20 { - assert_eq!(listener.accept().map(|_| ()).unwrap_err().kind(), std::io::ErrorKind::WouldBlock, "the raster was read"); + let nothing = listener.accept().err().map(|e| e.kind()); + assert_eq!(nothing, Some(std::io::ErrorKind::WouldBlock), "the raster was read"); std::thread::sleep(std::time::Duration::from_millis(50)); // test-wait: a read that must not happen signals nothing, so a stray one gets time to arrive } } @@ -2101,8 +2102,8 @@ mod tests { } for _ in 0..20 { for (capability, wanted, listener) in &listeners { - let nothing = listener.accept().map(|_| ()).unwrap_err().kind(); - assert_eq!(nothing, std::io::ErrorKind::WouldBlock, "{capability} {wanted}: the raster was read"); + let nothing = listener.accept().err().map(|e| e.kind()); + assert_eq!(nothing, Some(std::io::ErrorKind::WouldBlock), "{capability} {wanted}: the raster was read"); } std::thread::sleep(std::time::Duration::from_millis(50)); // test-wait: a read that must not happen signals nothing, so a stray one gets time to arrive } @@ -2120,6 +2121,7 @@ mod tests { let mut layouts = vec![plain; crate::project::MAX_LAYOUTS]; layouts.push(with_font); text["layouts"] = Value::Array(layouts); + assert_eq!(Project::from_json(&text.to_string()).unwrap().layouts.len(), crate::project::MAX_LAYOUTS, "loading cuts it"); assert!(command_uses_north_arrow_font("project.openText", &json!({"text": text.to_string()}))); }