Skip to content

Commit 29601bb

Browse files
committed
Add Priority::from_str and Priority::as_str
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent f330210 commit 29601bb

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/payload/priority.rs

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) 2022 Yuki Kishimoto
22
// Distributed under the MIT software license
33

4+
use std::fmt;
5+
use std::str::FromStr;
6+
47
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
58

69
use crate::error::Error;
@@ -16,6 +19,12 @@ pub enum Priority {
1619
Min = 1,
1720
}
1821

22+
impl fmt::Display for Priority {
23+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
write!(f, "{}", self.as_str())
25+
}
26+
}
27+
1928
impl Priority {
2029
pub fn from_u8(priority: u8) -> Result<Self, Error> {
2130
match priority {
@@ -33,6 +42,32 @@ impl Priority {
3342
pub fn as_u8(&self) -> u8 {
3443
*self as u8
3544
}
45+
46+
/// Convert to `&str`
47+
pub fn as_str(&self) -> &str {
48+
match self {
49+
Self::Max => "max",
50+
Self::High => "high",
51+
Self::Default => "default",
52+
Self::Low => "low",
53+
Self::Min => "min",
54+
}
55+
}
56+
}
57+
58+
impl FromStr for Priority {
59+
type Err = Error;
60+
61+
fn from_str(priority: &str) -> Result<Self, Self::Err> {
62+
match priority {
63+
"max" | "urgent" => Ok(Self::Max),
64+
"high" => Ok(Self::High),
65+
"default" => Ok(Self::Default),
66+
"low" => Ok(Self::Low),
67+
"min" => Ok(Self::Min),
68+
_ => Err(Error::UnknownPriority),
69+
}
70+
}
3671
}
3772

3873
impl Serialize for Priority {

0 commit comments

Comments
 (0)