Skip to content

Commit 145f90d

Browse files
authored
Use nicer Debug implementations (#509)
1 parent 684e82d commit 145f90d

File tree

6 files changed

+34
-27
lines changed

6 files changed

+34
-27
lines changed

src/date.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ pub struct Date {
4141
value: i32,
4242
}
4343

44-
impl fmt::Debug for Date {
45-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
46-
f.debug_struct("Date")
47-
.field("year", &self.year())
48-
.field("ordinal", &self.ordinal())
49-
.finish()
50-
}
51-
}
52-
5344
impl Date {
5445
/// The minimum valid `Date`.
5546
///
@@ -983,6 +974,12 @@ impl fmt::Display for Date {
983974
}
984975
}
985976
}
977+
978+
impl fmt::Debug for Date {
979+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
980+
fmt::Display::fmt(self, f)
981+
}
982+
}
986983
// endregion formatting & parsing
987984

988985
// region: trait impls

src/offset_date_time.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const UNIX_EPOCH_JULIAN_DAY: i32 = Date::__from_ordinal_date_unchecked(1970, 1).
2727
/// A [`PrimitiveDateTime`] with a [`UtcOffset`].
2828
///
2929
/// All comparisons are performed using the UTC time.
30-
#[derive(Debug, Clone, Copy, Eq)]
30+
#[derive(Clone, Copy, Eq)]
3131
pub struct OffsetDateTime {
3232
/// The [`PrimitiveDateTime`], which is _always_ in the stored offset.
3333
pub(crate) local_datetime: PrimitiveDateTime,
@@ -1132,6 +1132,12 @@ impl fmt::Display for OffsetDateTime {
11321132
write!(f, "{} {} {}", self.date(), self.time(), self.offset)
11331133
}
11341134
}
1135+
1136+
impl fmt::Debug for OffsetDateTime {
1137+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1138+
fmt::Display::fmt(self, f)
1139+
}
1140+
}
11351141
// endregion formatting & parsing
11361142

11371143
// region: trait impls

src/primitive_date_time.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::parsing::Parsable;
1313
use crate::{error, util, Date, Duration, Month, OffsetDateTime, Time, UtcOffset, Weekday};
1414

1515
/// Combined date and time.
16-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
16+
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
1717
pub struct PrimitiveDateTime {
1818
#[allow(clippy::missing_docs_in_private_items)]
1919
pub(crate) date: Date,
@@ -821,6 +821,12 @@ impl fmt::Display for PrimitiveDateTime {
821821
write!(f, "{} {}", self.date, self.time)
822822
}
823823
}
824+
825+
impl fmt::Debug for PrimitiveDateTime {
826+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
827+
fmt::Display::fmt(self, f)
828+
}
829+
}
824830
// endregion formatting & parsing
825831

826832
// region: trait impls

src/time.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ pub struct Time {
4242
padding: Padding,
4343
}
4444

45-
impl fmt::Debug for Time {
46-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
f.debug_struct("Time")
48-
.field("hour", &self.hour)
49-
.field("minute", &self.minute)
50-
.field("second", &self.second)
51-
.field("nanosecond", &self.nanosecond)
52-
.finish()
53-
}
54-
}
55-
5645
impl Time {
5746
/// Create a `Time` that is exactly midnight.
5847
///
@@ -656,6 +645,12 @@ impl fmt::Display for Time {
656645
)
657646
}
658647
}
648+
649+
impl fmt::Debug for Time {
650+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
651+
fmt::Display::fmt(self, f)
652+
}
653+
}
659654
// endregion formatting & parsing
660655

661656
// region: trait impls

src/utc_offset.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::OffsetDateTime;
2020
/// This struct can store values up to ±23:59:59. If you need support outside this range, please
2121
/// file an issue with your use case.
2222
// All three components _must_ have the same sign.
23-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
23+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2424
pub struct UtcOffset {
2525
#[allow(clippy::missing_docs_in_private_items)]
2626
hours: i8,
@@ -326,6 +326,12 @@ impl fmt::Display for UtcOffset {
326326
)
327327
}
328328
}
329+
330+
impl fmt::Debug for UtcOffset {
331+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
332+
fmt::Display::fmt(self, f)
333+
}
334+
}
329335
// endregion formatting & parsing
330336

331337
impl Neg for UtcOffset {

tests/integration/date.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use time::{util, Date, Duration, Month, Weekday};
88

99
#[test]
1010
fn debug() {
11-
assert_eq!(
12-
format!("{:?}", date!(2020 - 02 - 03)),
13-
"Date { year: 2020, ordinal: 34 }"
14-
);
11+
assert_eq!(format!("{:?}", date!(2020 - 02 - 03)), "2020-02-03");
1512
}
1613

1714
#[test]

0 commit comments

Comments
 (0)