Bug fixes for chrono integration
This commit is contained in:
parent
b47d6419b8
commit
630ca3fa0f
3 changed files with 14 additions and 10 deletions
|
@ -54,8 +54,7 @@ The features available are:
|
||||||
is the most effective `deflate` implementation available.
|
is the most effective `deflate` implementation available.
|
||||||
* `bzip2`: Enables the BZip2 compression algorithm.
|
* `bzip2`: Enables the BZip2 compression algorithm.
|
||||||
* `time`: Enables features using the [time](https://github.com/rust-lang-deprecated/time) crate.
|
* `time`: Enables features using the [time](https://github.com/rust-lang-deprecated/time) crate.
|
||||||
* `chrono`: Enables converting last-modified `zip_next::DateTime` to and from `chrono::NaiveDateTime` and from
|
* `chrono`: Enables converting last-modified `zip_next::DateTime` to and from `chrono::NaiveDateTime`.
|
||||||
`chrono::DateTime`.
|
|
||||||
* `zstd`: Enables the Zstandard compression algorithm.
|
* `zstd`: Enables the Zstandard compression algorithm.
|
||||||
|
|
||||||
By default `aes-crypto`, `deflate`, `deflate-zlib-ng`, `deflate-zopfli`, `bzip2`, `time` and `zstd` are enabled.
|
By default `aes-crypto`, `deflate`, `deflate-zlib-ng`, `deflate-zopfli`, `bzip2`, `time` and `zstd` are enabled.
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::IntoInnerError;
|
use std::io::IntoInnerError;
|
||||||
|
use std::num::TryFromIntError;
|
||||||
|
|
||||||
/// Generic result type with ZipError as its error variant
|
/// Generic result type with ZipError as its error variant
|
||||||
pub type ZipResult<T> = Result<T, ZipError>;
|
pub type ZipResult<T> = Result<T, ZipError>;
|
||||||
|
@ -93,6 +94,13 @@ impl From<ZipError> for io::Error {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DateTimeRangeError;
|
pub struct DateTimeRangeError;
|
||||||
|
|
||||||
|
// TryFromIntError is also an out-of-range error.
|
||||||
|
impl From<TryFromIntError> for DateTimeRangeError {
|
||||||
|
fn from(_value: TryFromIntError) -> Self {
|
||||||
|
DateTimeRangeError
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for DateTimeRangeError {
|
impl fmt::Display for DateTimeRangeError {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
|
|
13
src/types.rs
13
src/types.rs
|
@ -120,13 +120,10 @@ impl arbitrary::Arbitrary<'_> for DateTime {
|
||||||
|
|
||||||
#[cfg(feature = "chrono")]
|
#[cfg(feature = "chrono")]
|
||||||
#[allow(clippy::result_unit_err)]
|
#[allow(clippy::result_unit_err)]
|
||||||
impl<T> TryFrom<T> for DateTime
|
impl TryFrom<NaiveDateTime> for DateTime {
|
||||||
where
|
type Error = DateTimeRangeError;
|
||||||
T: Datelike + Timelike,
|
|
||||||
{
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn try_from(value: T) -> Result<Self, Self::Error> {
|
fn try_from(value: NaiveDateTime) -> Result<Self, Self::Error> {
|
||||||
Ok(DateTime::from_date_and_time(
|
Ok(DateTime::from_date_and_time(
|
||||||
value.year().try_into()?,
|
value.year().try_into()?,
|
||||||
value.month().try_into()?,
|
value.month().try_into()?,
|
||||||
|
@ -202,7 +199,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)
|
||||||
|
@ -219,7 +216,7 @@ impl DateTime {
|
||||||
second,
|
second,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
Err(DateTimeRangeError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue