Skip to content

Use nicer Debug implementations #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,6 @@ pub struct Date {
value: i32,
}

impl fmt::Debug for Date {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_struct("Date")
.field("year", &self.year())
.field("ordinal", &self.ordinal())
.finish()
}
}

impl Date {
/// The minimum valid `Date`.
///
Expand Down Expand Up @@ -983,6 +974,12 @@ impl fmt::Display for Date {
}
}
}

impl fmt::Debug for Date {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
fmt::Display::fmt(self, f)
}
}
// endregion formatting & parsing

// region: trait impls
Expand Down
8 changes: 7 additions & 1 deletion src/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const UNIX_EPOCH_JULIAN_DAY: i32 = Date::__from_ordinal_date_unchecked(1970, 1).
/// A [`PrimitiveDateTime`] with a [`UtcOffset`].
///
/// All comparisons are performed using the UTC time.
#[derive(Debug, Clone, Copy, Eq)]
#[derive(Clone, Copy, Eq)]
pub struct OffsetDateTime {
/// The [`PrimitiveDateTime`], which is _always_ in the stored offset.
pub(crate) local_datetime: PrimitiveDateTime,
Expand Down Expand Up @@ -1132,6 +1132,12 @@ impl fmt::Display for OffsetDateTime {
write!(f, "{} {} {}", self.date(), self.time(), self.offset)
}
}

impl fmt::Debug for OffsetDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
// endregion formatting & parsing

// region: trait impls
Expand Down
8 changes: 7 additions & 1 deletion src/primitive_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::parsing::Parsable;
use crate::{error, util, Date, Duration, Month, OffsetDateTime, Time, UtcOffset, Weekday};

/// Combined date and time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PrimitiveDateTime {
#[allow(clippy::missing_docs_in_private_items)]
pub(crate) date: Date,
Expand Down Expand Up @@ -821,6 +821,12 @@ impl fmt::Display for PrimitiveDateTime {
write!(f, "{} {}", self.date, self.time)
}
}

impl fmt::Debug for PrimitiveDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
// endregion formatting & parsing

// region: trait impls
Expand Down
17 changes: 6 additions & 11 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ pub struct Time {
padding: Padding,
}

impl fmt::Debug for Time {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Time")
.field("hour", &self.hour)
.field("minute", &self.minute)
.field("second", &self.second)
.field("nanosecond", &self.nanosecond)
.finish()
}
}

impl Time {
/// Create a `Time` that is exactly midnight.
///
Expand Down Expand Up @@ -656,6 +645,12 @@ impl fmt::Display for Time {
)
}
}

impl fmt::Debug for Time {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
// endregion formatting & parsing

// region: trait impls
Expand Down
8 changes: 7 additions & 1 deletion src/utc_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::OffsetDateTime;
/// This struct can store values up to ±23:59:59. If you need support outside this range, please
/// file an issue with your use case.
// All three components _must_ have the same sign.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UtcOffset {
#[allow(clippy::missing_docs_in_private_items)]
hours: i8,
Expand Down Expand Up @@ -326,6 +326,12 @@ impl fmt::Display for UtcOffset {
)
}
}

impl fmt::Debug for UtcOffset {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
// endregion formatting & parsing

impl Neg for UtcOffset {
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use time::{util, Date, Duration, Month, Weekday};

#[test]
fn debug() {
assert_eq!(
format!("{:?}", date!(2020 - 02 - 03)),
"Date { year: 2020, ordinal: 34 }"
);
assert_eq!(format!("{:?}", date!(2020 - 02 - 03)), "2020-02-03");
}

#[test]
Expand Down