Skip to content

Commit

Permalink
Merge pull request #197 from odilia-app/add-operation-enum
Browse files Browse the repository at this point in the history
Add Operation Enum
  • Loading branch information
TTWNO authored Jul 1, 2024
2 parents 3090cd3 + 502db9b commit 4cb4cd5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
6 changes: 6 additions & 0 deletions atspi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down Expand Up @@ -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())
}
Expand Down
32 changes: 8 additions & 24 deletions atspi-common/src/events/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [`crate::Operation`] being performed.
pub operation: crate::Operation,
/// Index to remove from/add to.
pub index_in_parent: i32,
/// A reference to the new child.
Expand Down Expand Up @@ -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 [`crate::Operation`] being performed.
pub operation: crate::Operation,
/// starting index of the insertion/deletion
pub start_pos: i32,
/// length of the insertion/deletion
Expand Down Expand Up @@ -692,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,
operation: body.kind.as_str().parse()?,
index_in_parent: body.detail1,
child: body.any_data.try_into()?,
})
Expand Down Expand Up @@ -971,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,
operation: body.kind.as_str().parse()?,
start_pos: body.detail1,
length: body.detail2,
text: body.any_data.try_into()?,
Expand Down Expand Up @@ -1184,7 +1168,7 @@ impl From<ChildrenChangedEvent> for EventBodyOwned {
fn from(event: ChildrenChangedEvent) -> Self {
EventBodyOwned {
properties: std::collections::HashMap::new(),
kind: event.operation,
kind: event.operation.to_string(),
detail1: event.index_in_parent,
detail2: i32::default(),
// `OwnedValue` is constructed from the `ObjectRef`
Expand Down Expand Up @@ -1597,7 +1581,7 @@ impl From<TextChangedEvent> for EventBodyOwned {
fn from(event: TextChangedEvent) -> Self {
EventBodyOwned {
properties: std::collections::HashMap::new(),
kind: event.operation,
kind: event.operation.to_string(),
detail1: event.start_pos,
detail2: event.length,

Expand Down
2 changes: 2 additions & 0 deletions atspi-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions atspi-common/src/operation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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)]
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")]
#[serde(alias = "remove")]
#[serde(alias = "remove/system")]
Delete,
}

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(AtspiError::KindMatch(format!("{s} is not a type of Operation"))),
}
}
}

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"),
}
}
}

0 comments on commit 4cb4cd5

Please sign in to comment.