Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Batch: a failed step inside a called action fails the file and the script (#2779) (#2783)

Valentin Klein2fac209d56fb
diff --git a/crates/engine/src/automate_cmds.rs b/crates/engine/src/automate_cmds.rs
index 6827c1424..d3196b932 100644
--- a/crates/engine/src/automate_cmds.rs
+++ b/crates/engine/src/automate_cmds.rs
@@ -107,7 +107,14 @@ fn run_steps(s: &mut Session, steps: &[(String, Value)]) -> Vec<Value> {
let mut results = Vec::new();
for (id, params) in steps {
match s.execute(id, params.clone()) {
- Ok(r) => results.push(json!({"command": id, "result": r})),
+ // A called action reports a failed step inside an `Ok`; it stops the script too.
+ Ok(r) => match crate::actions_cmds::nested_failure(id, &r) {
+ Some(error) => {
+ results.push(json!({"command": id, "result": r, "error": error}));
+ break;
+ }
+ None => results.push(json!({"command": id, "result": r})),
+ },
Err(e) => {
results.push(json!({"command": id, "error": e.to_string()}));
break;
diff --git a/crates/engine/src/automate_cmds/tests.rs b/crates/engine/src/automate_cmds/tests.rs
index f6dff2317..33ef1328d 100644
--- a/crates/engine/src/automate_cmds/tests.rs
+++ b/crates/engine/src/automate_cmds/tests.rs
@@ -199,3 +199,32 @@ fn contact_sheet_places_thumbnails_with_captions() {
assert_eq!(d.layers.len(), 1);
}
}
+
+#[test]
+fn a_failed_step_inside_a_called_action_fails_scripts_and_batch() {
+ use crate::actions_cmds::Action;
+ let dir = tmp("nested-failure");
+ images(&dir, 1, 8, 4);
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 8, "height": 6})).unwrap();
+ s.actions.list.push(Action { name: "Rotate".into(), steps: vec![("image.imageRotation.90cw".into(), json!({}))] });
+ s.actions.list.push(Action { name: "Bad".into(), steps: vec![("layer.delete".into(), json!({"layer": 999_999}))] });
+ // A script stops at the failed call and reports it.
+ let r = s.execute("file.scripts.browse", json!({"steps": [["actions.play", {"action": "Bad"}], ["image.imageRotation.90cw", {}]]})).unwrap();
+ assert_eq!(r["ok"], false, "{r}");
+ assert_eq!(r["results"].as_array().unwrap().len(), 1);
+ // Batch plays called actions, and a failure inside one is a file error, not a saved result.
+ let batch = |s: &mut Session, action: &str| {
+ let p = json!({"steps": [["actions.play", {"action": action}]], "input": dir.clone(), "output": format!("{dir}/{action}"), "format": "png"});
+ s.execute("file.automate.batch", p).unwrap()
+ };
+ let r = batch(&mut s, "Rotate");
+ assert!(r["errors"].as_array().unwrap().is_empty(), "{r}");
+ let r = batch(&mut s, "Bad");
+ assert_eq!(r["files"], json!([]), "{r}");
+ assert!(r["errors"][0]["error"].as_str().unwrap().contains("no such layer"), "{r}");
+ // An action that batches itself stops as a recursive call instead of overflowing the stack.
+ let inner = json!({"steps": [["actions.play", {"action": "Loop"}]], "input": dir.clone(), "output": format!("{dir}/inner"), "format": "png"});
+ s.actions.list.push(Action { name: "Loop".into(), steps: vec![("file.automate.batch".into(), inner)] });
+ batch(&mut s, "Loop");
+}
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs
index 8c12a56a8..32ddb42b4 100644
--- a/crates/engine/src/file_cmds.rs
+++ b/crates/engine/src/file_cmds.rs
@@ -770,7 +770,7 @@ impl OutputClaims {
}
}
-fn batch(_s: &mut Session, p: &Value) -> Result<Value> {
+fn batch(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "file.automate.batch";
let steps =
parse_steps(p.get("steps").or_else(|| p.get("action")).ok_or_else(|| EngineError::BadParams { cmd: cmd.into(), msg: "missing \"steps\"".into() })?)?;
@@ -780,9 +780,22 @@ fn batch(_s: &mut Session, p: &Value) -> Result<Value> {
let inputs = batch_inputs(p, cmd)?;
let output = str_param(p, "output", cmd)?.to_string();
let format = p.get("format").and_then(Value::as_str).unwrap_or("same").to_string();
+ // Steps that call another action play it from the caller's actions; the playback stack comes
+ // along so an action that batches itself is caught as a recursive call.
+ let actions = crate::actions_cmds::ActionState {
+ list: s.actions.list.clone(),
+ playing: s.actions.playing,
+ playback_stack: s.actions.playback_stack.clone(),
+ ..Default::default()
+ };
let r = process_files(&inputs, &output, &format, SaveOpts::from_params(p), "", &|scratch| {
+ scratch.actions = actions.clone();
for (id, params) in &steps {
- scratch.execute(id, params.clone())?;
+ let r = scratch.execute(id, params.clone())?;
+ // A failed step inside a called action makes this file an error, not a saved result.
+ if let Some(error) = crate::actions_cmds::nested_failure(id, &r) {
+ return Err(EngineError::Other(error));
+ }
}
Ok(())
});