From 484e31ad09e1f23b72cac2899248fbae716ce7d3 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 25 Jun 2024 21:44:11 -0600 Subject: [PATCH 1/9] Add new Operation type for add/delete string types --- atspi-common/src/operation.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 atspi-common/src/operation.rs diff --git a/atspi-common/src/operation.rs b/atspi-common/src/operation.rs new file mode 100644 index 00000000..7308f62b --- /dev/null +++ b/atspi-common/src/operation.rs @@ -0,0 +1,33 @@ +/// An operation can either be [`Self::Insert`] or [`Self::Delete`]. +/// These correspond to methods available on [`Vec`]. +#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] +pub enum Operation { + #[default] + #[serde(rename = "add")] + #[serde(alias = "add/system")] + Insert, + #[serde(rename = "delete")] + #[serde(alias = "delete/system")] + Delete, +} + +impl TryFrom<&str> for Operation { + type Error = crate::AtspiError; + fn try_from(s: &str) -> Result { + match s { + "add" | "add/system" => Ok(Operation::Insert), + "delete" | "delete/system" => Ok(Operation::Delete), + _ => Err(crate::AtspiError::KindMatch(format!("\"{s}\" is not a type of Operation"))), + } + } +} + +impl Into for Operation { + fn into(self) -> String { + match self { + Self::Insert => "add", + Self::Delete => "delete", + } + .to_string() + } +} From 76c60619b6eddb5f5c99f6412e41c3e67b6bde4a Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 25 Jun 2024 21:44:23 -0600 Subject: [PATCH 2/9] Add KindMatch error variant --- atspi-common/src/error.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/atspi-common/src/error.rs b/atspi-common/src/error.rs index 32b407dd..f6ac60bc 100644 --- a/atspi-common/src/error.rs +++ b/atspi-common/src/error.rs @@ -15,6 +15,9 @@ pub enum AtspiError { /// On specific types, if the event / message member does not match the Event's name. InterfaceMatch(String), + /// On specific types, if the kind (string variant) does not match the Event's kind. + KindMatch(String), + /// To indicate a match or equality test on a signal body signature failed. UnknownBusSignature(String), @@ -79,6 +82,9 @@ impl std::fmt::Display for AtspiError { Self::InterfaceMatch(e) => { f.write_str(format!("atspi: interface mismatch in conversion: {e}").as_str()) } + Self::KindMatch(e) => { + f.write_str(format!("atspi: kind mismatch in conversion: {e}").as_str()) + } Self::UnknownBusSignature(e) => { f.write_str(format!("atspi: Unknown bus body signature: {e:?}").as_str()) } From c24d4fcbf5afea9ee5d5dd9ea031905ec3accb4a Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 25 Jun 2024 21:44:41 -0600 Subject: [PATCH 3/9] Import Operation for crate --- atspi-common/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atspi-common/src/lib.rs b/atspi-common/src/lib.rs index 66a40915..fddc11ec 100644 --- a/atspi-common/src/lib.rs +++ b/atspi-common/src/lib.rs @@ -20,6 +20,8 @@ pub mod object_match; pub use object_match::{MatchType, ObjectMatchRule, SortOrder, TreeTraversalType}; pub mod object_ref; pub use object_ref::ObjectRef; +pub mod operation; +pub use operation::Operation; pub mod interface; pub use interface::{Interface, InterfaceSet}; pub mod state; From a43e072cfdb015bb86ace643663e59bdee6c0c1a Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 25 Jun 2024 21:45:06 -0600 Subject: [PATCH 4/9] Use Operation in TextChanged/ChildrenChangedEvent --- atspi-common/src/events/object.rs | 32 ++++++++----------------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/atspi-common/src/events/object.rs b/atspi-common/src/events/object.rs index 070ed12a..be5439be 100644 --- a/atspi-common/src/events/object.rs +++ b/atspi-common/src/events/object.rs @@ -454,16 +454,8 @@ mod i32_bool_conversion { pub struct ChildrenChangedEvent { /// The [`ObjectRef`] which the event applies to. pub item: crate::events::ObjectRef, - /// Operation, which may be one of: - /// - /// * "insert/system" - /// * "insert" - /// * "delete/system" - /// * "delete" - /// - /// The operation is the same whether it contains the "/system" suffix or not. - /// TODO: This should be an enum. - pub operation: String, + /// The [`Operation`] being performed. + pub operation: crate::Operation, /// Index to remove from/add to. pub index_in_parent: i32, /// A reference to the new child. @@ -570,16 +562,8 @@ pub struct TextSelectionChangedEvent { pub struct TextChangedEvent { /// The [`ObjectRef`] which the event applies to. pub item: crate::events::ObjectRef, - /// Operation, which may be one of: - /// - /// * "insert/system" - /// * "insert" - /// * "delete/system" - /// * "delete" - /// - /// The operation is the same whether it contains the "/system" suffix or not. - /// TODO: This should be an enum. - pub operation: String, + /// The [`Operation`] being performed. + pub operation: crate::Operation, /// starting index of the insertion/deletion pub start_pos: i32, /// length of the insertion/deletion @@ -692,7 +676,7 @@ impl BusProperties for ChildrenChangedEvent { fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result { Ok(Self { item, - operation: body.kind, + operation: body.kind.as_str().try_into()?, index_in_parent: body.detail1, child: body.any_data.try_into()?, }) @@ -971,7 +955,7 @@ impl BusProperties for TextChangedEvent { fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result { Ok(Self { item, - operation: body.kind, + operation: body.kind.as_str().try_into()?, start_pos: body.detail1, length: body.detail2, text: body.any_data.try_into()?, @@ -1184,7 +1168,7 @@ impl From for EventBodyOwned { fn from(event: ChildrenChangedEvent) -> Self { EventBodyOwned { properties: std::collections::HashMap::new(), - kind: event.operation, + kind: event.operation.into(), detail1: event.index_in_parent, detail2: i32::default(), // `OwnedValue` is constructed from the `ObjectRef` @@ -1597,7 +1581,7 @@ impl From for EventBodyOwned { fn from(event: TextChangedEvent) -> Self { EventBodyOwned { properties: std::collections::HashMap::new(), - kind: event.operation, + kind: event.operation.into(), detail1: event.start_pos, detail2: event.length, From 477bb47842ce3f2a3fe09f465b5325d0ead2d447 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 25 Jun 2024 22:15:08 -0600 Subject: [PATCH 5/9] Use From for String instead of Into for Op --- atspi-common/src/operation.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/atspi-common/src/operation.rs b/atspi-common/src/operation.rs index 7308f62b..f053dfd4 100644 --- a/atspi-common/src/operation.rs +++ b/atspi-common/src/operation.rs @@ -22,11 +22,11 @@ impl TryFrom<&str> for Operation { } } -impl Into for Operation { - fn into(self) -> String { - match self { - Self::Insert => "add", - Self::Delete => "delete", +impl From for String { + fn from(op: Operation) -> String { + match op { + Operation::Insert => "add", + Operation::Delete => "delete", } .to_string() } From 6b6fea145567f167178cefc4d86d7322e6204128 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Tue, 25 Jun 2024 22:20:31 -0600 Subject: [PATCH 6/9] Fix docs --- atspi-common/src/events/object.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/atspi-common/src/events/object.rs b/atspi-common/src/events/object.rs index be5439be..1bfed751 100644 --- a/atspi-common/src/events/object.rs +++ b/atspi-common/src/events/object.rs @@ -454,7 +454,7 @@ mod i32_bool_conversion { pub struct ChildrenChangedEvent { /// The [`ObjectRef`] which the event applies to. pub item: crate::events::ObjectRef, - /// The [`Operation`] being performed. + /// The [`crate::Operation`] being performed. pub operation: crate::Operation, /// Index to remove from/add to. pub index_in_parent: i32, @@ -562,7 +562,7 @@ pub struct TextSelectionChangedEvent { pub struct TextChangedEvent { /// The [`ObjectRef`] which the event applies to. pub item: crate::events::ObjectRef, - /// The [`Operation`] being performed. + /// The [`crate::Operation`] being performed. pub operation: crate::Operation, /// starting index of the insertion/deletion pub start_pos: i32, From a91152c2f177e4e3a5d3d9c183c23ffe090ba6c4 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Wed, 26 Jun 2024 08:45:09 -0600 Subject: [PATCH 7/9] Add insert+insert/system as expression of Operation::Insert --- atspi-common/src/operation.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atspi-common/src/operation.rs b/atspi-common/src/operation.rs index f053dfd4..ee312f9b 100644 --- a/atspi-common/src/operation.rs +++ b/atspi-common/src/operation.rs @@ -5,6 +5,8 @@ pub enum Operation { #[default] #[serde(rename = "add")] #[serde(alias = "add/system")] + #[serde(alias = "insert")] + #[serde(alias = "insert/system")] Insert, #[serde(rename = "delete")] #[serde(alias = "delete/system")] From fe742c62a2ec6bb1dfb5e72dac4dd9a9a03ca5d9 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Wed, 26 Jun 2024 08:49:56 -0600 Subject: [PATCH 8/9] Add variant matching in try_from(string) for operation --- atspi-common/src/operation.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/atspi-common/src/operation.rs b/atspi-common/src/operation.rs index ee312f9b..8563ac7b 100644 --- a/atspi-common/src/operation.rs +++ b/atspi-common/src/operation.rs @@ -10,6 +10,8 @@ pub enum Operation { Insert, #[serde(rename = "delete")] #[serde(alias = "delete/system")] + #[serde(alias = "remove")] + #[serde(alias = "remove/system")] Delete, } @@ -17,9 +19,9 @@ impl TryFrom<&str> for Operation { type Error = crate::AtspiError; fn try_from(s: &str) -> Result { match s { - "add" | "add/system" => Ok(Operation::Insert), - "delete" | "delete/system" => Ok(Operation::Delete), - _ => Err(crate::AtspiError::KindMatch(format!("\"{s}\" is not a type of Operation"))), + "add" | "add/system" | "insert" | "insert/system" => Ok(Operation::Insert), + "delete" | "delete/system" | "remove" | "remove/system" => Ok(Operation::Delete), + _ => Err(crate::AtspiError::KindMatch(format!("{s} is not a type of Operation"))), } } } @@ -27,8 +29,8 @@ impl TryFrom<&str> for Operation { impl From for String { fn from(op: Operation) -> String { match op { - Operation::Insert => "add", - Operation::Delete => "delete", + Operation::Insert => "insert", + Operation::Delete => "remove", } .to_string() } From 502db9b47d52095eac146761985b9684deb6af84 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Wed, 26 Jun 2024 20:18:01 -0600 Subject: [PATCH 9/9] TryFrom<&str> -> FromStr; impl Display for Operation" --- atspi-common/src/events/object.rs | 8 ++++---- atspi-common/src/operation.rs | 22 ++++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/atspi-common/src/events/object.rs b/atspi-common/src/events/object.rs index 1bfed751..a2ff363b 100644 --- a/atspi-common/src/events/object.rs +++ b/atspi-common/src/events/object.rs @@ -676,7 +676,7 @@ impl BusProperties for ChildrenChangedEvent { fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result { Ok(Self { item, - operation: body.kind.as_str().try_into()?, + operation: body.kind.as_str().parse()?, index_in_parent: body.detail1, child: body.any_data.try_into()?, }) @@ -955,7 +955,7 @@ impl BusProperties for TextChangedEvent { fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result { Ok(Self { item, - operation: body.kind.as_str().try_into()?, + operation: body.kind.as_str().parse()?, start_pos: body.detail1, length: body.detail2, text: body.any_data.try_into()?, @@ -1168,7 +1168,7 @@ impl From for EventBodyOwned { fn from(event: ChildrenChangedEvent) -> Self { EventBodyOwned { properties: std::collections::HashMap::new(), - kind: event.operation.into(), + kind: event.operation.to_string(), detail1: event.index_in_parent, detail2: i32::default(), // `OwnedValue` is constructed from the `ObjectRef` @@ -1581,7 +1581,7 @@ impl From for EventBodyOwned { fn from(event: TextChangedEvent) -> Self { EventBodyOwned { properties: std::collections::HashMap::new(), - kind: event.operation.into(), + kind: event.operation.to_string(), detail1: event.start_pos, detail2: event.length, diff --git a/atspi-common/src/operation.rs b/atspi-common/src/operation.rs index 8563ac7b..748d9faa 100644 --- a/atspi-common/src/operation.rs +++ b/atspi-common/src/operation.rs @@ -1,3 +1,6 @@ +use crate::AtspiError; +use std::{fmt, str::FromStr}; + /// An operation can either be [`Self::Insert`] or [`Self::Delete`]. /// These correspond to methods available on [`Vec`]. #[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, Eq, Hash, Default)] @@ -15,23 +18,22 @@ pub enum Operation { Delete, } -impl TryFrom<&str> for Operation { - type Error = crate::AtspiError; - fn try_from(s: &str) -> Result { +impl FromStr for Operation { + type Err = AtspiError; + fn from_str(s: &str) -> Result { match s { "add" | "add/system" | "insert" | "insert/system" => Ok(Operation::Insert), "delete" | "delete/system" | "remove" | "remove/system" => Ok(Operation::Delete), - _ => Err(crate::AtspiError::KindMatch(format!("{s} is not a type of Operation"))), + _ => Err(AtspiError::KindMatch(format!("{s} is not a type of Operation"))), } } } -impl From for String { - fn from(op: Operation) -> String { - match op { - Operation::Insert => "insert", - Operation::Delete => "remove", +impl fmt::Display for Operation { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Operation::Insert => write!(f, "insert"), + Operation::Delete => write!(f, "delete"), } - .to_string() } }