diff --git a/crates/automation/src/actions/list.rs b/crates/automation/src/actions/list.rs index ad20af073..d19f1e654 100644 --- a/crates/automation/src/actions/list.rs +++ b/crates/automation/src/actions/list.rs @@ -482,8 +482,8 @@ impl ActionList { } let mut merged = MergedFile::default(); let mut names = Names::of(&self.actions); - for mut action in actions { - let name = names.claim(&action.name); + let claimed = names.claim_all(actions.iter().map(|action| action.name.as_str())); + for (mut action, name) in actions.into_iter().zip(claimed) { if name != action.name { merged.renames.push((action.name.clone(), name.clone())); } @@ -510,9 +510,9 @@ impl ActionList { /// keeps reaching the first action that has it. pub fn from_file(file: ActionsFile) -> Self { let ActionsFile { mut actions, sets, .. } = file; - let mut names = Names::default(); - for action in &mut actions { - action.name = names.claim(&action.name); + let claimed = Names::default().claim_all(actions.iter().map(|action| action.name.as_str())); + for (action, name) in actions.iter_mut().zip(claimed) { + action.name = name; } Self { actions, sets, revision: 0 } } @@ -575,6 +575,15 @@ impl Names { Self::new(actions.iter().map(|action| action.name.clone())) } + /// The names for `bases`, in order. Every base that is free keeps its name, even when an + /// earlier repeat of another base would have picked it as a suffix; only the repeats and the + /// clashes with taken names go through [`claim`](Self::claim). + fn claim_all<'a>(&mut self, bases: impl IntoIterator) -> Vec { + let bases: Vec<&str> = bases.into_iter().collect(); + let free: Vec = bases.iter().map(|base| self.taken.insert((*base).to_owned())).collect(); + bases.iter().zip(free).map(|(base, free)| if free { (*base).to_owned() } else { self.claim(base) }).collect() + } + /// `base` if it is free, else `base 2`, `base 3` … the first free one; the name returned is /// taken from then on. fn claim(&mut self, base: &str) -> String { diff --git a/crates/automation/src/actions/tests.rs b/crates/automation/src/actions/tests.rs index 42c428fb4..0ef6ba13f 100644 --- a/crates/automation/src/actions/tests.rs +++ b/crates/automation/src/actions/tests.rs @@ -80,6 +80,20 @@ fn action_list_file_round_trip_keeps_pc_json() { assert_eq!(back.sets, ["Empty"]); assert_eq!(ActionList::from_file(back).sets(), ["S", "Empty"]); } +// 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] +fn action_names_in_a_file_keep_their_own_names_and_rename_only_repeats() { + let names = |list: &ActionList| list.actions.iter().map(|action| action.name.clone()).collect::>(); + let file = || ActionsFile { version: ActionsFile::VERSION, actions: vec![action("Foo", &[]), action("Foo", &[]), action("Foo 2", &[])], sets: Vec::new() }; + assert_eq!(names(&ActionList::from_file(file())), ["Foo", "Foo 3", "Foo 2"]); + + let mut list = list_of(vec![action("Foo", &[])]); + let merged = list.merge_file(file(), false, |_, _| {}); + assert_eq!(names(&list), ["Foo", "Foo 3", "Foo 4", "Foo 2"]); + assert_eq!(merged.renames, [("Foo".to_owned(), "Foo 3".to_owned()), ("Foo".to_owned(), "Foo 4".to_owned())]); +} + /// A runner whose steps are named by what they do: `pause`, `wait`, `stop`, `deny` (refused by the /// gate), `noop`; any other is `Done`. Its own `charge` refuses once `charges` are spent. struct Scripted {