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
115
116
117
118
119
// Take a look at the license at the top of the repository in the LICENSE file.

use serde::{
    de::{Deserialize, Deserializer},
    ser::{Serialize, Serializer},
};

use crate::ClockTime;

impl Serialize for ClockTime {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use std::ops::Deref;
        self.deref().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ClockTime {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        skip_assert_initialized!();
        u64::deserialize(deserializer).and_then(|value| {
            ClockTime::try_from(value).map_err(|_| {
                use serde::de::{Error, Unexpected};
                D::Error::invalid_value(Unexpected::Unsigned(value), &"valid `ClockTime`")
            })
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::ClockTime;

    #[test]
    fn test_serialize() {
        crate::init().unwrap();

        // Some
        let clocktime = Some(ClockTime::from_nseconds(42_123_456_789));

        let pretty_config = ron::ser::PrettyConfig::new().new_line("".to_string());

        let res = ron::ser::to_string_pretty(&clocktime, pretty_config.clone());
        assert_eq!(Ok("Some(42123456789)".to_owned()), res);

        let res = serde_json::to_string(&clocktime).unwrap();
        assert_eq!("42123456789".to_owned(), res);

        // None
        let clocktime = ClockTime::NONE;

        let res = ron::ser::to_string_pretty(&clocktime, pretty_config);
        assert_eq!(Ok("None".to_owned()), res);

        let res = serde_json::to_string(&clocktime).unwrap();
        assert_eq!("null".to_owned(), res);
    }

    #[test]
    fn test_deserialize() {
        crate::init().unwrap();

        // Some
        let clocktime_ron = "Some(42123456789)";
        let clocktime: Option<ClockTime> = ron::de::from_str(clocktime_ron).unwrap();
        let clocktime = clocktime.unwrap();
        assert_eq!(clocktime.seconds(), 42);
        assert_eq!(clocktime.mseconds(), 42_123);
        assert_eq!(clocktime.useconds(), 42_123_456);
        assert_eq!(clocktime.nseconds(), 42_123_456_789);

        let clocktime_json = "42123456789";
        let clocktime: Option<ClockTime> = serde_json::from_str(clocktime_json).unwrap();
        let clocktime = clocktime.unwrap();
        assert_eq!(clocktime.seconds(), 42);
        assert_eq!(clocktime.mseconds(), 42_123);
        assert_eq!(clocktime.useconds(), 42_123_456);
        assert_eq!(clocktime.nseconds(), 42_123_456_789);

        // None
        let clocktime_ron = "None";
        let clocktime: Option<ClockTime> = ron::de::from_str(clocktime_ron).unwrap();
        assert!(clocktime.is_none());

        let clocktime_json = "null";
        let clocktime: Option<ClockTime> = serde_json::from_str(clocktime_json).unwrap();
        assert!(clocktime.is_none());
        assert!(clocktime.is_none());
    }

    #[test]
    fn test_serde_roundtrip() {
        crate::init().unwrap();

        // Direct
        let clocktime = ClockTime::from_nseconds(42_123_456_789);
        let clocktime_ser = ron::ser::to_string(&clocktime).unwrap();
        let clocktime: ClockTime = ron::de::from_str(clocktime_ser.as_str()).unwrap();
        assert_eq!(clocktime.seconds(), 42);
        assert_eq!(clocktime.mseconds(), 42_123);
        assert_eq!(clocktime.useconds(), 42_123_456);
        assert_eq!(clocktime.nseconds(), 42_123_456_789);

        // Some
        let clocktime = Some(ClockTime::from_nseconds(42_123_456_789));
        let clocktime_ser = ron::ser::to_string(&clocktime).unwrap();
        let clocktime: Option<ClockTime> = ron::de::from_str(clocktime_ser.as_str()).unwrap();
        let clocktime = clocktime.unwrap();
        assert_eq!(clocktime.seconds(), 42);
        assert_eq!(clocktime.mseconds(), 42_123);
        assert_eq!(clocktime.useconds(), 42_123_456);
        assert_eq!(clocktime.nseconds(), 42_123_456_789);

        // None
        let clocktime = ClockTime::NONE;
        let clocktime_ser = ron::ser::to_string(&clocktime).unwrap();
        let clocktime: Option<ClockTime> = ron::de::from_str(clocktime_ser.as_str()).unwrap();
        assert!(clocktime.is_none());
    }
}