address review: a failed group write keeps only the old files that hold its presets

This commit is contained in:
Dylan M. Taylor committed 2026-11-11 08:54:31 +00:00
1 parent 2764186dd5
commit 390080ee17
2 files changed
+30 -4

No files matched your search

+8 -4
View File
@@ -921,7 +921,8 @@ impl PresetStore {
None => order.push((p.group.clone(), vec![i])),
}
}
let warnings_before = self.warnings.len();
// Names of the presets in groups that could not be written: the old files that hold them stay.
let mut unsaved: HashSet<String> = HashSet::new();
let mut files = Vec::with_capacity(order.len());
for (group, idx) in &order {
let file = group_file_name(group);
@@ -941,14 +942,17 @@ impl PresetStore {
continue;
}
let mut items: Vec<&mut BrushPreset> = presets.iter_mut().filter(|p| !p.builtin && p.group == *group).collect();
let names: Vec<String> = items.iter().map(|p| p.name.clone()).collect();
if let Err(e) = self.write_group(&file, group, &mut items) {
unsaved.extend(names);
self.warnings.push(format!("Couldn't save brush presets `{}`: {e}", if group.is_empty() { "(ungrouped)" } else { group }));
}
}
// Groups that are gone. When a group could not be written (a rename to a name that failed,
// say), the old files stay: they may be the only copy of those presets and their tips.
let write_failed = self.warnings.len() > warnings_before;
let gone: Vec<String> = self.groups.keys().filter(|f| !write_failed && !files.contains(f)).cloned().collect();
// say), the old files holding its presets stay: they may be the only copy of those presets and
// their tips. Other removed groups are still deleted.
let gone: Vec<String> =
self.groups.iter().filter(|(f, g)| !files.contains(f) && !g.presets.iter().any(|p| unsaved.contains(&p.name))).map(|(f, _)| f.clone()).collect();
for f in gone {
match self.backend.remove(&f) {
Ok(()) => drop(self.groups.remove(&f)),
+22
View File
@@ -1189,3 +1189,25 @@ fn a_group_that_cannot_be_saved_keeps_the_old_group_file_and_tips() {
assert!(w.is_empty(), "{w:?}");
assert_eq!(photocraft_paint::presets::find(&t.tools.presets, "Keep").map(|p| p.group.clone()), Some(name));
}
#[test]
fn a_failed_group_write_does_not_keep_unrelated_deleted_groups() {
let dir = TempDir::new("keep-unrelated");
let (mut s, _) = session(&dir);
s.tools.presets.push(sampled("Keep", "Old", tip8(8, 8, 1)));
s.tools.presets.push(sampled("Drop", "Gone", tip8(8, 8, 2)));
s.brush_presets_changed();
s.sync_preset_store();
let before = dir.files();
assert_eq!(before.iter().filter(|f| f.ends_with(".pcbrushes")).count(), 2, "{before:?}");
// "Keep" moves to a group name that cannot be written; "Drop" is deleted.
s.tools.presets.retain(|p| p.name != "Drop");
for p in s.tools.presets.iter_mut().filter(|p| p.name == "Keep") {
p.group = "g".repeat(MAX_GROUP_NAME_BYTES + 1);
}
s.brush_presets_changed();
s.sync_preset_store();
let (t, _) = session(&dir);
assert!(photocraft_paint::presets::find(&t.tools.presets, "Keep").is_some(), "the old copy of the unsaved preset stays");
assert!(photocraft_paint::presets::find(&t.tools.presets, "Drop").is_none(), "the unrelated deleted group is gone after a restart");
}