serde_nanos/
ser.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#[cfg(feature = "chrono")]
use chrono;

use serde::ser::SerializeSeq;
use serde::Serializer;

/// Types that can be serialized via `#[serde(with = "serde_nanos")]`.
pub trait Serialize {
    #[allow(missing_docs)]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer;
}

impl Serialize for std::time::Duration {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let nanoseconds = self.as_nanos() as i64;
        serializer.serialize_i64(nanoseconds)
    }
}

impl Serialize for Option<std::time::Duration> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        struct Data<'a, V: 'a>(&'a V)
        where
            V: Serialize;

        impl<'a, V: Serialize + 'a> serde::Serialize for Data<'a, V> {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                self.0.serialize(serializer)
            }
        }

        match *self {
            Some(ref value) => serializer.serialize_some(&Data(value)),
            None => serializer.serialize_none(),
        }
    }
}

impl Serialize for Vec<std::time::Duration> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.len()))?;
        for e in self {
            let nanos = e.as_nanos() as i64;
            seq.serialize_element(&nanos)?;
        }
        seq.end()
    }
}

#[cfg(feature = "chrono")]
impl Serialize for chrono::Duration {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let nanoseconds = self.num_nanoseconds().unwrap();
        serializer.serialize_i64(nanoseconds)
    }
}

#[cfg(feature = "chrono")]
impl Serialize for Option<chrono::Duration> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        struct Data<'a, V: 'a>(&'a V)
        where
            V: Serialize;

        impl<'a, V: Serialize + 'a> serde::Serialize for Data<'a, V> {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                self.0.serialize(serializer)
            }
        }

        match *self {
            Some(ref value) => serializer.serialize_some(&Data(value)),
            None => serializer.serialize_none(),
        }
    }
}

#[cfg(feature = "chrono")]
impl Serialize for Vec<chrono::Duration> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.len()))?;
        for e in self {
            let nanos = e.as_nanos() as i64;
            seq.serialize_element(&nanos)?;
        }
        seq.end()
    }
}