From 60553541edcc8b0f8c758802442d69d00868a749 Mon Sep 17 00:00:00 2001 From: Chris Hennick Date: Thu, 1 Jun 2023 12:41:54 -0700 Subject: [PATCH] Change error return type to `DateTimeRangeError` --- CHANGELOG.md | 6 +++++- src/types.rs | 42 +++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f48ba12..8a799138 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -209,4 +209,8 @@ ### Fixed - Fixes build errors that occur when all default features are disabled. - - Fixes more cases of a bug when ZIP64 magic bytes occur in filenames. \ No newline at end of file + - Fixes more cases of a bug when ZIP64 magic bytes occur in filenames. + +## [0.10.1] + + - Date and time conversion methods now return `DateTimeRangeError` rather than `()` on error. \ No newline at end of file diff --git a/src/types.rs b/src/types.rs index 8af87d0b..b3ec71d7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -53,6 +53,7 @@ mod atomic { #[cfg(feature = "time")] use time::{error::ComponentRange, Date, Month, OffsetDateTime, PrimitiveDateTime, Time}; +use crate::result::DateTimeRangeError; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum System { @@ -115,33 +116,31 @@ impl arbitrary::Arbitrary<'_> for DateTime { } #[cfg(feature = "chrono")] -#[allow(clippy::result_unit_err)] impl TryFrom for DateTime { - type Error = (); + type Error = DateTimeRangeError; fn try_from(value: NaiveDateTime) -> Result { DateTime::from_date_and_time( - value.year().try_into().map_err(|_| ())?, - value.month().try_into().map_err(|_| ())?, - value.day().try_into().map_err(|_| ())?, - value.hour().try_into().map_err(|_| ())?, - value.minute().try_into().map_err(|_| ())?, - value.second().try_into().map_err(|_| ())?, + value.year().try_into()?, + value.month().try_into()?, + value.day().try_into()?, + value.hour().try_into()?, + value.minute().try_into()?, + value.second().try_into()?, ) } } #[cfg(feature = "chrono")] -#[allow(clippy::result_unit_err)] impl TryInto for DateTime { - type Error = (); + type Error = DateTimeRangeError; fn try_into(self) -> Result { let date = NaiveDate::from_ymd_opt(self.year.into(), self.month.into(), self.day.into()) - .ok_or(())?; + .ok_or(DateTimeRangeError)?; let time = NaiveTime::from_hms_opt(self.hour.into(), self.minute.into(), self.second.into()) - .ok_or(())?; + .ok_or(DateTimeRangeError)?; Ok(NaiveDateTime::new(date, time)) } } @@ -189,7 +188,6 @@ impl DateTime { /// * hour: [0, 23] /// * minute: [0, 59] /// * second: [0, 60] - #[allow(clippy::result_unit_err)] pub fn from_date_and_time( year: u16, month: u8, @@ -197,7 +195,7 @@ impl DateTime { hour: u8, minute: u8, second: u8, - ) -> Result { + ) -> Result { if (1980..=2107).contains(&year) && (1..=12).contains(&month) && (1..=31).contains(&day) @@ -214,7 +212,7 @@ impl DateTime { second, }) } else { - Err(()) + Err(DateTimeRangeError) } } @@ -235,10 +233,9 @@ impl DateTime { /// Converts a OffsetDateTime object to a DateTime /// /// Returns `Err` when this object is out of bounds - #[allow(clippy::result_unit_err)] #[deprecated(note = "use `DateTime::try_from()`")] - pub fn from_time(dt: OffsetDateTime) -> Result { - dt.try_into().map_err(|_err| ()) + pub fn from_time(dt: OffsetDateTime) -> Result { + dt.try_into().map_err(|_err| DateTimeRangeError) } /// Gets the time portion of this datetime in the msdos representation @@ -312,22 +309,21 @@ impl DateTime { } #[cfg(feature = "time")] -#[allow(clippy::result_unit_err)] impl TryFrom for DateTime { - type Error = (); + type Error = DateTimeRangeError; fn try_from(dt: OffsetDateTime) -> Result { if dt.year() >= 1980 && dt.year() <= 2107 { Ok(DateTime { - year: (dt.year()) as u16, - month: (dt.month()) as u8, + year: (dt.year()).try_into()?, + month: (dt.month()).try_into()?, day: dt.day(), hour: dt.hour(), minute: dt.minute(), second: dt.second(), }) } else { - Err(()) + Err(DateTimeRangeError) } } }