From e56756d5e4e60f7f5361c8b50a44c2ff779f1cdd Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Wed, 18 Oct 2023 14:54:21 +0200 Subject: [PATCH] Implement Debug for Time --- src/lib.rs | 171 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 132 insertions(+), 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 75629cf..8a4ec17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ use std::cmp::max; use std::cmp::min; use std::cmp::Ordering; use std::error::Error; -use std::fmt::Debug; +use std::fmt::{Debug, Formatter}; use std::fmt::Display; use std::ops::Add; use std::ops::AddAssign; @@ -73,7 +73,7 @@ use serde::Serialize; /// /// Low overhead time representation. Internally represented as milliseconds. #[derive( - Eq, PartialEq, Hash, Ord, PartialOrd, Copy, Clone, Debug, Default, Serialize, Deref, From, Into, +Eq, PartialEq, Hash, Ord, PartialOrd, Copy, Clone, Default, Serialize, Deref, From, Into, )] pub struct Time(i64); impl Time { @@ -132,7 +132,7 @@ impl Time { // casting to u32 is safe here because it is guaranteed that the value is in // 0..1_000_000_000 #[allow(clippy::cast_possible_truncation)] - let nanos = if nanos.is_negative() { + let nanos = if nanos.is_negative() { 1_000_000_000 - nanos.unsigned_abs() } else { nanos.unsigned_abs() @@ -263,8 +263,8 @@ impl Display for Time { /// Allows deserializing from RFC 3339 strings and unsigned integers. impl<'de> Deserialize<'de> for Time { fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, + where + D: serde::Deserializer<'de>, { deserializer.deserialize_any(TimeVisitor) } @@ -279,15 +279,15 @@ impl<'de> Visitor<'de> for TimeVisitor { } fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, + where + E: serde::de::Error, { Time::parse_from_rfc3339(v).map_err(|e| E::custom(e.to_string())) } fn visit_u64(self, v: u64) -> Result - where - E: serde::de::Error, + where + E: serde::de::Error, { i64::try_from(v) .map_err(|e| E::custom(e.to_string())) @@ -295,8 +295,8 @@ impl<'de> Visitor<'de> for TimeVisitor { } fn visit_newtype_struct(self, deserializer: D) -> Result - where - D: serde::Deserializer<'de>, + where + D: serde::Deserializer<'de>, { // expecting an unsigned integer inside the newtype struct, but technically also // allowing strings @@ -317,6 +317,40 @@ impl From