Skip to content

Commit

Permalink
Implement serde traits to Priority instead of using custom funcs
Browse files Browse the repository at this point in the history
Ref #10 (comment)

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jan 8, 2025
1 parent 0a058cb commit 7df38ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/payload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct Payload {
pub message: String,
pub title: Option<String>,
pub tags: Option<Vec<String>>,
#[serde(with = "priority")]
pub priority: Priority,
pub actions: Option<Vec<Action>>,
pub click: Option<Url>,
Expand Down
38 changes: 21 additions & 17 deletions src/payload/priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ pub enum Priority {
Min = 1,
}

pub(super) fn serialize<S>(p: &Priority, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let p: u8 = *p as u8;
p.serialize(s)
impl Serialize for Priority {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let p: u8 = *self as u8;
p.serialize(serializer)
}
}

pub(super) fn deserialize<'de, D>(d: D) -> Result<Priority, D::Error>
where
D: Deserializer<'de>,
{
match u8::deserialize(d)? {
5 => Ok(Priority::Max),
4 => Ok(Priority::High),
3 => Ok(Priority::Default),
2 => Ok(Priority::Low),
1 => Ok(Priority::Min),
o => Err(Error::custom(format_args!("Invalid value: {}", o))),
impl<'de> Deserialize<'de> for Priority {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
match u8::deserialize(deserializer)? {
5 => Ok(Priority::Max),
4 => Ok(Priority::High),
3 => Ok(Priority::Default),
2 => Ok(Priority::Low),
1 => Ok(Priority::Min),
o => Err(Error::custom(format_args!("Invalid value: {}", o))),
}
}
}

0 comments on commit 7df38ea

Please sign in to comment.