From 983d846d4638141c218e11773e247c334d9f4552 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 25 Feb 2024 18:31:56 +0100 Subject: [PATCH] Fully convert `Parsed::set_*` to new error type --- src/format/parse.rs | 10 +- src/format/parsed.rs | 301 +++++++++++++++++++++++-------------------- 2 files changed, 163 insertions(+), 148 deletions(-) diff --git a/src/format/parse.rs b/src/format/parse.rs index 0c77d22cd5..b64bb9080a 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -14,7 +14,7 @@ use super::{Fixed, InternalFixed, InternalInternal, Item, Numeric, Pad, Parsed}; use super::{BAD_FORMAT, INVALID, OUT_OF_RANGE, TOO_LONG, TOO_SHORT}; use crate::{DateTime, Error, FixedOffset, Weekday}; -fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult<&mut Parsed> { +fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> Result<&mut Parsed, Error> { p.set_weekday(match v { 0 => Weekday::Sun, 1 => Weekday::Mon, @@ -23,11 +23,11 @@ fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult< 4 => Weekday::Thu, 5 => Weekday::Fri, 6 => Weekday::Sat, - _ => return Err(OUT_OF_RANGE), + _ => return Err(Error::InvalidArgument), }) } -fn set_weekday_with_number_from_monday(p: &mut Parsed, v: i64) -> ParseResult<&mut Parsed> { +fn set_weekday_with_number_from_monday(p: &mut Parsed, v: i64) -> Result<&mut Parsed, Error> { p.set_weekday(match v { 1 => Weekday::Mon, 2 => Weekday::Tue, @@ -36,7 +36,7 @@ fn set_weekday_with_number_from_monday(p: &mut Parsed, v: i64) -> ParseResult<&m 5 => Weekday::Fri, 6 => Weekday::Sat, 7 => Weekday::Sun, - _ => return Err(OUT_OF_RANGE), + _ => return Err(Error::InvalidArgument), }) } @@ -339,7 +339,7 @@ where Item::Numeric(ref spec, ref _pad) => { use super::Numeric::*; - type Setter = fn(&mut Parsed, i64) -> ParseResult<&mut Parsed>; + type Setter = fn(&mut Parsed, i64) -> Result<&mut Parsed, Error>; let (width, signed, set): (usize, bool, Setter) = match *spec { Year => (4, true, Parsed::set_year), diff --git a/src/format/parsed.rs b/src/format/parsed.rs index 0a91f8e821..b1807f35d8 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -146,12 +146,12 @@ pub struct Parsed { /// Checks if `old` is either empty or has the same value as `new` (i.e. "consistent"), /// and if it is empty, set `old` to `new` as well. #[inline] -fn set_if_consistent(old: &mut Option, new: T) -> ParseResult<()> { +fn set_if_consistent(old: &mut Option, new: T) -> Result<(), Error> { if let Some(ref old) = *old { if *old == new { Ok(()) } else { - Err(IMPOSSIBLE) + Err(Error::Inconsistent) } } else { *old = Some(new); @@ -167,12 +167,15 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is outside the range of an `i32`. + /// Returns [`Error::InvalidArgument`] if `value` is outside the range of an `i32`. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_year(&mut self, value: i64) -> ParseResult<&mut Self> { - set_if_consistent(&mut self.year, i32::try_from(value).map_err(|_| OUT_OF_RANGE)?)?; + pub fn set_year(&mut self, value: i64) -> Result<&mut Self, Error> { + set_if_consistent( + &mut self.year, + i32::try_from(value).map_err(|_| Error::InvalidArgument)?, + )?; Ok(self) } @@ -180,13 +183,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is negative or if it is greater than `i32::MAX`. + /// Returns [`Error::InvalidArgument`] if `value` is negative or if it is greater than `i32::MAX`. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_year_div_100(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_year_div_100(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=i32::MAX as i64).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.year_div_100, value as i32)?; Ok(self) @@ -203,13 +206,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is negative or if it is greater than 99. + /// Return [`Error::InvalidArgument`] if `value` is negative or if it is greater than 99. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_year_mod_100(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_year_mod_100(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..100).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.year_mod_100, value as i32)?; Ok(self) @@ -225,12 +228,15 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is outside the range of an `i32`. + /// Returns [`Error::InvalidArgument`] if `value` is outside the range of an `i32`. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_isoyear(&mut self, value: i64) -> ParseResult<&mut Self> { - set_if_consistent(&mut self.isoyear, i32::try_from(value).map_err(|_| OUT_OF_RANGE)?)?; + pub fn set_isoyear(&mut self, value: i64) -> Result<&mut Self, Error> { + set_if_consistent( + &mut self.isoyear, + i32::try_from(value).map_err(|_| Error::InvalidArgument)?, + )?; Ok(self) } @@ -241,13 +247,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is negative or if it is greater than `i32::MAX`. + /// Returns [`Error::InvalidArgument`] if `value` is negative or if it is greater than `i32::MAX`. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_isoyear_div_100(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_isoyear_div_100(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=i32::MAX as i64).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.isoyear_div_100, value as i32)?; Ok(self) @@ -267,13 +273,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is negative or if it is greater than 99. + /// Returns [`Error::InvalidArgument`] if `value` is negative or if it is greater than 99. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_isoyear_mod_100(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_isoyear_mod_100(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..100).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.isoyear_mod_100, value as i32)?; Ok(self) @@ -283,13 +289,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 1-12. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 1-12. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_month(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_month(&mut self, value: i64) -> Result<&mut Self, Error> { if !(1..=12).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.month, value as u32)?; Ok(self) @@ -301,13 +307,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 0-53. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 0-53. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_week_from_sun(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_week_from_sun(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=53).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.week_from_sun, value as u32)?; Ok(self) @@ -320,13 +326,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 0-53. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 0-53. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_week_from_mon(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_week_from_mon(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=53).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.week_from_mon, value as u32)?; Ok(self) @@ -338,13 +344,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 1-53. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 1-53. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_isoweek(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_isoweek(&mut self, value: i64) -> Result<&mut Self, Error> { if !(1..=53).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.isoweek, value as u32)?; Ok(self) @@ -354,9 +360,9 @@ impl Parsed { /// /// # Errors /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_weekday(&mut self, value: Weekday) -> ParseResult<&mut Self> { + pub fn set_weekday(&mut self, value: Weekday) -> Result<&mut Self, Error> { set_if_consistent(&mut self.weekday, value)?; Ok(self) } @@ -365,13 +371,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 1-366. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 1-366. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_ordinal(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_ordinal(&mut self, value: i64) -> Result<&mut Self, Error> { if !(1..=366).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.ordinal, value as u32)?; Ok(self) @@ -381,13 +387,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 1-31. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 1-31. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_day(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_day(&mut self, value: i64) -> Result<&mut Self, Error> { if !(1..=31).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.day, value as u32)?; Ok(self) @@ -399,9 +405,9 @@ impl Parsed { /// /// # Errors /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_ampm(&mut self, value: bool) -> ParseResult<&mut Self> { + pub fn set_ampm(&mut self, value: bool) -> Result<&mut Self, Error> { set_if_consistent(&mut self.hour_div_12, value as u32)?; Ok(self) } @@ -414,13 +420,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 1-12. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 1-12. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_hour12(&mut self, mut value: i64) -> ParseResult<&mut Self> { + pub fn set_hour12(&mut self, mut value: i64) -> Result<&mut Self, Error> { if !(1..=12).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } if value == 12 { value = 0 @@ -434,16 +440,16 @@ impl Parsed { /// /// # Errors /// - /// May return `OUT_OF_RANGE` if `value` is not in the range 0-23. + /// May return [`Error::InvalidArgument`] if `value` is not in the range 0-23. /// Currently only checks the value is not out of range for a `u32`. /// - /// Returns `IMPOSSIBLE` one of the fields was already set to a different value. + /// Returns [`Error::Inconsistent`] one of the fields was already set to a different value. #[inline] - pub fn set_hour(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_hour(&mut self, value: i64) -> Result<&mut Self, Error> { let (hour_div_12, hour_mod_12) = match value { hour @ 0..=11 => (0, hour as u32), hour @ 12..=23 => (1, hour as u32 - 12), - _ => return Err(OUT_OF_RANGE), + _ => return Err(Error::InvalidArgument), }; set_if_consistent(&mut self.hour_div_12, hour_div_12)?; set_if_consistent(&mut self.hour_mod_12, hour_mod_12)?; @@ -454,13 +460,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 0-59. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 0-59. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_minute(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_minute(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=59).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.minute, value as u32)?; Ok(self) @@ -472,13 +478,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 0-60. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 0-60. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_second(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_second(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=60).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.second, value as u32)?; Ok(self) @@ -490,13 +496,13 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is not in the range 0-999,999,999. + /// Returns [`Error::InvalidArgument`] if `value` is not in the range 0-999,999,999. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_nanosecond(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_nanosecond(&mut self, value: i64) -> Result<&mut Self, Error> { if !(0..=999_999_999).contains(&value) { - return Err(OUT_OF_RANGE); + return Err(Error::InvalidArgument); } set_if_consistent(&mut self.nanosecond, value as u32)?; Ok(self) @@ -509,9 +515,9 @@ impl Parsed { /// /// # Errors /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_timestamp(&mut self, value: i64) -> ParseResult<&mut Self> { + pub fn set_timestamp(&mut self, value: i64) -> Result<&mut Self, Error> { set_if_consistent(&mut self.timestamp, value)?; Ok(self) } @@ -522,12 +528,15 @@ impl Parsed { /// /// # Errors /// - /// Returns `OUT_OF_RANGE` if `value` is ouside the range of an `i32`. + /// Returns [`Error::InvalidArgument`] if `value` is ouside the range of an `i32`. /// - /// Returns `IMPOSSIBLE` if this field was already set to a different value. + /// Returns [`Error::Inconsistent`] if this field was already set to a different value. #[inline] - pub fn set_offset(&mut self, value: i64) -> ParseResult<&mut Self> { - set_if_consistent(&mut self.offset, i32::try_from(value).map_err(|_| OUT_OF_RANGE)?)?; + pub fn set_offset(&mut self, value: i64) -> Result<&mut Self, Error> { + set_if_consistent( + &mut self.offset, + i32::try_from(value).map_err(|_| Error::InvalidArgument)?, + )?; Ok(self) } @@ -1133,15 +1142,15 @@ mod tests { // year*, isoyear* let mut p = Parsed::default(); assert!(p.set_year(1987).is_ok()); - assert_eq!(p.set_year(1986), Err(IMPOSSIBLE)); - assert_eq!(p.set_year(1988), Err(IMPOSSIBLE)); + assert_eq!(p.set_year(1986), Err(Error::Inconsistent)); + assert_eq!(p.set_year(1988), Err(Error::Inconsistent)); assert!(p.set_year(1987).is_ok()); assert!(p.set_year_div_100(20).is_ok()); // independent to `year` - assert_eq!(p.set_year_div_100(21), Err(IMPOSSIBLE)); - assert_eq!(p.set_year_div_100(19), Err(IMPOSSIBLE)); + assert_eq!(p.set_year_div_100(21), Err(Error::Inconsistent)); + assert_eq!(p.set_year_div_100(19), Err(Error::Inconsistent)); assert!(p.set_year_mod_100(37).is_ok()); // ditto - assert_eq!(p.set_year_mod_100(38), Err(IMPOSSIBLE)); - assert_eq!(p.set_year_mod_100(36), Err(IMPOSSIBLE)); + assert_eq!(p.set_year_mod_100(38), Err(Error::Inconsistent)); + assert_eq!(p.set_year_mod_100(36), Err(Error::Inconsistent)); let mut p = Parsed::default(); assert!(p.set_year(0).is_ok()); @@ -1149,150 +1158,156 @@ mod tests { assert!(p.set_year_mod_100(0).is_ok()); let mut p = Parsed::default(); - assert_eq!(p.set_year_div_100(-1), Err(OUT_OF_RANGE)); - assert_eq!(p.set_year_mod_100(-1), Err(OUT_OF_RANGE)); + assert_eq!(p.set_year_div_100(-1), Err(Error::InvalidArgument)); + assert_eq!(p.set_year_mod_100(-1), Err(Error::InvalidArgument)); assert!(p.set_year(-1).is_ok()); - assert_eq!(p.set_year(-2), Err(IMPOSSIBLE)); - assert_eq!(p.set_year(0), Err(IMPOSSIBLE)); + assert_eq!(p.set_year(-2), Err(Error::Inconsistent)); + assert_eq!(p.set_year(0), Err(Error::Inconsistent)); let mut p = Parsed::default(); - assert_eq!(p.set_year_div_100(0x1_0000_0008), Err(OUT_OF_RANGE)); + assert_eq!(p.set_year_div_100(0x1_0000_0008), Err(Error::InvalidArgument)); assert!(p.set_year_div_100(8).is_ok()); - assert_eq!(p.set_year_div_100(0x1_0000_0008), Err(OUT_OF_RANGE)); + assert_eq!(p.set_year_div_100(0x1_0000_0008), Err(Error::InvalidArgument)); // month, week*, isoweek, ordinal, day, minute, second, nanosecond, offset let mut p = Parsed::default(); assert!(p.set_month(7).is_ok()); - assert_eq!(p.set_month(1), Err(IMPOSSIBLE)); - assert_eq!(p.set_month(6), Err(IMPOSSIBLE)); - assert_eq!(p.set_month(8), Err(IMPOSSIBLE)); - assert_eq!(p.set_month(12), Err(IMPOSSIBLE)); + assert_eq!(p.set_month(1), Err(Error::Inconsistent)); + assert_eq!(p.set_month(6), Err(Error::Inconsistent)); + assert_eq!(p.set_month(8), Err(Error::Inconsistent)); + assert_eq!(p.set_month(12), Err(Error::Inconsistent)); let mut p = Parsed::default(); assert!(p.set_month(8).is_ok()); - assert_eq!(p.set_month(0x1_0000_0008), Err(OUT_OF_RANGE)); + assert_eq!(p.set_month(0x1_0000_0008), Err(Error::InvalidArgument)); // hour let mut p = Parsed::default(); assert!(p.set_hour(12).is_ok()); - assert_eq!(p.set_hour(11), Err(IMPOSSIBLE)); - assert_eq!(p.set_hour(13), Err(IMPOSSIBLE)); + assert_eq!(p.set_hour(11), Err(Error::Inconsistent)); + assert_eq!(p.set_hour(13), Err(Error::Inconsistent)); assert!(p.set_hour(12).is_ok()); - assert_eq!(p.set_ampm(false), Err(IMPOSSIBLE)); + assert_eq!(p.set_ampm(false), Err(Error::Inconsistent)); assert!(p.set_ampm(true).is_ok()); assert!(p.set_hour12(12).is_ok()); - assert_eq!(p.set_hour12(0), Err(OUT_OF_RANGE)); // requires canonical representation - assert_eq!(p.set_hour12(1), Err(IMPOSSIBLE)); - assert_eq!(p.set_hour12(11), Err(IMPOSSIBLE)); + assert_eq!(p.set_hour12(0), Err(Error::InvalidArgument)); // requires canonical representation + assert_eq!(p.set_hour12(1), Err(Error::Inconsistent)); + assert_eq!(p.set_hour12(11), Err(Error::Inconsistent)); let mut p = Parsed::default(); assert!(p.set_ampm(true).is_ok()); assert!(p.set_hour12(7).is_ok()); - assert_eq!(p.set_hour(7), Err(IMPOSSIBLE)); - assert_eq!(p.set_hour(18), Err(IMPOSSIBLE)); + assert_eq!(p.set_hour(7), Err(Error::Inconsistent)); + assert_eq!(p.set_hour(18), Err(Error::Inconsistent)); assert!(p.set_hour(19).is_ok()); // timestamp let mut p = Parsed::default(); assert!(p.set_timestamp(1_234_567_890).is_ok()); - assert_eq!(p.set_timestamp(1_234_567_889), Err(IMPOSSIBLE)); - assert_eq!(p.set_timestamp(1_234_567_891), Err(IMPOSSIBLE)); + assert_eq!(p.set_timestamp(1_234_567_889), Err(Error::Inconsistent)); + assert_eq!(p.set_timestamp(1_234_567_891), Err(Error::Inconsistent)); } #[test] fn test_parsed_set_range() { - assert_eq!(Parsed::default().set_year(i32::MIN as i64 - 1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_year(i32::MIN as i64 - 1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_year(i32::MIN as i64).is_ok()); assert!(Parsed::default().set_year(i32::MAX as i64).is_ok()); - assert_eq!(Parsed::default().set_year(i32::MAX as i64 + 1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_year(i32::MAX as i64 + 1), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_year_div_100(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_year_div_100(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_year_div_100(0).is_ok()); assert!(Parsed::default().set_year_div_100(i32::MAX as i64).is_ok()); - assert_eq!(Parsed::default().set_year_div_100(i32::MAX as i64 + 1), Err(OUT_OF_RANGE)); + assert_eq!( + Parsed::default().set_year_div_100(i32::MAX as i64 + 1), + Err(Error::InvalidArgument) + ); - assert_eq!(Parsed::default().set_year_mod_100(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_year_mod_100(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_year_mod_100(0).is_ok()); assert!(Parsed::default().set_year_mod_100(99).is_ok()); - assert_eq!(Parsed::default().set_year_mod_100(100), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_year_mod_100(100), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_isoyear(i32::MIN as i64 - 1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoyear(i32::MIN as i64 - 1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_isoyear(i32::MIN as i64).is_ok()); assert!(Parsed::default().set_isoyear(i32::MAX as i64).is_ok()); - assert_eq!(Parsed::default().set_isoyear(i32::MAX as i64 + 1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoyear(i32::MAX as i64 + 1), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_isoyear_div_100(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoyear_div_100(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_isoyear_div_100(0).is_ok()); assert!(Parsed::default().set_isoyear_div_100(99).is_ok()); - assert_eq!(Parsed::default().set_isoyear_div_100(i32::MAX as i64 + 1), Err(OUT_OF_RANGE)); + assert_eq!( + Parsed::default().set_isoyear_div_100(i32::MAX as i64 + 1), + Err(Error::InvalidArgument) + ); - assert_eq!(Parsed::default().set_isoyear_mod_100(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoyear_mod_100(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_isoyear_mod_100(0).is_ok()); assert!(Parsed::default().set_isoyear_mod_100(99).is_ok()); - assert_eq!(Parsed::default().set_isoyear_mod_100(100), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoyear_mod_100(100), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_month(0), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_month(0), Err(Error::InvalidArgument)); assert!(Parsed::default().set_month(1).is_ok()); assert!(Parsed::default().set_month(12).is_ok()); - assert_eq!(Parsed::default().set_month(13), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_month(13), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_week_from_sun(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_week_from_sun(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_week_from_sun(0).is_ok()); assert!(Parsed::default().set_week_from_sun(53).is_ok()); - assert_eq!(Parsed::default().set_week_from_sun(54), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_week_from_sun(54), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_week_from_mon(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_week_from_mon(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_week_from_mon(0).is_ok()); assert!(Parsed::default().set_week_from_mon(53).is_ok()); - assert_eq!(Parsed::default().set_week_from_mon(54), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_week_from_mon(54), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_isoweek(0), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoweek(0), Err(Error::InvalidArgument)); assert!(Parsed::default().set_isoweek(1).is_ok()); assert!(Parsed::default().set_isoweek(53).is_ok()); - assert_eq!(Parsed::default().set_isoweek(54), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_isoweek(54), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_ordinal(0), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_ordinal(0), Err(Error::InvalidArgument)); assert!(Parsed::default().set_ordinal(1).is_ok()); assert!(Parsed::default().set_ordinal(366).is_ok()); - assert_eq!(Parsed::default().set_ordinal(367), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_ordinal(367), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_day(0), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_day(0), Err(Error::InvalidArgument)); assert!(Parsed::default().set_day(1).is_ok()); assert!(Parsed::default().set_day(31).is_ok()); - assert_eq!(Parsed::default().set_day(32), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_day(32), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_hour12(0), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_hour12(0), Err(Error::InvalidArgument)); assert!(Parsed::default().set_hour12(1).is_ok()); assert!(Parsed::default().set_hour12(12).is_ok()); - assert_eq!(Parsed::default().set_hour12(13), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_hour12(13), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_hour(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_hour(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_hour(0).is_ok()); assert!(Parsed::default().set_hour(23).is_ok()); - assert_eq!(Parsed::default().set_hour(24), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_hour(24), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_minute(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_minute(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_minute(0).is_ok()); assert!(Parsed::default().set_minute(59).is_ok()); - assert_eq!(Parsed::default().set_minute(60), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_minute(60), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_second(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_second(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_second(0).is_ok()); assert!(Parsed::default().set_second(60).is_ok()); - assert_eq!(Parsed::default().set_second(61), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_second(61), Err(Error::InvalidArgument)); - assert_eq!(Parsed::default().set_nanosecond(-1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_nanosecond(-1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_nanosecond(0).is_ok()); assert!(Parsed::default().set_nanosecond(999_999_999).is_ok()); - assert_eq!(Parsed::default().set_nanosecond(1_000_000_000), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_nanosecond(1_000_000_000), Err(Error::InvalidArgument)); assert!(Parsed::default().set_timestamp(i64::MIN).is_ok()); assert!(Parsed::default().set_timestamp(i64::MAX).is_ok()); - assert_eq!(Parsed::default().set_offset(i32::MIN as i64 - 1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_offset(i32::MIN as i64 - 1), Err(Error::InvalidArgument)); assert!(Parsed::default().set_offset(i32::MIN as i64).is_ok()); assert!(Parsed::default().set_offset(i32::MAX as i64).is_ok()); - assert_eq!(Parsed::default().set_offset(i32::MAX as i64 + 1), Err(OUT_OF_RANGE)); + assert_eq!(Parsed::default().set_offset(i32::MAX as i64 + 1), Err(Error::InvalidArgument)); } #[test]