Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 110 additions & 15 deletions crates/engine/src/project_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ 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::<Value>(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)
}
Expand Down Expand Up @@ -709,13 +709,19 @@ 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`]).
/// 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` 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<bool> {
Project::from_peer_raw(text).ok().map(|p| p.required_capabilities().contains(&capability))
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}

/// Whether a layout element is a legend in one row.
Expand Down Expand Up @@ -788,12 +794,12 @@ 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))
&& 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.
Expand Down Expand Up @@ -1306,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))
&& Project::from_json(text).is_ok_and(|p| p.required_capabilities().contains(&capability))
(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 {
Expand Down Expand Up @@ -2025,11 +2030,101 @@ 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
}
}

/// 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().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
}
}

/// 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_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()})));
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
}

/// 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,
Expand Down
Loading