diff --git a/crates/keymap/src/command.rs b/crates/keymap/src/command.rs index 8ee338d32..96712aa59 100644 --- a/crates/keymap/src/command.rs +++ b/crates/keymap/src/command.rs @@ -9,8 +9,9 @@ use serde_json::Value; /// Longest binding key [`CommandRef::parse_key`] accepts, in bytes. pub const MAX_KEY: usize = 256; -/// A command id with the parameters a binding runs it with. -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, craft_ui_macros::PlainData)] +/// A command id with the parameters a binding runs it with. Two are equal when their binding keys are: `Null` and an +/// empty object are both no parameters. +#[derive(Clone, Debug, Serialize, Deserialize, craft_ui_macros::PlainData)] #[serde(rename_all = "camelCase")] pub struct CommandRef { /// The command's id. @@ -41,17 +42,19 @@ impl CommandRef { /// [`key`](Self::key), borrowing the id when it is the whole key: a lookup by key of a command without parameters /// allocates nothing. pub fn key_text(&self) -> Cow<'_, str> { - match &self.params { - Value::Null => Cow::Borrowed(&self.id), - Value::Object(m) if m.is_empty() => Cow::Borrowed(&self.id), - p => { - let mut out = String::with_capacity(self.id.len() + 16); - out.push_str(&self.id); - out.push(' '); - write_canonical(p, &mut out); - Cow::Owned(out) - } + if !self.has_params() { + return Cow::Borrowed(&self.id); } + let mut out = String::with_capacity(self.id.len() + 16); + out.push_str(&self.id); + out.push(' '); + write_canonical(&self.params, &mut out); + Cow::Owned(out) + } + + /// Whether the command has parameters: `Null` and an empty object are none. + fn has_params(&self) -> bool { + !matches!(&self.params, Value::Null) && !matches!(&self.params, Value::Object(m) if m.is_empty()) } /// Reads a binding key: the id up to the first space, then JSON parameters. `None` for an @@ -71,6 +74,12 @@ impl CommandRef { } } +impl PartialEq for CommandRef { + fn eq(&self, other: &CommandRef) -> bool { + self.id == other.id && (self.params == other.params || !self.has_params() && !other.has_params()) + } +} + impl fmt::Display for CommandRef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.key()) diff --git a/crates/keymap/tests/keymap.rs b/crates/keymap/tests/keymap.rs index 69605373c..fec6264cb 100644 --- a/crates/keymap/tests/keymap.rs +++ b/crates/keymap/tests/keymap.rs @@ -365,3 +365,16 @@ fn hold_bindings_clash_only_with_holds() { Err(AssignError::NotHoldable(_)) )); } + +/// Two command references are equal when their binding keys are: no parameters and an empty object are the same +/// command, other parameters tell commands apart. Guards against equality and the key disagreeing. +#[test] +fn command_refs_are_equal_when_their_keys_are() { + let bare = CommandRef::new("tool.move"); + let empty = CommandRef::with_params("tool.move", json!({})); + let given = CommandRef::with_params("tool.move", json!({"copy": true})); + assert_eq!(bare.key(), empty.key()); + assert_eq!(bare, empty, "no parameters and an empty object are one command"); + assert_ne!(bare.key(), given.key()); + assert_ne!(bare, given, "parameters tell commands apart"); +}