Skip to content

Commit

Permalink
Add variant matching in try_from(string) for operation
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Jul 1, 2024
1 parent a91152c commit fe742c6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions atspi-common/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ pub enum Operation {
Insert,
#[serde(rename = "delete")]
#[serde(alias = "delete/system")]
#[serde(alias = "remove")]
#[serde(alias = "remove/system")]
Delete,
}

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

impl From<Operation> for String {
fn from(op: Operation) -> String {
match op {
Operation::Insert => "add",
Operation::Delete => "delete",
Operation::Insert => "insert",
Operation::Delete => "remove",
}
.to_string()
}
Expand Down

0 comments on commit fe742c6

Please sign in to comment.