ruma_common/serde/duration/
secs.rs
1use std::time::Duration;
7
8use js_int::UInt;
9use serde::{
10 de::{Deserialize, Deserializer},
11 ser::{Error, Serialize, Serializer},
12};
13
14pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
19where
20 S: Serializer,
21{
22 match UInt::try_from(duration.as_secs()) {
23 Ok(uint) => uint.serialize(serializer),
24 Err(err) => Err(S::Error::custom(err)),
25 }
26}
27
28pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
33where
34 D: Deserializer<'de>,
35{
36 UInt::deserialize(deserializer).map(|secs| Duration::from_secs(secs.into()))
37}
38
39#[cfg(test)]
40mod tests {
41 use std::time::Duration;
42
43 use serde::{Deserialize, Serialize};
44 use serde_json::json;
45
46 #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
47 struct DurationTest {
48 #[serde(with = "super")]
49 timeout: Duration,
50 }
51
52 #[test]
53 fn deserialize() {
54 let json = json!({ "timeout": 3 });
55
56 assert_eq!(
57 serde_json::from_value::<DurationTest>(json).unwrap(),
58 DurationTest { timeout: Duration::from_secs(3) },
59 );
60 }
61
62 #[test]
63 fn serialize() {
64 let test = DurationTest { timeout: Duration::from_millis(7000) };
65 assert_eq!(serde_json::to_value(test).unwrap(), json!({ "timeout": 7 }));
66 }
67}