rspotify_model/
custom_serde.rs

1//! Custom serialization methods used throughout the crate
2
3pub mod duration_ms {
4    use chrono::Duration;
5    use serde::{de, Serializer};
6    use std::convert::TryFrom;
7    use std::fmt;
8
9    /// Vistor to help deserialize duration represented as millisecond to
10    /// `chrono::Duration`.
11    pub struct DurationVisitor;
12    impl de::Visitor<'_> for DurationVisitor {
13        type Value = Duration;
14        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
15            write!(formatter, "a milliseconds represents chrono::Duration")
16        }
17        fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
18        where
19            E: de::Error,
20        {
21            Duration::try_milliseconds(v).ok_or_else(|| {
22                E::invalid_value(
23                    serde::de::Unexpected::Signed(v),
24                    &"an invalid duration in milliseconds",
25                )
26            })
27        }
28
29        // JSON deserializer calls visit_u64 for non-negative intgers
30        fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
31        where
32            E: de::Error,
33        {
34            match i64::try_from(v) {
35                Ok(val) => Duration::try_milliseconds(val).ok_or_else(|| {
36                    E::invalid_value(
37                        serde::de::Unexpected::Signed(val),
38                        &"a valid duration in
39        milliseconds",
40                    )
41                }),
42                Err(_) => Err(E::custom(format!(
43                    "Conversion error: u64 to i64 conversion failed for value {v}"
44                ))),
45            }
46        }
47    }
48
49    /// Deserialize `chrono::Duration` from milliseconds (represented as i64)
50    pub fn deserialize<'de, D>(d: D) -> Result<Duration, D::Error>
51    where
52        D: de::Deserializer<'de>,
53    {
54        d.deserialize_i64(DurationVisitor)
55    }
56
57    /// Serialize `chrono::Duration` to milliseconds (represented as i64)
58    pub fn serialize<S>(x: &Duration, s: S) -> Result<S::Ok, S::Error>
59    where
60        S: Serializer,
61    {
62        s.serialize_i64(x.num_milliseconds())
63    }
64}
65
66pub mod option_duration_ms {
67    use crate::custom_serde::duration_ms;
68    use chrono::Duration;
69    use serde::{de, Serializer};
70    use std::fmt;
71
72    /// Vistor to help deserialize duration represented as milliseconds to
73    /// `Option<chrono::Duration>`
74    struct OptionDurationVisitor;
75
76    impl<'de> de::Visitor<'de> for OptionDurationVisitor {
77        type Value = Option<Duration>;
78
79        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
80            write!(
81                formatter,
82                "a optional milliseconds represents chrono::Duration"
83            )
84        }
85
86        fn visit_none<E>(self) -> Result<Self::Value, E>
87        where
88            E: de::Error,
89        {
90            Ok(None)
91        }
92
93        fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
94        where
95            D: de::Deserializer<'de>,
96        {
97            Ok(Some(
98                deserializer.deserialize_i64(duration_ms::DurationVisitor)?,
99            ))
100        }
101    }
102
103    /// Deserialize `Option<chrono::Duration>` from milliseconds
104    /// (represented as i64)
105    pub fn deserialize<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
106    where
107        D: de::Deserializer<'de>,
108    {
109        d.deserialize_option(OptionDurationVisitor)
110    }
111
112    /// Serialize `Option<chrono::Duration>` to milliseconds (represented as
113    /// i64)
114    pub fn serialize<S>(x: &Option<Duration>, s: S) -> Result<S::Ok, S::Error>
115    where
116        S: Serializer,
117    {
118        match *x {
119            Some(duration) => s.serialize_i64(duration.num_milliseconds()),
120            None => s.serialize_none(),
121        }
122    }
123}
124
125/// Deserialize/Serialize `Modality` to integer(0, 1, -1).
126pub mod modality {
127    use crate::Modality;
128    use serde::{de, Deserialize, Serializer};
129
130    pub fn deserialize<'de, D>(d: D) -> Result<Modality, D::Error>
131    where
132        D: de::Deserializer<'de>,
133    {
134        let v = i8::deserialize(d)?;
135        match v {
136            0 => Ok(Modality::Minor),
137            1 => Ok(Modality::Major),
138            -1 => Ok(Modality::NoResult),
139            _ => Err(de::Error::invalid_value(
140                de::Unexpected::Signed(v.into()),
141                &"valid value: 0, 1, -1",
142            )),
143        }
144    }
145
146    pub fn serialize<S>(x: &Modality, s: S) -> Result<S::Ok, S::Error>
147    where
148        S: Serializer,
149    {
150        match x {
151            Modality::Minor => s.serialize_i8(0),
152            Modality::Major => s.serialize_i8(1),
153            Modality::NoResult => s.serialize_i8(-1),
154        }
155    }
156}
157
158pub mod duration_second {
159    use chrono::Duration;
160    use serde::{de, Deserialize, Serializer};
161
162    /// Deserialize `chrono::Duration` from seconds (represented as u64)
163    pub fn deserialize<'de, D>(d: D) -> Result<Duration, D::Error>
164    where
165        D: de::Deserializer<'de>,
166    {
167        let duration: i64 = Deserialize::deserialize(d)?;
168        Duration::try_seconds(duration).ok_or(serde::de::Error::invalid_value(
169            serde::de::Unexpected::Signed(duration),
170            &"an invalid duration in seconds",
171        ))
172    }
173
174    /// Serialize `chrono::Duration` to seconds (represented as u64)
175    pub fn serialize<S>(x: &Duration, s: S) -> Result<S::Ok, S::Error>
176    where
177        S: Serializer,
178    {
179        s.serialize_i64(x.num_seconds())
180    }
181}
182
183pub mod space_separated_scopes {
184    use serde::{de, Deserialize, Serializer};
185    use std::collections::HashSet;
186
187    pub fn deserialize<'de, D>(d: D) -> Result<HashSet<String>, D::Error>
188    where
189        D: de::Deserializer<'de>,
190    {
191        let scopes: String = Deserialize::deserialize(d)?;
192        Ok(scopes.split_whitespace().map(ToOwned::to_owned).collect())
193    }
194
195    pub fn serialize<S>(scopes: &HashSet<String>, s: S) -> Result<S::Ok, S::Error>
196    where
197        S: Serializer,
198    {
199        let scopes = scopes.clone().into_iter().collect::<Vec<_>>().join(" ");
200        s.serialize_str(&scopes)
201    }
202}