From 9da354739d323ac12f30ca3aa0a3308173d488dd Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 04:32:42 +0700 Subject: [PATCH] automation: ActionList::set_options replaces an action's app options --- crates/automation/src/actions/list.rs | 11 +++++++++++ crates/automation/src/actions/tests.rs | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/crates/automation/src/actions/list.rs b/crates/automation/src/actions/list.rs index 5ca18f8b0..4828bcb13 100644 --- a/crates/automation/src/actions/list.rs +++ b/crates/automation/src/actions/list.rs @@ -409,6 +409,17 @@ impl ActionList { Ok(()) } + /// Replaces the app's own options of the action at `index` ([`Action::options`]). Setting the options it holds + /// changes nothing, so the revision stays. + pub fn set_options(&mut self, index: usize, options: serde_json::Map) -> Result<(), ActionListError> { + let action = self.actions.get_mut(index).ok_or(ActionListError::NoSuchAction(index))?; + if action.options != options { + action.options = options; + self.touch(); + } + Ok(()) + } + /// Appends a whole action and returns its index; an empty or taken name is refused. pub fn push(&mut self, action: Action) -> Result { if action.name.trim().is_empty() { diff --git a/crates/automation/src/actions/tests.rs b/crates/automation/src/actions/tests.rs index a3ec1840c..7bea5df8c 100644 --- a/crates/automation/src/actions/tests.rs +++ b/crates/automation/src/actions/tests.rs @@ -332,3 +332,20 @@ fn app_options_ride_with_an_action_through_edits_and_files() { merged.merge_file(ActionsFile::from_json(&text).unwrap(), false, |_, _| {}); assert!(merged.actions().iter().all(|action| action.options.get("key") == Some(&json!(3)))); } + +// An action's options are set in the list, kept through a duplicate and the file, and the same options or an unknown +// action change nothing. +#[trial] +fn an_actions_options_are_set_and_kept_through_a_duplicate_and_the_file() { + let mut list = list_of(vec![action("a", &["one"])]); + let options = json!({"key": "F5", "automaticAlignTo": "artboard"}).as_object().cloned().unwrap(); + list.set_options(0, options.clone()).unwrap(); + assert_eq!(list.action(0).unwrap().options, options); + let revision = list.revision(); + list.set_options(0, options.clone()).unwrap(); + assert_eq!(list.revision(), revision, "the options it holds change nothing"); + assert_eq!(list.set_options(7, serde_json::Map::new()), Err(ActionListError::NoSuchAction(7))); + let copy = list.duplicate_action(0, |_, _, _| {}).unwrap(); + assert_eq!(list.action(copy).unwrap().options, options, "a duplicate keeps them"); + assert_eq!(ActionList::from_file(list.to_file()).action(0).unwrap().options, options, "the file keeps them"); +}