mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
Merge pull request #3318 from aiongg/rc-actions-order
automation: the actions file keeps every set's order
This commit is contained in:
@@ -22,6 +22,12 @@ pub struct ActionsFile {
|
||||
/// Sets that hold no action.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub sets: Vec<String>,
|
||||
/// 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<String>,
|
||||
/// 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<String>,
|
||||
#[serde(default)]
|
||||
set_order: Vec<String>,
|
||||
#[serde(default)]
|
||||
set_rows: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -71,7 +79,7 @@ impl From<RawFile> for ActionsFile {
|
||||
let (actions, occurrences): (Vec<Action>, Vec<ActionOccurrence>) = 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<Action>, sets: Vec<String>) -> 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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user