diff --git a/crates/ui-egui/src/io/save.rs b/crates/ui-egui/src/io/save.rs index 9d242427d1..ce4cb57ecd 100644 --- a/crates/ui-egui/src/io/save.rs +++ b/crates/ui-egui/src/io/save.rs @@ -542,6 +542,18 @@ pub(crate) fn saved_status(path: &str, r: &Value) -> String { } } +/// A string writer's error during a save: [`EngineError::Cancelled`] when the save was canceled and +/// the writer stopped on that Cancel (its message is the Cancel's own, alone or after the path), so +/// the file was not put in place. Any other message, such as one naming the copies a failed +/// replacement kept, stays [`EngineError::Other`]. +fn writer_error(e: String) -> EngineError { + use vectorcraft_engine::cancel; + let stopped = [EngineError::Cancelled.to_string(), cancel::Cancelled.to_string()] + .iter() + .any(|c| e == *c || e.strip_suffix(c.as_str()).is_some_and(|rest| rest.ends_with(": "))); + if stopped && cancel::is_cancelled() { EngineError::Cancelled } else { EngineError::Other(e) } +} + /// Write a planned save through the services and report it in the status bar. /// With Background Save on, the file is encoded and written from a snapshot off the UI thread /// ([`background`]): this returns `{path, background: true, status, format, warnings}` at once @@ -610,8 +622,8 @@ fn write_plan( let r = match (save_stream, save_write, stream) { (Some(stream), _, _) => job.write_streamed(&mut |p, fill| observed(p, stream(p, fill))), (_, Some(write), _) => job.write_unless(check, |p, b| observed(p, write(p, b))), - (_, _, Some(stream)) => job.write_streamed(&mut |p, fill| stream(p, fill).map_err(EngineError::Other)), - _ => job.write_unless(check, |p, b| write(p, b).map_err(EngineError::Other)), + (_, _, Some(stream)) => job.write_streamed(&mut |p, fill| stream(p, fill).map_err(writer_error)), + _ => job.write_unless(check, |p, b| write(p, b).map_err(writer_error)), }; match r { // Nothing written: the file is as it was. diff --git a/crates/ui-egui/src/tests/background.rs b/crates/ui-egui/src/tests/background.rs index ff26f7ff7c..f4e358bcf8 100644 --- a/crates/ui-egui/src/tests/background.rs +++ b/crates/ui-egui/src/tests/background.rs @@ -285,6 +285,39 @@ fn a_line_set_after_cancel_save_stays() { assert_eq!(app.ui.status, "Another command's line"); } +/// A Cancel that comes once the encoder has filled a streamed save, while the host's writer is still +/// flushing it, stops the save as any other Cancel does: the writer's last check refuses to put the +/// file in place, and the save reports it canceled, not failed. +#[test] +fn a_cancel_after_a_streamed_save_is_filled_cancels_it() { + let (gate, filled) = (Gate::default(), Gate::default()); + let (g, w) = (gate.clone(), filled.clone()); + let services = Services { + write_shared: Some(Arc::new(|_: &str, _: &[u8]| Ok(()))), + stream_shared: Some(Arc::new(move |_: &str, fill: &mut dyn FnMut(&mut dyn std::io::Write) -> std::io::Result<()>| { + fill(&mut Vec::new()).map_err(|e| e.to_string())?; + w.open(); + g.wait(); + // The atomic writer's last check before the file is put in place. + vectorcraft_engine::cancel::check().map_err(|e| std::io::Error::other(e).to_string()) + })), + ..Default::default() + }; + let mut app = VectorcraftApp::new(Session::new(), services); + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap(); + let r = app.run("file.save", json!({"path": "/docs/streamed.vectorcraft"})).unwrap(); + assert_eq!(r["background"], true, "{r}"); + filled.wait(); + assert_eq!(app.run("file.cancelSave", json!({})).unwrap()["cancelled"], 1); + gate.open(); + background::wait_all(&mut app); + assert!(app.ui.dialog.is_none(), "no error is shown"); + assert_eq!(app.ui.status, io::SAVE_CANCELED); + let st = app.session.active().unwrap(); + assert!(st.is_dirty() && st.path.is_none(), "the canceled save left the document as it was"); +} + #[test] fn quitting_waits_for_background_saves() { let gate = Gate::default();