diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index c857c4eb62..2dd654c9cf 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -126,6 +126,7 @@ fn web_link_chart(s: &mut Session, p: &Value) -> CmdResult { } const MANAGE: &str = r#"{"item"?: id|name (default: the last of its kind), "map"?: re-share from another map}"#; +/// Publish shared items and import models, checking file compatibility before creating a model. pub fn commands() -> Vec { let share_as = |id: &'static str, label: &'static str, run: fn(&mut Session, &Value) -> CmdResult, params: &'static str| { C::new(id, label, "Publish › Share As", run).params(params) @@ -158,13 +159,19 @@ pub fn commands() -> Vec { C::new("model.import", "Import Model", "Analyze › Geoprocessing", |s, p| { let text = super::items::read_text(p)?; let v: Value = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS web tool: {e}"))?; + crate::model::check_file(&v)?; let name = v["name"].as_str().unwrap_or("Tool").chars().take(200).collect::(); // A model file (`*.model.json`) holds a graph; a web tool its steps. - if let Some(graph) = v.get("graph") { - return s.run("model.new", &json!({"name": name, "graph": graph, "imported": true})); + let imported = if let Some(graph) = v.get("graph") { + s.run("model.new", &json!({"name": name, "graph": graph, "imported": true}))? + } else { + let steps = v.get("steps").cloned().ok_or("not a RustGIS model or web tool (no graph or steps)")?; + s.run("model.new", &json!({"name": name, "steps": steps, "imported": true}))? + }; + if v.get("name").map_or(Some("Tool"), Value::as_str) != imported.get("model").and_then(Value::as_str) { + s.notify("1 model name was trimmed, shortened, replaced or made unique during import.", false); } - let steps = v.get("steps").cloned().ok_or("not a RustGIS model or web tool (no graph or steps)")?; - s.run("model.new", &json!({"name": name, "steps": steps, "imported": true})) + Ok(imported) }) .params(r#"{"path"?: "*.model.json" | "*.rgistool", "text"?: file JSON, "data"?: base64 file}"#), ] @@ -221,7 +228,7 @@ pub fn share_bytes(s: &mut Session, kind: &str, p: &Value) -> Result<(String, Ve .map(move |(k, v)| json!({"step": i + 1, "tool": st.tool, "name": k, "default": v})) }) .collect(); - let tool = json!({"format": "RustGIS web tool", "version": 1, "name": m.name, "steps": m.steps, "parameters": params}); + let tool = json!({"format": "RustGIS web tool", "version": crate::model::WEB_TOOL_VERSION, "name": m.name, "steps": m.steps, "parameters": params}); Ok((m.name.clone(), serde_json::to_vec_pretty(&tool).map_err(|e| e.to_string())?, m.name.clone())) } "webScene" => { diff --git a/crates/engine/src/cmd/workflows.rs b/crates/engine/src/cmd/workflows.rs index 0dabd83a86..35f1d1f0ff 100644 --- a/crates/engine/src/cmd/workflows.rs +++ b/crates/engine/src/cmd/workflows.rs @@ -493,7 +493,10 @@ fn model_graph(s: &Session, p: &Value) -> Result<(String, crate::project::ModelG Some(n) => s.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(n)).ok_or_else(|| format!("no model '{n}'"))?, None => s.project.models.last().ok_or("the project has no models")?, }; - let g = model.graph.clone().unwrap_or_else(|| crate::model::steps_to_graph(&model.steps)); + let g = match &model.graph { + Some(graph) => graph.clone(), + None => crate::model::checked_steps_to_graph(&model.steps)?, + }; Ok((model.name.clone(), g, s.trusted_models.contains(&model.name.to_ascii_lowercase()))) } diff --git a/crates/engine/src/model.rs b/crates/engine/src/model.rs index bc48297e7a..74676d0268 100644 --- a/crates/engine/src/model.rs +++ b/crates/engine/src/model.rs @@ -10,7 +10,7 @@ use std::collections::{HashMap, HashSet}; use rustgis_analysis::{ParamKind, ToolSpec}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; use crate::Session; @@ -19,6 +19,9 @@ use crate::project::{ModelEdge, ModelGraph, ModelNode, ModelNodeKind, ModelStep} /// The most nodes and edges a graph may have (files and parameters are hostile). pub const MAX_NODES: usize = 2000; pub const MAX_EDGES: usize = 4000; +/// Identifier length and canvas coordinate limits used by graph commands and file checks. +const MAX_NODE_ID: usize = 100; +const MAX_NODE_POSITION: f32 = 1e6; /// The output port of input and tool nodes. pub const OUT: &str = "out"; /// The input port of output nodes. @@ -377,6 +380,64 @@ pub fn steps_to_graph(steps: &[ModelStep]) -> ModelGraph { g } +/// Converts steps within the graph's supported limits, returning an error instead of truncating. +/// Referenced steps must be earlier and have a layer output. +pub fn checked_steps_to_graph(steps: &[ModelStep]) -> Result { + let limit = MAX_NODES / 2; + if steps.len() > limit { + return Err(format!( + "this model has {} steps; graph conversion supports at most {limit}. The original steps have not been changed.", + steps.len() + )); + } + let mut inputs = HashSet::new(); + let mut edges = 0; + for (index, step) in steps.iter().enumerate() { + if !step.params.is_null() && !step.params.is_object() { + return Err(format!("model step {} needs an object for its parameters", index + 1)); + } + let node = ModelNode { tool: Some(step.tool.clone()), kind: ModelNodeKind::Tool, ..Default::default() }; + for port in input_ports(&node) { + let Some(value) = step.params.get(port.id).and_then(Value::as_str) else { continue }; + for part in value.split(|c| port.many && c == ';').map(str::trim) { + if port.many && part.is_empty() { + continue; + } + match part.strip_prefix('%').and_then(|n| n.parse::().ok()) { + Some(n) if !(1..=index).contains(&n) => { + return Err(format!( + "model step {} uses %{n}, which is not an earlier step. The original steps have not been changed.", + index + 1 + )); + } + Some(n) => { + let has_no_output = steps.get(n.saturating_sub(1)).is_some_and(|previous| { + let node = ModelNode { tool: Some(previous.tool.clone()), kind: ModelNodeKind::Tool, ..Default::default() }; + output_ports(&node).is_empty() + }); + if has_no_output { + return Err(format!( + "model step {} uses %{n}, but step {n} has no layer output. The original steps have not been changed.", + index + 1 + )); + } + } + None => { + inputs.insert(part.to_ascii_lowercase()); + } + } + edges += 1; + if steps.len() + inputs.len() > MAX_NODES || edges > MAX_EDGES { + return Err(format!( + "this model exceeds the graph limits of {MAX_NODES} nodes and {MAX_EDGES} connections. The original steps have not been changed." + )); + } + } + } + } + Ok(steps_to_graph(steps)) +} + /// The graph's tools as steps in run order, edges as layer names (from inputs) and `%n` (from /// step n); empty when the graph has a loop. pub fn graph_to_steps(g: &ModelGraph) -> Vec { @@ -414,6 +475,7 @@ pub fn graph_to_steps(g: &ModelGraph) -> Vec { /// Reads a graph from JSON, dropping what can't be used: nodes without an id, repeated ids, /// edges to missing nodes or to themselves. Bounded in size. +/// Repeated connections into many-valued input ports retain their multiplicity. pub fn graph_from_json(v: &Value) -> Result { let nodes = v.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?; if nodes.len() > MAX_NODES { @@ -426,7 +488,7 @@ pub fn graph_from_json(v: &Value) -> Result { let mut g = ModelGraph::default(); for n in nodes { let Ok(mut node) = serde_json::from_value::(n.clone()) else { continue }; - node.id = node.id.trim().chars().take(100).collect(); + node.id = node.id.trim().chars().take(MAX_NODE_ID).collect(); if node.id.is_empty() || g.nodes.iter().any(|m| m.id == node.id) { continue; } @@ -434,8 +496,8 @@ pub fn graph_from_json(v: &Value) -> Result { node.x = 0.0; node.y = 0.0; } - node.x = node.x.clamp(-1e6, 1e6); - node.y = node.y.clamp(-1e6, 1e6); + node.x = node.x.clamp(-MAX_NODE_POSITION, MAX_NODE_POSITION); + node.y = node.y.clamp(-MAX_NODE_POSITION, MAX_NODE_POSITION); if !node.params.is_object() { node.params = Value::Null; } @@ -444,7 +506,9 @@ pub fn graph_from_json(v: &Value) -> Result { for e in edges { let Ok(e) = serde_json::from_value::(e.clone()) else { continue }; let has = |id: &str| g.nodes.iter().any(|n| n.id == id); - if e.from != e.to && has(&e.from) && has(&e.to) && !e.to_port.is_empty() && !g.edges.contains(&e) { + let repeated_input = + g.nodes.iter().find(|n| n.id == e.to).is_some_and(|n| input_ports(n).iter().any(|p| p.id == e.to_port && p.many)); + if e.from != e.to && has(&e.from) && has(&e.to) && !e.to_port.is_empty() && (repeated_input || !g.edges.contains(&e)) { g.edges.push(e); } } @@ -685,8 +749,152 @@ fn rename(s: &mut Session, id: u32, wanted: &str) -> Option { /// The JSON a model file holds (`*.model.json`). pub const SCHEMA: &str = "https://rustgis.app/schemas/model-graph-v1.json"; +/// The model-file version understood by this reader and emitted by this writer. +pub(crate) const FILE_VERSION: &str = "1.0.0"; +/// Web tools store steps under a separate, integer version marker. +pub(crate) const WEB_TOOL_VERSION: u64 = 1; + +/// Keep unknown fields visible at the file boundary without changing the command data types. +#[derive(Deserialize)] +struct ImportItem { + #[serde(flatten)] + known: T, + #[serde(flatten)] + extra: HashMap, +} + +/// Check declared versions and model contents before import can discard data. +/// Missing versions are legacy files. The stricter field checks apply only to file import. +pub(crate) fn check_file(v: &Value) -> Result<(), String> { + let graph = v.get("graph"); + if let Some(version) = v.get("version") { + let supported = if graph.is_some() { version.as_str() == Some(FILE_VERSION) } else { version.as_u64() == Some(WEB_TOOL_VERSION) }; + if !supported { + return Err("this model or web tool has an unsupported version. Update RustGIS to import it.".into()); + } + } + if graph.is_some() && v.get("steps").is_some() { + return Err("a model file cannot contain both a graph and steps. Choose one representation before importing it.".into()); + } + let fields: &[&str] = + if graph.is_some() { &["$schema", "version", "name", "graph"] } else { &["format", "version", "name", "steps", "parameters"] }; + if v.as_object().is_some_and(|file| file.keys().any(|key| !fields.contains(&key.as_str()))) { + return Err("model file has unsupported fields. Update RustGIS to import this model.".into()); + } + if let Some(graph) = graph { + let nodes = graph.get("nodes").and_then(Value::as_array).ok_or("a model graph needs \"nodes\"")?; + if nodes.len() > MAX_NODES { + return Err(format!("a model can have at most {MAX_NODES} nodes")); + } + if graph.as_object().is_some_and(|g| g.keys().any(|k| !matches!(k.as_str(), "nodes" | "edges"))) { + return Err("model graph has unsupported fields. Update RustGIS to import this model.".into()); + } + for (index, node) in nodes.iter().enumerate() { + let node = ImportItem::::deserialize(node) + .map_err(|_| format!("model node {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?; + if !node.extra.is_empty() { + return Err(format!("model node {} has unsupported fields. Update RustGIS to import this model.", index + 1)); + } + if node.known.id.trim() != node.known.id || node.known.id.chars().nth(MAX_NODE_ID).is_some() { + return Err(format!( + "model node {} has an ID that would be trimmed or truncated. Use at most {MAX_NODE_ID} characters without surrounding whitespace.", + index + 1 + )); + } + if !(-MAX_NODE_POSITION..=MAX_NODE_POSITION).contains(&node.known.x) + || !(-MAX_NODE_POSITION..=MAX_NODE_POSITION).contains(&node.known.y) + { + return Err(format!( + "model node {} has a position outside the supported canvas bounds (-{MAX_NODE_POSITION} to {MAX_NODE_POSITION})", + index + 1 + )); + } + if !node.known.params.is_null() && !node.known.params.is_object() { + return Err(format!("model node {} needs an object for its parameters", index + 1)); + } + } + let edges = match graph.get("edges") { + None => &[][..], + Some(edges) => edges.as_array().map(Vec::as_slice).ok_or("a model graph's connections must be an array")?, + }; + if edges.len() > MAX_EDGES { + return Err(format!("a model can have at most {MAX_EDGES} connections")); + } + for (index, edge) in edges.iter().enumerate() { + let edge = ImportItem::::deserialize(edge).map_err(|_| format!("model connection {} is malformed", index + 1))?; + if !edge.extra.is_empty() { + return Err(format!("model connection {} has unsupported fields. Update RustGIS to import this model.", index + 1)); + } + } + let normalized = graph_from_json(graph)?; + let lost_nodes = nodes.len().saturating_sub(normalized.nodes.len()); + let lost_edges = edges.len().saturating_sub(normalized.edges.len()); + if lost_nodes > 0 || lost_edges > 0 { + return Err(format!( + "this model would discard {lost_nodes} node(s) and {lost_edges} connection(s). Fix empty or duplicate node IDs and invalid connections before importing it." + )); + } + let by_id: HashMap<&str, &ModelNode> = normalized.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); + let mut single_inputs = HashSet::new(); + for (index, edge) in normalized.edges.iter().enumerate() { + let from_known = by_id.get(edge.from.as_str()).is_some_and(|n| output_ports(n).iter().any(|p| p.id == edge.from_port)); + let to_port = by_id.get(edge.to.as_str()).and_then(|n| input_ports(n).into_iter().find(|p| p.id == edge.to_port)); + if !from_known || to_port.is_none() { + return Err(format!( + "model connection {} refers to an unsupported port. Update RustGIS or fix the connection before importing this model.", + index + 1 + )); + } + if let Some(input) = by_id.get(edge.from.as_str()).filter(|n| n.kind == ModelNodeKind::Input) { + let layer = input.layer.as_deref().filter(|layer| !layer.trim().is_empty()).ok_or_else(|| { + format!( + "model connection {} has an input without a layer. Choose a layer for this input before importing this model.", + index + 1 + ) + })?; + if by_id.get(edge.to.as_str()).is_some_and(|n| n.kind == ModelNodeKind::Tool) + && (layer.trim() != layer + || to_port.as_ref().is_some_and(|port| port.many && layer.contains(';')) + || layer.strip_prefix('%').and_then(|n| n.parse::().ok()).is_some()) + { + return Err(format!( + "model connection {} has an input layer name that cannot be represented in steps. Rename the layer to avoid surrounding whitespace, numeric %n references, or semicolons on multi-input ports before importing this model.", + index + 1 + )); + } + } + if to_port.is_some_and(|p| !p.many) && !single_inputs.insert((edge.to.as_str(), edge.to_port.as_str())) { + return Err(format!( + "model connection {} duplicates a single-valued input port. Keep one connection to this port before importing this model.", + index + 1 + )); + } + } + if topological_order(&normalized).is_none() { + return Err("this model contains a loop and cannot be converted to steps. Fix the loop before importing it.".into()); + } + } else if let Some(steps) = v.get("steps") { + let steps = steps.as_array().ok_or("model steps must be an array")?; + if steps.len() > MAX_NODES { + return Err(format!("a model can have at most {MAX_NODES} steps")); + } + for (index, step) in steps.iter().enumerate() { + let step = ImportItem::::deserialize(step) + .map_err(|_| format!("model step {} is unsupported or malformed. Update RustGIS to import this model.", index + 1))?; + if !step.extra.is_empty() { + return Err(format!("model step {} has unsupported fields. Update RustGIS to import this model.", index + 1)); + } + if !step.known.params.is_null() && !step.known.params.is_object() { + return Err(format!("model step {} needs an object for its parameters", index + 1)); + } + } + } + Ok(()) +} + +/// A graph's current file envelope; imports also accept legacy envelopes without a version. pub fn to_file(name: &str, g: &ModelGraph) -> Value { - json!({"$schema": SCHEMA, "version": "1.0.0", "name": name, "graph": g}) + json!({"$schema": SCHEMA, "version": FILE_VERSION, "name": name, "graph": g}) } #[cfg(test)] #[allow(clippy::unwrap_used)] diff --git a/crates/engine/tests/workflows.rs b/crates/engine/tests/workflows.rs index 8d71711d1d..8e040efb5f 100644 --- a/crates/engine/tests/workflows.rs +++ b/crates/engine/tests/workflows.rs @@ -9,6 +9,329 @@ use rustgis_engine::{Project, Session}; use serde_json::{Value, json}; use std::path::{Path, PathBuf}; +/// Declared versions are checked before payloads, and unreadable nodes cannot disappear on import. +#[test] +fn model_import_refuses_unsupported_files_without_changing_session() { + use base64::Engine; + let scratch = Scratch::new("model-compatibility"); + let mut s = Session::new(Project::new("Original")); + s.run("model.new", &json!({"name": "Keep", "steps": []})).unwrap(); + s.run("project.rename", &json!({"name": "Redo"})).unwrap(); + s.run("edit.undo", &Value::Null).unwrap(); + s.path = Some(scratch.path("original.rgis")); + s.mark_saved(); + let before = serde_json::to_value(&s.project).unwrap(); + let history = (s.undo_depth(), s.undo_label(), s.redo_label()); + let graph = json!({"nodes": [ + {"id": "i", "kind": "input", "layer": "Points"}, + {"id": "future", "kind": "branch"}, + {"id": "o", "kind": "output", "name": "Result"} + ], "edges": [ + {"from": "i", "fromPort": "out", "to": "future", "toPort": "input"}, + {"from": "future", "fromPort": "out", "to": "o", "toPort": "in"} + ]}); + for (file, message) in [ + (json!({"version": "2.0.0", "graph": "unreadable future graph"}), "unsupported version"), + (json!({"version": 2, "steps": "unreadable future steps"}), "unsupported version"), + (json!({"version": null, "graph": {"nodes": []}}), "unsupported version"), + (json!({"version": "1", "steps": []}), "unsupported version"), + (json!({"graph": {"nodes": []}, "description": "Keep this"}), "file has unsupported fields"), + (json!({"steps": [], "future": true}), "file has unsupported fields"), + (json!({"graph": {"nodes": []}, "steps": [{"tool": "buffer", "params": {}}]}), "both a graph and steps"), + (json!({"steps": [{"tool": "buffer", "params": {}, "when": {"enabled": true}}]}), "step 1 has unsupported fields"), + (json!({"steps": [{"tool": "buffer", "params": ["input"]}]}), "step 1 needs an object"), + (json!({"steps": vec![json!({"tool": "buffer", "params": {}}); 2001]}), "at most 2000 steps"), + (json!({"graph": {"nodes": [], "edges": [], "future": {"enabled": true}}}), "graph has unsupported fields"), + (json!({"version": "1.0.0", "graph": graph}), "node 2"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "tool": {}}]}}), "node 1"), + (json!({"graph": {"nodes": [{"id": "", "kind": "input"}]}}), "discard 1 node(s)"), + (json!({"graph": {"nodes": [{"id": " padded ", "kind": "input"}]}}), "would be trimmed or truncated"), + (json!({"graph": {"nodes": [{"id": "a".repeat(101), "kind": "input"}]}}), "would be trimmed or truncated"), + (json!({"graph": {"nodes": [{"id": "a", "kind": "input", "x": 1000001}]}}), "outside the supported canvas bounds"), + (json!({"graph": {"nodes": [{"id": "a", "kind": "input", "y": 1e300}]}}), "outside the supported canvas bounds"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool"}, {"id": "t", "kind": "output"}]}}), "discard 1 node(s)"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "parms": {"distance": 37}}]}}), "unsupported fields"), + (json!({"graph": {"nodes": [{"id": "t", "kind": "tool", "params": [37]}]}}), "needs an object"), + (json!({"graph": {"nodes": [], "edges": {}}}), "connections must be an array"), + (json!({"graph": {"nodes": [], "edges": ["bad edge"]}}), "connection 1 is malformed"), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "to": "b"}]}}), + "1 connection(s)", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}], + "edges": [{"from": "a", "to": "missing", "toPort": "input"}]}}), + "1 connection(s)", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "to": "b", "toPort": "in"}, {"from": "a", "to": "b", "toPort": "in"}]}}), + "1 connection(s)", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "to": "b", "toPort": "in", "future": true}]}}), + "unsupported fields", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "Points"}, + {"id": "b", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "b", "toPort": "future"}]}}), + "connection 1 refers to an unsupported port", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "fromPort": "out", "to": "b", "toPort": "future"}]}}), + "connection 1 refers to an unsupported port", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "b", "kind": "output"}], + "edges": [{"from": "a", "fromPort": "future", "to": "b", "toPort": "in"}]}}), + "connection 1 refers to an unsupported port", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "A"}, + {"id": "b", "kind": "input", "layer": "B"}, {"id": "t", "kind": "tool", "tool": "buffer"}], "edges": [ + {"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}, + {"from": "b", "fromPort": "out", "to": "t", "toPort": "input"} + ]}}), + "connection 2 duplicates a single-valued input port", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input"}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input without a layer", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": ""}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input without a layer", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "East;West"}, {"id": "t", "kind": "tool", "tool": "merge"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "inputs"}]}}), + "connection 1 has an input layer name that cannot be represented in steps", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": "%1"}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input layer name that cannot be represented in steps", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "input", "layer": " Points "}, {"id": "t", "kind": "tool", "tool": "buffer"}], + "edges": [{"from": "a", "fromPort": "out", "to": "t", "toPort": "input"}]}}), + "connection 1 has an input layer name that cannot be represented in steps", + ), + ( + json!({"graph": {"nodes": [{"id": "a", "kind": "tool", "tool": "buffer"}, + {"id": "b", "kind": "tool", "tool": "buffer"}], "edges": [ + {"from": "a", "fromPort": "out", "to": "b", "toPort": "input"}, + {"from": "b", "fromPort": "out", "to": "a", "toPort": "input"} + ]}}), + "contains a loop", + ), + ] { + let text = file.to_string(); + let path = scratch.path("unsupported.model.json"); + std::fs::write(&path, &text).unwrap(); + for params in + [json!({"text": text}), json!({"data": base64::engine::general_purpose::STANDARD.encode(&text)}), json!({"path": path})] + { + let error = s.run("model.import", ¶ms).unwrap_err(); + assert!(error.contains(message), "{error}"); + if message == "unsupported version" || message.starts_with("node ") || message.contains("unsupported fields") { + assert!(error.contains("Update RustGIS"), "{error}"); + } + assert_eq!(serde_json::to_value(&s.project).unwrap(), before); + assert_eq!((s.undo_depth(), s.undo_label(), s.redo_label()), history); + assert!(s.can_undo() && s.can_redo()); + assert_eq!(s.path.as_ref(), Some(&scratch.path("original.rgis"))); + assert!(!s.dirty()); + } + } +} + +/// Actual graph and web-tool writers still round-trip, with or without their legacy version marker. +#[test] +fn model_import_preserves_supported_and_legacy_files() { + use base64::Engine; + let scratch = Scratch::new("model-roundtrip"); + let mut original = Session::new(Project::new("Original")); + original + .run( + "model.new", + &json!({"name": "Graph", "graph": {"nodes": [ + {"id": "i", "kind": "input", "layer": "Points"}, + {"id": "t", "kind": "tool", "tool": "buffer", "params": {"distance": 37}}, + {"id": "o", "kind": "output", "name": "Near points"} + ], "edges": [ + {"from": "i", "fromPort": "out", "to": "t", "toPort": "input"}, + {"from": "t", "fromPort": "out", "to": "o", "toPort": "in"} + ]}}), + ) + .unwrap(); + original + .run( + "model.new", + &json!({"name": "Steps", "steps": [ + {"tool": "buffer", "params": {"input": "Points", "distance": 37}}, + {"tool": "buffer", "params": {"input": "%1", "distance": 74}} + ]}), + ) + .unwrap(); + let graph = original.run("model.export", &json!({"name": "Graph"})).unwrap(); + let (_, web, _) = rustgis_engine::cmd::sharing::share_bytes(&mut original, "webTool", &json!({"model": "Steps"})).unwrap(); + original.run("model.new", &json!({"name": "Long Steps", "steps": vec![json!({"tool": "buffer", "params": {}}); 1001]})).unwrap(); + let (_, long, _) = rustgis_engine::cmd::sharing::share_bytes(&mut original, "webTool", &json!({"model": "Long Steps"})).unwrap(); + for (index, text) in [graph["text"].as_str().unwrap().to_string(), String::from_utf8(web).unwrap(), String::from_utf8(long).unwrap()] + .into_iter() + .enumerate() + { + let file: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(file["version"], if index == 0 { json!("1.0.0") } else { json!(1) }); + for legacy in [false, true] { + let mut file = file.clone(); + if legacy { + file.as_object_mut().unwrap().remove("version"); + } + let text = file.to_string(); + let path = scratch.path(if index == 0 { "graph.model.json" } else { "steps.rgistool" }); + std::fs::write(&path, &text).unwrap(); + for params in + [json!({"text": text}), json!({"data": base64::engine::general_purpose::STANDARD.encode(&text)}), json!({"path": path})] + { + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", ¶ms).unwrap(); + assert_eq!(loaded.project.models[0], original.project.models[index]); + assert!(loaded.notices.is_empty(), "supported files need no warning"); + if index == 1 { + let graph = loaded.run("model.export", &json!({})).unwrap(); + let mut reopened = Session::new(Project::new("Graph")); + reopened.run("model.import", &json!({"text": graph["text"]})).unwrap(); + assert_eq!(reopened.project.models[0].steps, original.project.models[index].steps); + assert!(reopened.notices.is_empty(), "supported references need no warning"); + } + if index == 2 { + let output = scratch.path("keep.model.json"); + std::fs::write(&output, "keep").unwrap(); + let error = loaded.run("model.export", &json!({"path": output})).unwrap_err(); + assert!(error.contains("1001 steps") && error.contains("at most 1000"), "{error}"); + assert_eq!(std::fs::read_to_string(output).unwrap(), "keep"); + assert_eq!(loaded.project.models[0], original.project.models[index]); + } + } + } + } + // Step syntax stays literal for semicolons on single-input ports and nonnumeric percent names. + for (layer, tool, port) in [("East;West", "buffer", "input"), ("%north", "merge", "inputs")] { + let graph = json!({"nodes": [{"id": "i", "kind": "input", "layer": layer}, {"id": "t", "kind": "tool", "tool": tool}], + "edges": [{"from": "i", "fromPort": "out", "to": "t", "toPort": port}]}); + let mut loaded = Session::new(Project::default()); + loaded.run("model.import", &json!({"text": json!({"graph": graph}).to_string()})).unwrap(); + assert_eq!(loaded.project.models[0].steps[0].params[port], layer); + let (_, web, _) = rustgis_engine::cmd::sharing::share_bytes(&mut loaded, "webTool", &json!({})).unwrap(); + let mut reopened = Session::new(Project::default()); + reopened.run("model.import", &json!({"text": String::from_utf8(web).unwrap()})).unwrap(); + let exported = reopened.run("model.export", &json!({})).unwrap(); + let file: Value = serde_json::from_str(exported["text"].as_str().unwrap()).unwrap(); + assert!(file["graph"]["nodes"].as_array().unwrap().iter().any(|node| node["layer"] == layer)); + assert!(loaded.notices.is_empty() && reopened.notices.is_empty()); + } + // Graph capacity is checked before building or laying out a large multi-input step. + for (name, inputs) in + [("Connections", "A;".repeat(4001)), ("Nodes", (0..2001).map(|i| format!("Layer{i}")).collect::>().join(";"))] + { + original.run("model.new", &json!({"name": name, "steps": [{"tool": "merge", "params": {"inputs": inputs}}]})).unwrap(); + let before = original.project.models.clone(); + let error = original.run("model.export", &json!({"name": name})).unwrap_err(); + assert!(error.contains("graph limits"), "{error}"); + assert_eq!(original.project.models, before); + } + for (steps, reference, message) in [ + (json!([{"tool": "buffer", "params": {"input": "%0"}}]), "%0", "not an earlier step"), + (json!([{"tool": "buffer", "params": {"input": "%2"}}]), "%2", "not an earlier step"), + ( + json!([{"tool": "vectorToPmtiles", "params": {"input": "Points"}}, {"tool": "buffer", "params": {"input": "%1"}}]), + "%1", + "has no layer output", + ), + ] { + let mut loaded = Session::new(Project::new("Invalid reference")); + let file = json!({"steps": steps}); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + let before = loaded.project.models.clone(); + let output = scratch.path("keep-reference.model.json"); + std::fs::write(&output, "keep").unwrap(); + let error = loaded.run("model.export", &json!({"path": output})).unwrap_err(); + assert!(error.contains(reference) && error.contains(message), "{error}"); + assert_eq!(std::fs::read_to_string(output).unwrap(), "keep"); + assert_eq!(loaded.project.models, before); + } +} + +/// Even a name emitted by the writer can exceed the import naming limit after disambiguation. +#[test] +fn model_import_reports_changed_names() { + let mut original = Session::new(Project::new("Original")); + for _ in 0..2 { + original.run("model.new", &json!({"name": "N".repeat(200), "steps": [{"tool": "buffer", "params": {"input": "Cities"}}]})).unwrap(); + } + let name = original.project.models[1].name.clone(); + assert!(name.chars().count() > 200); + let exported = original.run("model.export", &json!({"name": name})).unwrap(); + let mut file: Value = serde_json::from_str(exported["text"].as_str().unwrap()).unwrap(); + for name in [file["name"].clone(), json!(" Padded "), json!(17)] { + file["name"] = name.clone(); + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + assert_ne!(Some(loaded.project.models[0].name.as_str()), name.as_str()); + assert_eq!(loaded.notices.len(), 1); + assert!(!loaded.notices[0].error && loaded.notices[0].text.contains("1 model name")); + assert_eq!(serde_json::to_value(loaded.project.models[0].graph.as_ref().unwrap()).unwrap(), file["graph"]); + } + file.as_object_mut().unwrap().remove("name"); + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + assert_eq!(loaded.project.models[0].name, "Tool"); + assert!(loaded.notices.is_empty(), "a missing name alone needs no warning"); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + assert_eq!(loaded.project.models[1].name, "Tool 2"); + assert_eq!(loaded.notices.len(), 1); + assert!(!loaded.notices[0].error && loaded.notices[0].text.contains("1 model name")); + assert_eq!(serde_json::to_value(loaded.project.models[1].graph.as_ref().unwrap()).unwrap(), file["graph"]); +} + +/// Repeated merge inputs survive import, saving under a new name and re-export. +#[test] +fn model_import_round_trips_repeated_step_inputs() { + let mut original = Session::new(Project::new("Original")); + let steps = json!([{"tool": "merge", "params": {"inputs": "A;A"}}]); + original.run("model.new", &json!({"name": "Repeated", "steps": steps})).unwrap(); + let exported = original.run("model.export", &json!({"name": "Repeated"})).unwrap(); + let mut file: Value = serde_json::from_str(exported["text"].as_str().unwrap()).unwrap(); + assert_eq!(file["graph"]["edges"].as_array().unwrap().len(), 2); + for legacy in [false, true] { + if legacy { + file.as_object_mut().unwrap().remove("version"); + } + let mut loaded = Session::new(Project::new("Loaded")); + loaded.run("model.import", &json!({"text": file.to_string()})).unwrap(); + let model = &loaded.project.models[0]; + assert_eq!(serde_json::to_value(&model.steps).unwrap(), steps); + assert_eq!(serde_json::to_value(model.graph.as_ref().unwrap()).unwrap(), file["graph"]); + loaded.run("model.save", &json!({"name": "Repeated", "rename": "Resaved", "graph": file["graph"]})).unwrap(); + assert_eq!(serde_json::to_value(&loaded.project.models[0].steps).unwrap(), steps); + assert_eq!(serde_json::to_value(loaded.project.models[0].graph.as_ref().unwrap()).unwrap(), file["graph"]); + assert!(loaded.notices.is_empty(), "supported files need no warning"); + for params in [json!({"name": "Resaved"}), json!({"name": "Resaved", "graph": file["graph"]})] { + let reexported = loaded.run("model.export", ¶ms).unwrap(); + let reexported: Value = serde_json::from_str(reexported["text"].as_str().unwrap()).unwrap(); + assert_eq!(reexported["graph"], file["graph"]); + } + } +} + /// The parcels' south-west corner, in Memphis, and their size in degrees: 4 columns by 3 rows. const WEST: f64 = -90.05; const SOUTH: f64 = 35.10; diff --git a/crates/ui-egui/src/modelbuilder.rs b/crates/ui-egui/src/modelbuilder.rs index 013fa79e6d..46d8e124fa 100644 --- a/crates/ui-egui/src/modelbuilder.rs +++ b/crates/ui-egui/src/modelbuilder.rs @@ -317,12 +317,12 @@ pub fn open(app: &mut RustGisApp, name: Option<&str>) -> Result<(), String> { forget_old_project(app); if let Some(n) = name { if !app.session.project.models.iter().any(|m| m.name.eq_ignore_ascii_case(n)) { - return Err(format!("no model '{n}'")); + return Err(crate::i18n::fmt("no model '{name}'", &[("name", n)])); } if app.mb.open && app.mb.dirty() && !app.mb.saved_as.as_deref().is_some_and(|s| s.eq_ignore_ascii_case(n)) { app.mb.confirm = Some(Pending::Open(n.to_string())); } else if !(app.mb.open && app.mb.saved_as.as_deref().is_some_and(|s| s.eq_ignore_ascii_case(n))) { - load_saved(app, n); + load_saved(app, n)?; } } else if !app.mb.open { let name = untitled(app); @@ -352,12 +352,19 @@ fn untitled(app: &RustGisApp) -> String { name } -fn load_saved(app: &mut RustGisApp, name: &str) { - let Some(m) = app.session.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name)) else { return }; - let graph = m.graph.clone().unwrap_or_else(|| model::steps_to_graph(&m.steps)); +/// Loads a saved model only when its graph can be shown without truncating its steps. +fn load_saved(app: &mut RustGisApp, name: &str) -> Result<(), String> { + let Some(m) = app.session.project.models.iter().find(|m| m.name.eq_ignore_ascii_case(name)) else { + return Err(crate::i18n::fmt("no model '{name}'", &[("name", name)])); + }; + let graph = match &m.graph { + Some(graph) => graph.clone(), + None => model::checked_steps_to_graph(&m.steps)?, + }; let imported = !app.session.trusted_models.contains(&m.name.to_ascii_lowercase()); let n = m.name.clone(); app.mb.load(&n, Some(n.clone()), graph, imported); + Ok(()) } /// Saves the canvas as a project model. @@ -474,7 +481,10 @@ pub(crate) fn import(app: &mut RustGisApp) { /// Opens a model `model.import` just added. pub fn imported(app: &mut RustGisApp, v: &Value) { if let Some(name) = v["model"].as_str() { - load_saved(app, name); + if let Err(error) = load_saved(app, name) { + app.session.notify(error, true); + return; + } app.mb.open = true; show_view(app); } @@ -487,7 +497,11 @@ fn proceed(app: &mut RustGisApp, p: Pending) { let name = untitled(app); app.mb.load(&name, None, ModelGraph::default(), false); } - Pending::Open(n) => load_saved(app, &n), + Pending::Open(n) => { + if let Err(error) = load_saved(app, &n) { + app.session.notify(error, true); + } + } Pending::Close => { app.mb.open = false; app.mb.active = false; @@ -1897,4 +1911,32 @@ mod tests { assert_eq!(app.mb.graph.edges.len(), 2); assert!(!app.mb.imported); } + + /// A step model beyond the conversion limit keeps its steps and the current canvas on + /// every open route, including import and an accepted discard prompt. + #[test] + fn oversized_step_models_do_not_replace_the_canvas() { + let mut app = RustGisApp::with_sample(); + app.run("model.new", json!({"name": "Keep", "steps": [{"tool": "buffer", "params": {"input": "Cities"}}]})).unwrap(); + app.run("ui.modelBuilder", json!({"name": "Keep"})).unwrap(); + let graph = app.mb.graph.clone(); + let steps = vec![json!({"tool": "buffer", "params": {}}); 1001]; + app.run("model.new", json!({"name": "Large", "steps": steps})).unwrap(); + let models = app.session.project.models.clone(); + + let error = app.run("ui.modelBuilder", json!({"name": "Large"})).unwrap_err(); + assert!(error.contains("1001 steps") && error.contains("at most 1000"), "{error}"); + for route in 0..2 { + app.session.notices.clear(); + if route == 0 { + imported(&mut app, &json!({"model": "Large"})); + } else { + proceed(&mut app, Pending::Open("Large".into())); + } + assert!(app.session.notices.iter().any(|notice| notice.error && notice.text.contains("1001 steps"))); + assert_eq!(app.mb.graph, graph); + assert_eq!(app.mb.saved_as.as_deref(), Some("Keep")); + assert_eq!(app.session.project.models, models); + } + } } diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 7d727b597b..87b48b7c7e 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1177,6 +1177,29 @@ their name. or write files. **Copy Command** copies a `model.run` command that runs the model from the Script pane, the command line or Python. +Import checks declared model and web-tool versions before interpreting their contents. Unsupported +versions, unreadable nodes, connections or steps, and unknown file, graph, node, connection or step +fields are refused. A file containing both a graph and steps is refused rather than choosing one. +Connections using unknown ports or feeding multiple sources into a single-valued port are refused. +Connections from inputs with missing or blank layer names are also refused. For connections to tools, +layer names must have no surrounding whitespace, must not look like numeric step references (`%1`), +and must not contain semicolons on multi-input ports; these names cannot be preserved in steps. +Graphs that would lose nodes or connections during import are refused with a count and +advice to fix the file, leaving the current project unchanged. Supported files and legacy files +without a version marker continue to import, including repeated sources in multi-input tools such +as Merge. Graphs containing a loop are refused rather than becoming empty steps. IDs that need +trimming or exceed 100 characters, and positions outside the supported +canvas bounds, are refused before they can be changed. Repeated inputs stay intact when an imported +model is saved or renamed. If import must trim, shorten or replace a model name, or make it unique, +it reports the change with a notice. + +Step files keep the existing 2,000-step limit. Converting steps to a ModelBuilder graph has a +1,000-step limit: opening, exporting or checking (`model.validate`) a larger model as a graph +gives an error and leaves the +original steps intact. References to a step that is not earlier in the model, or has no layer +output, also stop graph conversion before data can be dropped. Larger models can still run as +steps or be shared as a web tool. + The Workflows pane's Models section lists the project's models, and New Model From History turns the geoprocessing you've run into one. Models made as steps (`model.new` with `steps`, where `%1` stands for step 1's output) run as they always have until they are saved from ModelBuilder, which