From 502db9b47d52095eac146761985b9684deb6af84 Mon Sep 17 00:00:00 2001 From: Tait Hoyem Date: Wed, 26 Jun 2024 20:18:01 -0600 Subject: [PATCH] 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() } }