Skip to content

Commit

Permalink
TryFrom<&str> -> FromStr; impl Display for Operation"
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Jul 1, 2024
1 parent fe742c6 commit 502db9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
8 changes: 4 additions & 4 deletions atspi-common/src/events/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ impl BusProperties for ChildrenChangedEvent {
fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
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()?,
})
Expand Down Expand Up @@ -955,7 +955,7 @@ impl BusProperties for TextChangedEvent {
fn from_message_parts(item: ObjectRef, body: Self::Body) -> Result<Self, AtspiError> {
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()?,
Expand Down Expand Up @@ -1168,7 +1168,7 @@ impl From<ChildrenChangedEvent> 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`
Expand Down Expand Up @@ -1581,7 +1581,7 @@ impl From<TextChangedEvent> 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,

Expand Down
22 changes: 12 additions & 10 deletions atspi-common/src/operation.rs
Original file line number Diff line number Diff line change
@@ -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)]

Check warning on line 6 in atspi-common/src/operation.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/operation.rs#L6

Added line #L6 was not covered by tests
Expand All @@ -15,23 +18,22 @@ pub enum Operation {
Delete,
}

impl TryFrom<&str> for Operation {
type Error = crate::AtspiError;
fn try_from(s: &str) -> Result<Operation, Self::Error> {
impl FromStr for Operation {
type Err = AtspiError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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"))),

Check warning on line 27 in atspi-common/src/operation.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/operation.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
}
}
}

impl From<Operation> 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"),

Check warning on line 36 in atspi-common/src/operation.rs

View check run for this annotation

Codecov / codecov/patch

atspi-common/src/operation.rs#L36

Added line #L36 was not covered by tests
}
.to_string()
}
}

0 comments on commit 502db9b

Please sign in to comment.