Change error return type to DateTimeRangeError
This commit is contained in:
parent
3daa3a7ef4
commit
60553541ed
2 changed files with 24 additions and 24 deletions
|
@ -209,4 +209,8 @@
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Fixes build errors that occur when all default features are disabled.
|
- Fixes build errors that occur when all default features are disabled.
|
||||||
- Fixes more cases of a bug when ZIP64 magic bytes occur in filenames.
|
- 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.
|
42
src/types.rs
42
src/types.rs
|
@ -53,6 +53,7 @@ mod atomic {
|
||||||
|
|
||||||
#[cfg(feature = "time")]
|
#[cfg(feature = "time")]
|
||||||
use time::{error::ComponentRange, Date, Month, OffsetDateTime, PrimitiveDateTime, Time};
|
use time::{error::ComponentRange, Date, Month, OffsetDateTime, PrimitiveDateTime, Time};
|
||||||
|
use crate::result::DateTimeRangeError;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum System {
|
pub enum System {
|
||||||
|
@ -115,33 +116,31 @@ impl arbitrary::Arbitrary<'_> for DateTime {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
#[allow(clippy::result_unit_err)]
|
|
||||||
impl TryFrom<NaiveDateTime> for DateTime {
|
impl TryFrom<NaiveDateTime> for DateTime {
|
||||||
type Error = ();
|
type Error = DateTimeRangeError;
|
||||||
|
|
||||||
fn try_from(value: NaiveDateTime) -> Result<Self, Self::Error> {
|
fn try_from(value: NaiveDateTime) -> Result<Self, Self::Error> {
|
||||||
DateTime::from_date_and_time(
|
DateTime::from_date_and_time(
|
||||||
value.year().try_into().map_err(|_| ())?,
|
value.year().try_into()?,
|
||||||
value.month().try_into().map_err(|_| ())?,
|
value.month().try_into()?,
|
||||||
value.day().try_into().map_err(|_| ())?,
|
value.day().try_into()?,
|
||||||
value.hour().try_into().map_err(|_| ())?,
|
value.hour().try_into()?,
|
||||||
value.minute().try_into().map_err(|_| ())?,
|
value.minute().try_into()?,
|
||||||
value.second().try_into().map_err(|_| ())?,
|
value.second().try_into()?,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
#[allow(clippy::result_unit_err)]
|
|
||||||
impl TryInto<NaiveDateTime> for DateTime {
|
impl TryInto<NaiveDateTime> for DateTime {
|
||||||
type Error = ();
|
type Error = DateTimeRangeError;
|
||||||
|
|
||||||
fn try_into(self) -> Result<NaiveDateTime, Self::Error> {
|
fn try_into(self) -> Result<NaiveDateTime, Self::Error> {
|
||||||
let date = NaiveDate::from_ymd_opt(self.year.into(), self.month.into(), self.day.into())
|
let date = NaiveDate::from_ymd_opt(self.year.into(), self.month.into(), self.day.into())
|
||||||
.ok_or(())?;
|
.ok_or(DateTimeRangeError)?;
|
||||||
let time =
|
let time =
|
||||||
NaiveTime::from_hms_opt(self.hour.into(), self.minute.into(), self.second.into())
|
NaiveTime::from_hms_opt(self.hour.into(), self.minute.into(), self.second.into())
|
||||||
.ok_or(())?;
|
.ok_or(DateTimeRangeError)?;
|
||||||
Ok(NaiveDateTime::new(date, time))
|
Ok(NaiveDateTime::new(date, time))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,7 +188,6 @@ impl DateTime {
|
||||||
/// * hour: [0, 23]
|
/// * hour: [0, 23]
|
||||||
/// * minute: [0, 59]
|
/// * minute: [0, 59]
|
||||||
/// * second: [0, 60]
|
/// * second: [0, 60]
|
||||||
#[allow(clippy::result_unit_err)]
|
|
||||||
pub fn from_date_and_time(
|
pub fn from_date_and_time(
|
||||||
year: u16,
|
year: u16,
|
||||||
month: u8,
|
month: u8,
|
||||||
|
@ -197,7 +195,7 @@ impl DateTime {
|
||||||
hour: u8,
|
hour: u8,
|
||||||
minute: u8,
|
minute: u8,
|
||||||
second: u8,
|
second: u8,
|
||||||
) -> Result<DateTime, ()> {
|
) -> Result<DateTime, DateTimeRangeError> {
|
||||||
if (1980..=2107).contains(&year)
|
if (1980..=2107).contains(&year)
|
||||||
&& (1..=12).contains(&month)
|
&& (1..=12).contains(&month)
|
||||||
&& (1..=31).contains(&day)
|
&& (1..=31).contains(&day)
|
||||||
|
@ -214,7 +212,7 @@ impl DateTime {
|
||||||
second,
|
second,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err(DateTimeRangeError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,10 +233,9 @@ impl DateTime {
|
||||||
/// Converts a OffsetDateTime object to a DateTime
|
/// Converts a OffsetDateTime object to a DateTime
|
||||||
///
|
///
|
||||||
/// Returns `Err` when this object is out of bounds
|
/// Returns `Err` when this object is out of bounds
|
||||||
#[allow(clippy::result_unit_err)]
|
|
||||||
#[deprecated(note = "use `DateTime::try_from()`")]
|
#[deprecated(note = "use `DateTime::try_from()`")]
|
||||||
pub fn from_time(dt: OffsetDateTime) -> Result<DateTime, ()> {
|
pub fn from_time(dt: OffsetDateTime) -> Result<DateTime, DateTimeRangeError> {
|
||||||
dt.try_into().map_err(|_err| ())
|
dt.try_into().map_err(|_err| DateTimeRangeError)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the time portion of this datetime in the msdos representation
|
/// Gets the time portion of this datetime in the msdos representation
|
||||||
|
@ -312,22 +309,21 @@ impl DateTime {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "time")]
|
#[cfg(feature = "time")]
|
||||||
#[allow(clippy::result_unit_err)]
|
|
||||||
impl TryFrom<OffsetDateTime> for DateTime {
|
impl TryFrom<OffsetDateTime> for DateTime {
|
||||||
type Error = ();
|
type Error = DateTimeRangeError;
|
||||||
|
|
||||||
fn try_from(dt: OffsetDateTime) -> Result<Self, Self::Error> {
|
fn try_from(dt: OffsetDateTime) -> Result<Self, Self::Error> {
|
||||||
if dt.year() >= 1980 && dt.year() <= 2107 {
|
if dt.year() >= 1980 && dt.year() <= 2107 {
|
||||||
Ok(DateTime {
|
Ok(DateTime {
|
||||||
year: (dt.year()) as u16,
|
year: (dt.year()).try_into()?,
|
||||||
month: (dt.month()) as u8,
|
month: (dt.month()).try_into()?,
|
||||||
day: dt.day(),
|
day: dt.day(),
|
||||||
hour: dt.hour(),
|
hour: dt.hour(),
|
||||||
minute: dt.minute(),
|
minute: dt.minute(),
|
||||||
second: dt.second(),
|
second: dt.second(),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err(DateTimeRangeError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue