2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
use serde::de;
|
|
|
|
use serde::de::{Deserialize, Deserializer, Visitor};
|
|
|
|
use serde::ser::{Serialize, Serializer};
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::ClockTime;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
impl<'a> Serialize for ClockTime {
|
|
|
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
|
|
|
match self.nanoseconds() {
|
|
|
|
Some(ref value) => serializer.serialize_some(value),
|
|
|
|
None => serializer.serialize_none(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ClockTimeVisitor;
|
|
|
|
impl<'de> Visitor<'de> for ClockTimeVisitor {
|
|
|
|
type Value = ClockTime;
|
|
|
|
|
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
formatter.write_str("an optional u64 ClockTime with ns precision")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
2018-10-11 08:25:28 +00:00
|
|
|
u64::deserialize(deserializer).map(ClockTime::from_nseconds)
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
|
|
|
|
Ok(ClockTime(None))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for ClockTime {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2018-07-16 19:46:10 +00:00
|
|
|
deserializer.deserialize_option(ClockTimeVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-11-21 13:46:48 +00:00
|
|
|
use crate::ClockTime;
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialize() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
// Some
|
|
|
|
let clocktime = ClockTime::from_nseconds(42_123_456_789);
|
|
|
|
|
2021-01-01 19:35:43 +00:00
|
|
|
let pretty_config = ron::ser::PrettyConfig::new().with_new_line("".to_string());
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
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() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-16 19:46:10 +00:00
|
|
|
|
|
|
|
// Some
|
|
|
|
let clocktime_ron = "Some(42123456789)";
|
|
|
|
let clocktime: ClockTime = ron::de::from_str(clocktime_ron).unwrap();
|
|
|
|
assert_eq!(clocktime.seconds(), Some(42));
|
|
|
|
assert_eq!(clocktime.mseconds(), Some(42_123));
|
|
|
|
assert_eq!(clocktime.useconds(), Some(42_123_456));
|
|
|
|
assert_eq!(clocktime.nseconds(), Some(42_123_456_789));
|
|
|
|
|
|
|
|
let clocktime_json = "42123456789";
|
|
|
|
let clocktime: ClockTime = serde_json::from_str(clocktime_json).unwrap();
|
|
|
|
assert_eq!(clocktime.seconds(), Some(42));
|
|
|
|
assert_eq!(clocktime.mseconds(), Some(42_123));
|
|
|
|
assert_eq!(clocktime.useconds(), Some(42_123_456));
|
|
|
|
assert_eq!(clocktime.nseconds(), Some(42_123_456_789));
|
|
|
|
|
|
|
|
// None
|
|
|
|
let clocktime_ron = "None";
|
|
|
|
let clocktime: ClockTime = ron::de::from_str(clocktime_ron).unwrap();
|
|
|
|
assert_eq!(clocktime.nseconds(), None);
|
|
|
|
|
|
|
|
let clocktime_json = "null";
|
|
|
|
let clocktime: ClockTime = serde_json::from_str(clocktime_json).unwrap();
|
|
|
|
assert_eq!(clocktime.nseconds(), None);
|
|
|
|
}
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serde_roundtrip() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2018-07-29 16:18:24 +00:00
|
|
|
|
|
|
|
// Some
|
|
|
|
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(), Some(42));
|
|
|
|
assert_eq!(clocktime.mseconds(), Some(42_123));
|
|
|
|
assert_eq!(clocktime.useconds(), Some(42_123_456));
|
|
|
|
assert_eq!(clocktime.nseconds(), Some(42_123_456_789));
|
|
|
|
|
|
|
|
// None
|
|
|
|
let clocktime = ClockTime(None);
|
|
|
|
let clocktime_ser = ron::ser::to_string(&clocktime).unwrap();
|
|
|
|
let clocktime: ClockTime = ron::de::from_str(clocktime_ser.as_str()).unwrap();
|
|
|
|
assert_eq!(clocktime.nseconds(), None);
|
|
|
|
}
|
2018-07-16 19:46:10 +00:00
|
|
|
}
|