From 7df38eaaffad7065ecab1e0a3faa76e84d7de12b Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 8 Jan 2025 11:44:33 +0100 Subject: [PATCH] Implement `serde` traits to `Priority` instead of using custom funcs Ref https://github.com/shadowylab/ntfy/pull/10#issuecomment-2577334553 Signed-off-by: Yuki Kishimoto --- src/payload/mod.rs | 1 - src/payload/priority.rs | 38 +++++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/payload/mod.rs b/src/payload/mod.rs index 7e2e9f4..618a153 100644 --- a/src/payload/mod.rs +++ b/src/payload/mod.rs @@ -15,7 +15,6 @@ pub struct Payload { pub message: String, pub title: Option, pub tags: Option>, - #[serde(with = "priority")] pub priority: Priority, pub actions: Option>, pub click: Option, diff --git a/src/payload/priority.rs b/src/payload/priority.rs index 47b9d66..d3af204 100644 --- a/src/payload/priority.rs +++ b/src/payload/priority.rs @@ -15,24 +15,28 @@ pub enum Priority { Min = 1, } -pub(super) fn serialize(p: &Priority, s: S) -> Result -where - S: Serializer, -{ - let p: u8 = *p as u8; - p.serialize(s) +impl Serialize for Priority { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let p: u8 = *self as u8; + p.serialize(serializer) + } } -pub(super) fn deserialize<'de, D>(d: D) -> Result -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(deserializer: D) -> Result + 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))), + } } }