Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Synchronize Folder: steps never land in another library or swallow an Undo (review fixes)

Nathaniel Ledford05178a783f05
diff --git a/crates/engine/src/sync.rs b/crates/engine/src/sync.rs
index a1e1c799c..d4f638955 100644
--- a/crates/engine/src/sync.rs
+++ b/crates/engine/src/sync.rs
@@ -468,7 +468,15 @@ pub struct SyncWork {
pub struct SyncCommit {
opts: ImportOptions,
now: String,
- mark: u64,
+ /// The library the run was readied for: steps never land in another one (File › Open
+ /// Library while the work runs), whose photo ids name other photos.
+ library: Arc<()>,
+ /// The run's own undo steps so far, and the history's length after the last of them: they
+ /// are merged only while nothing else came in between (an Undo or an edit made meanwhile
+ /// stays its own step, and the run's parts stay theirs).
+ own: usize,
+ after_last: Option<usize>,
+ interleaved: bool,
report: SyncReport,
}
@@ -517,7 +525,16 @@ impl SyncJob {
missing: if choice.remove_missing { missing } else { Vec::new() },
sidecars,
};
- Ok((work, SyncCommit { opts, now, mark: s.commits(), report: SyncReport::default() }))
+ let commit = SyncCommit {
+ opts,
+ now,
+ library: Arc::clone(&s.library_identity),
+ own: 0,
+ after_last: None,
+ interleaved: false,
+ report: SyncReport::default(),
+ };
+ Ok((work, commit))
}
}
@@ -595,6 +612,22 @@ impl SyncCommit {
/// Put a step into the catalog (no file is touched). A part that fails is reported in the
/// end, and the rest is still done.
pub fn apply(&mut self, s: &mut Session, step: SyncStep) {
+ if !Arc::ptr_eq(&self.library, &s.library_identity) {
+ self.report.failed.push((String::new(), "another library was opened: the rest of the synchronize was dropped".into()));
+ return;
+ }
+ let commits = s.commits();
+ if self.after_last.is_some_and(|n| n != s.undo.len()) {
+ self.interleaved = true;
+ }
+ self.apply_step(s, step);
+ if s.commits() != commits {
+ self.own += usize::try_from(s.commits().wrapping_sub(commits)).unwrap_or(0);
+ self.after_last = Some(s.undo.len());
+ }
+ }
+
+ fn apply_step(&mut self, s: &mut Session, step: SyncStep) {
let report = &mut self.report;
match step {
SyncStep::Import(prepared) => match crate::import::commit_prepared(s, &self.opts, &self.now, prepared) {
@@ -649,8 +682,11 @@ impl SyncCommit {
}
/// What was done, as one undo step ("Synchronize Folder").
- pub fn finish(self, s: &mut Session) -> SyncReport {
- let steps = usize::try_from(s.commits().wrapping_sub(self.mark)).unwrap_or(usize::MAX).min(s.undo.len());
+ pub fn finish(mut self, s: &mut Session) -> SyncReport {
+ // a report line once, however many steps came after a library switch
+ self.report.failed.dedup();
+ let ours = Arc::ptr_eq(&self.library, &s.library_identity) && !self.interleaved && self.after_last == Some(s.undo.len());
+ let steps = if ours { self.own.min(s.undo.len()) } else { 0 };
s.merge_undo(steps, "Synchronize Folder");
if let Some(last) = s.undo.last_mut().filter(|_| steps == 1) {
last.label = "Synchronize Folder".into();
diff --git a/crates/engine/src/tests_sync.rs b/crates/engine/src/tests_sync.rs
index dd6ab7ac1..75d40d8cc 100644
--- a/crates/engine/src/tests_sync.rs
+++ b/crates/engine/src/tests_sync.rs
@@ -538,3 +538,62 @@ fn a_cancelled_synchronize_does_nothing_more() {
let r = commit.finish(&mut s);
assert_eq!((r.imported, s.undo.len()), (0, steps_before));
}
+
+/// Ready a synchronize of the trip folder and do its work, keeping the steps for the test to
+/// commit when it likes.
+fn readied(s: &mut Session, dir: &Scratch, choice: crate::sync::SyncChoice) -> (Vec<crate::sync::SyncStep>, crate::sync::SyncCommit) {
+ let changes = crate::sync::scan(s, &dir.path("trip"), false).unwrap();
+ let (work, commit) = crate::sync::SyncJob::start(s, changes, choice).unwrap();
+ let mut steps = Vec::new();
+ work.run(&crate::sync::SyncProgress::default(), |step| {
+ steps.push(step);
+ true
+ });
+ (steps, commit)
+}
+
+#[test]
+fn steps_for_a_library_that_was_closed_meanwhile_are_not_applied() {
+ let dir = Scratch::new("switched");
+ let mut s = library(&dir);
+ write_png(&dir.path("trip/c.png"), 3);
+ std::fs::remove_file(dir.path("trip/day1/b.png")).unwrap();
+ let choice = crate::sync::SyncChoice { remove_missing: true, ..Default::default() };
+ let (steps, mut commit) = readied(&mut s, &dir, choice);
+ // File › Open Library while the work runs: another catalog, whose photo ids mean other photos
+ s.library_identity = std::sync::Arc::new(());
+ let before = holdings(&s);
+ let undo = s.undo.len();
+ for step in steps {
+ commit.apply(&mut s, step);
+ }
+ let r = commit.finish(&mut s);
+ assert_eq!(holdings(&s), before, "nothing landed in the other library");
+ assert_eq!(s.undo.len(), undo);
+ assert_eq!((r.imported, r.removed), (0, 0), "{r:?}");
+ assert!(!r.failed.is_empty(), "and the report says why: {r:?}");
+}
+
+#[test]
+fn an_undo_pressed_while_synchronizing_is_not_folded_into_it() {
+ let dir = Scratch::new("undo-mid");
+ let mut s = library(&dir);
+ let a = s.catalog.photos().find(|p| p.file_name == "a.png").unwrap().id;
+ s.execute("photo.rate", &json!({"ids": [a.0], "rating": 4})).unwrap();
+ write_png(&dir.path("trip/c.png"), 3);
+ std::fs::remove_file(dir.path("trip/day1/b.png")).unwrap();
+ let choice = crate::sync::SyncChoice { remove_missing: true, ..Default::default() };
+ let (mut steps, mut commit) = readied(&mut s, &dir, choice);
+ assert!(steps.len() >= 2, "an import step and a remove step");
+ let first = steps.remove(0);
+ commit.apply(&mut s, first);
+ // the person presses Undo between two steps of the run
+ s.execute("edit.undo", &json!({})).unwrap();
+ for step in steps {
+ commit.apply(&mut s, step);
+ }
+ commit.finish(&mut s);
+ // undoing the run's last step never takes the rating with it
+ s.execute("edit.undo", &json!({})).unwrap();
+ assert_eq!(s.catalog.photo(a).unwrap().rating, 4, "the earlier, unrelated step stays");
+}