From 225de8b83cb6a40eb518431f2f6ab8f08f515d42 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 06:45:13 +0000 Subject: [PATCH] automation: the actions file keeps every set's order ActionList::to_file stored only the empty sets, so a set created empty before filled ones moved after them on reload. The file gains an optional set_order, written when loading would not give the list's order; files without it load as before. --- crates/automation/src/actions/file.rs | 12 ++++++++++-- crates/automation/src/actions/list.rs | 20 +++++++++++++++++--- crates/automation/src/actions/tests.rs | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/crates/automation/src/actions/file.rs b/crates/automation/src/actions/file.rs index 845fd82a0..3553932c9 100644 --- a/crates/automation/src/actions/file.rs +++ b/crates/automation/src/actions/file.rs @@ -22,6 +22,12 @@ pub struct ActionsFile { /// Sets that hold no action. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub sets: Vec, + /// Every set name in display order, empty sets included. Written only when loading `actions` + /// and `sets` would not give this order (the actions' sets in order of first appearance, then + /// the empty sets), so files in that natural order, PhotoCraft's included, save unchanged. + /// Files without it load in the natural order. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub set_order: Vec, /// The folder occurrences a PhotoCraft file carried; empty for every other file. #[serde(skip)] pub(super) occurrences: FileOccurrences, @@ -55,6 +61,8 @@ struct RawFile { #[serde(default)] sets: Vec, #[serde(default)] + set_order: Vec, + #[serde(default)] set_rows: Vec, } @@ -71,7 +79,7 @@ impl From for ActionsFile { let (actions, occurrences): (Vec, Vec) = raw.actions.into_iter().map(|raw| (raw.action, raw.occurrence)).unzip(); let empty = raw.set_rows.is_empty() && occurrences.iter().all(|occurrence| *occurrence == ActionOccurrence::default()); let occurrences = if empty { FileOccurrences::default() } else { FileOccurrences { set_rows: raw.set_rows, actions: occurrences } }; - ActionsFile { version: raw.version, actions, sets: raw.sets, occurrences } + ActionsFile { version: raw.version, actions, sets: raw.sets, set_order: raw.set_order, occurrences } } } @@ -117,7 +125,7 @@ impl ActionsFile { /// A file of version [`VERSION`](Self::VERSION) with `actions` and the empty `sets`. pub fn new(actions: Vec, sets: Vec) -> Self { - ActionsFile { version: Self::VERSION, actions, sets, occurrences: FileOccurrences::default() } + ActionsFile { version: Self::VERSION, actions, sets, set_order: Vec::new(), occurrences: FileOccurrences::default() } } /// Reads a file's JSON text. diff --git a/crates/automation/src/actions/list.rs b/crates/automation/src/actions/list.rs index 4828bcb13..ce533f746 100644 --- a/crates/automation/src/actions/list.rs +++ b/crates/automation/src/actions/list.rs @@ -576,12 +576,13 @@ impl ActionList { /// actions sharing a name, the file's shortcut owner keeps the name. The stored ids come back /// in [`MergedFile::ids`]. pub fn merge_file(&mut self, file: ActionsFile, replace: bool, mut calls: impl FnMut(&mut ActionStep, &[(String, String)])) -> MergedFile { - let ActionsFile { mut actions, sets, occurrences, .. } = file; + let ActionsFile { mut actions, sets, set_order, occurrences, .. } = file; if replace { let named: BTreeSet<&str> = actions .iter() .filter_map(|action| action.set.as_deref()) .chain(sets.iter().map(String::as_str)) + .chain(set_order.iter().map(String::as_str)) .chain(occurrences.set_rows.iter().map(String::as_str)) .collect(); self.actions.retain(|action| !action.set.as_deref().is_some_and(|set| named.contains(set))); @@ -596,6 +597,9 @@ impl ActionList { for (&i, name) in order.iter().zip(claimed) { given[i] = name; } + for set in &set_order { + self.record_set(set); + } let mut merged = MergedFile::default(); let ids = file_ids(&occurrences.actions); for (index, (mut action, name)) in actions.into_iter().zip(given).enumerate() { @@ -757,10 +761,20 @@ impl ActionList { self.touch(); } - /// The list as a file (version [`ActionsFile::VERSION`]; the empty sets in `sets`). + /// The list as a file (version [`ActionsFile::VERSION`]; the empty sets in `sets`, the set order in `set_order` when it is not the natural one). pub fn to_file(&self) -> ActionsFile { let empty = self.sets().into_iter().filter(|set| !self.actions.iter().any(|action| action.set.as_deref() == Some(*set))); - ActionsFile::new(self.actions.clone(), empty.map(str::to_owned).collect()) + let mut file = ActionsFile::new(self.actions.clone(), empty.map(str::to_owned).collect()); + let mut natural: Vec<&str> = Vec::new(); + for set in self.actions.iter().filter_map(|action| action.set.as_deref()).chain(file.sets.iter().map(String::as_str)) { + if !natural.contains(&set) { + natural.push(set); + } + } + if natural != self.sets() { + file.set_order = self.sets().into_iter().map(str::to_owned).collect(); + } + file } /// Puts `set` at the end of the set order when the order lacks it. diff --git a/crates/automation/src/actions/tests.rs b/crates/automation/src/actions/tests.rs index 7bea5df8c..2ae7845e9 100644 --- a/crates/automation/src/actions/tests.rs +++ b/crates/automation/src/actions/tests.rs @@ -80,6 +80,22 @@ fn action_list_file_round_trip_keeps_pc_json() { assert_eq!(back.sets, ["Empty"]); assert_eq!(ActionList::from_file(back).sets(), ["S", "Empty"]); } +// An empty set made before filled ones keeps its place through a save; a file of the older layout +// (empty sets in `sets`, no `set_order`) loads with its filled sets first. +#[trial] +fn set_order_survives_the_file() { + let mut list = ActionList::new(); + list.new_set("A").unwrap(); + list.new_set("B").unwrap(); + list.new_set("C").unwrap(); + list.push(Action { set: Some("B".into()), ..action("X", &[]) }).unwrap(); + list.push(Action { set: Some("C".into()), ..action("Y", &[]) }).unwrap(); + let back = ActionList::from_file(ActionsFile::from_json(&list.to_file().to_json().unwrap()).unwrap()); + assert_eq!(back.sets(), ["A", "B", "C"]); + + let old = r#"{"version":1,"actions":[{"name":"X","steps":[],"set":"F"}],"sets":["E"]}"#; + assert_eq!(ActionList::from_file(ActionsFile::from_json(old).unwrap()).sets(), ["F", "E"]); +} // A file that repeats a name keeps a later action's own name: `Foo, Foo, Foo 2` renames the repeat // (`Foo 3`), never the real `Foo 2`; a merge does the same against the names already in the list. #[trial]