-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #197 from odilia-app/add-operation-enum
Add Operation Enum
- Loading branch information
Showing
4 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} | ||
} |