1 // Copyright (C) 2018 François Laignel <fengalin@free.fr> 2 // 3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 6 // option. This file may not be copied, modified, or distributed 7 // except according to those terms. 8 9 use serde::de; 10 use serde::de::{Deserialize, Deserializer, Visitor}; 11 use serde::ser::{Serialize, Serializer}; 12 13 use std::fmt; 14 15 use ClockTime; 16 17 impl<'a> Serialize for ClockTime { serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>18 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 19 match self.nanoseconds() { 20 Some(ref value) => serializer.serialize_some(value), 21 None => serializer.serialize_none(), 22 } 23 } 24 } 25 26 struct ClockTimeVisitor; 27 impl<'de> Visitor<'de> for ClockTimeVisitor { 28 type Value = ClockTime; 29 expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result30 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { 31 formatter.write_str("an optional u64 ClockTime with ns precision") 32 } 33 visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de>,34 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> 35 where 36 D: Deserializer<'de>, 37 { 38 u64::deserialize(deserializer).map(ClockTime::from_nseconds) 39 } 40 visit_none<E: de::Error>(self) -> Result<Self::Value, E>41 fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> { 42 Ok(ClockTime(None)) 43 } 44 } 45 46 impl<'de> Deserialize<'de> for ClockTime { deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>47 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 48 deserializer.deserialize_option(ClockTimeVisitor) 49 } 50 } 51 52 #[cfg(test)] 53 mod tests { 54 extern crate ron; 55 extern crate serde_json; 56 57 use ClockTime; 58 59 #[test] test_serialize()60 fn test_serialize() { 61 ::init().unwrap(); 62 63 // Some 64 let clocktime = ClockTime::from_nseconds(42_123_456_789); 65 66 let mut pretty_config = ron::ser::PrettyConfig::default(); 67 pretty_config.new_line = "".to_string(); 68 69 let res = ron::ser::to_string_pretty(&clocktime, pretty_config.clone()); 70 assert_eq!(Ok("Some(42123456789)".to_owned()), res); 71 72 let res = serde_json::to_string(&clocktime).unwrap(); 73 assert_eq!("42123456789".to_owned(), res); 74 75 // None 76 let clocktime = ClockTime(None); 77 78 let res = ron::ser::to_string_pretty(&clocktime, pretty_config); 79 assert_eq!(Ok("None".to_owned()), res); 80 81 let res = serde_json::to_string(&clocktime).unwrap(); 82 assert_eq!("null".to_owned(), res); 83 } 84 85 #[test] test_deserialize()86 fn test_deserialize() { 87 ::init().unwrap(); 88 89 // Some 90 let clocktime_ron = "Some(42123456789)"; 91 let clocktime: ClockTime = ron::de::from_str(clocktime_ron).unwrap(); 92 assert_eq!(clocktime.seconds(), Some(42)); 93 assert_eq!(clocktime.mseconds(), Some(42_123)); 94 assert_eq!(clocktime.useconds(), Some(42_123_456)); 95 assert_eq!(clocktime.nseconds(), Some(42_123_456_789)); 96 97 let clocktime_json = "42123456789"; 98 let clocktime: ClockTime = serde_json::from_str(clocktime_json).unwrap(); 99 assert_eq!(clocktime.seconds(), Some(42)); 100 assert_eq!(clocktime.mseconds(), Some(42_123)); 101 assert_eq!(clocktime.useconds(), Some(42_123_456)); 102 assert_eq!(clocktime.nseconds(), Some(42_123_456_789)); 103 104 // None 105 let clocktime_ron = "None"; 106 let clocktime: ClockTime = ron::de::from_str(clocktime_ron).unwrap(); 107 assert_eq!(clocktime.nseconds(), None); 108 109 let clocktime_json = "null"; 110 let clocktime: ClockTime = serde_json::from_str(clocktime_json).unwrap(); 111 assert_eq!(clocktime.nseconds(), None); 112 } 113 114 #[test] test_serde_roundtrip()115 fn test_serde_roundtrip() { 116 ::init().unwrap(); 117 118 // Some 119 let clocktime = ClockTime::from_nseconds(42_123_456_789); 120 let clocktime_ser = ron::ser::to_string(&clocktime).unwrap(); 121 let clocktime: ClockTime = ron::de::from_str(clocktime_ser.as_str()).unwrap(); 122 assert_eq!(clocktime.seconds(), Some(42)); 123 assert_eq!(clocktime.mseconds(), Some(42_123)); 124 assert_eq!(clocktime.useconds(), Some(42_123_456)); 125 assert_eq!(clocktime.nseconds(), Some(42_123_456_789)); 126 127 // None 128 let clocktime = ClockTime(None); 129 let clocktime_ser = ron::ser::to_string(&clocktime).unwrap(); 130 let clocktime: ClockTime = ron::de::from_str(clocktime_ser.as_str()).unwrap(); 131 assert_eq!(clocktime.nseconds(), None); 132 } 133 } 134