From 3aefc88388d23fab5bd1c181dd30ae1e24a736ba Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Tue, 12 Mar 2024 14:33:06 +0100 Subject: [PATCH] Replace `ParseError` with `Error` --- src/datetime/mod.rs | 12 ++++++------ src/format/mod.rs | 9 +++------ src/format/parse.rs | 20 ++++++++++---------- src/format/parsed.rs | 10 +++++----- src/format/strftime.rs | 18 ++++++++++-------- src/lib.rs | 2 +- src/naive/date/mod.rs | 6 +++--- src/naive/datetime/mod.rs | 4 ++-- src/naive/time/mod.rs | 6 +++--- src/offset/fixed.rs | 5 +++-- 10 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 85e01c505a..7ef6a67c62 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -17,8 +17,8 @@ use std::time::{SystemTime, UNIX_EPOCH}; #[cfg(all(feature = "unstable-locales", feature = "alloc"))] use crate::format::Locale; use crate::format::{ - parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed, - StrftimeItems, TOO_LONG, + parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseResult, Parsed, StrftimeItems, + TOO_LONG, }; #[cfg(feature = "alloc")] use crate::format::{write_rfc2822, write_rfc3339, DelayedFormat, SecondsFormat}; @@ -1651,10 +1651,10 @@ where /// "2012-12-12 12:12:12Z".parse::>()?; /// "2012-12-12 12:12:12+0000".parse::>()?; /// "2012-12-12 12:12:12+00:00".parse::>()?; -/// # Ok::<(), chrono::ParseError>(()) +/// # Ok::<(), chrono::Error>(()) /// ``` impl str::FromStr for DateTime { - type Err = ParseError; + type Err = Error; fn from_str(s: &str) -> ParseResult> { s.parse::>().map(|dt| dt.with_timezone(&Utc)) @@ -1672,11 +1672,11 @@ impl str::FromStr for DateTime { /// "2012-12-12 12:12:12Z".parse::>()?; /// "2012-12-12 12:12:12+0000".parse::>()?; /// "2012-12-12 12:12:12+00:00".parse::>()?; -/// # Ok::<(), chrono::ParseError>(()) +/// # Ok::<(), chrono::Error>(()) /// ``` #[cfg(feature = "clock")] impl str::FromStr for DateTime { - type Err = ParseError; + type Err = Error; fn from_str(s: &str) -> ParseResult> { s.parse::>().map(|dt| dt.with_timezone(&Local)) diff --git a/src/format/mod.rs b/src/format/mod.rs index 3f711e2049..82921c1cd3 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -27,7 +27,7 @@ //! //! let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.and_utc(); //! assert_eq!(parsed, date_time); -//! # Ok::<(), chrono::ParseError>(()) +//! # Ok::<(), chrono::Error>(()) //! ``` #[cfg(all(feature = "alloc", not(feature = "std"), not(test)))] @@ -378,11 +378,8 @@ impl<'a> Item<'a> { } } -/// An error from the `parse` function. -pub type ParseError = Error; - -/// Same as `Result`. -pub type ParseResult = Result; +/// Same as `Result`. +pub type ParseResult = Result; // to be used in this module and submodules pub(crate) const OUT_OF_RANGE: Error = Error::InvalidArgument; diff --git a/src/format/parse.rs b/src/format/parse.rs index 4e5bcc2edc..0c77d22cd5 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -9,10 +9,10 @@ use core::str; use core::usize; use super::scan; +use super::ParseResult; use super::{Fixed, InternalFixed, InternalInternal, Item, Numeric, Pad, Parsed}; -use super::{ParseError, ParseResult}; use super::{BAD_FORMAT, INVALID, OUT_OF_RANGE, TOO_LONG, TOO_SHORT}; -use crate::{DateTime, FixedOffset, Weekday}; +use crate::{DateTime, Error, FixedOffset, Weekday}; fn set_weekday_with_num_days_from_sunday(p: &mut Parsed, v: i64) -> ParseResult<&mut Parsed> { p.set_weekday(match v { @@ -288,7 +288,7 @@ fn parse_internal<'a, 'b, I, B>( parsed: &mut Parsed, mut s: &'b str, items: I, -) -> Result<&'b str, ParseError> +) -> Result<&'b str, Error> where I: Iterator, B: Borrow>, @@ -521,10 +521,10 @@ where /// "2012-12-12T12:12:12Z".parse::>()?; /// "2012-12-12 12:12:12Z".parse::>()?; /// "2012- 12-12T12: 12:12Z".parse::>()?; -/// # Ok::<(), chrono::ParseError>(()) +/// # Ok::<(), chrono::Error>(()) /// ``` impl str::FromStr for DateTime { - type Err = ParseError; + type Err = Error; fn from_str(s: &str) -> ParseResult> { let mut parsed = Parsed::default(); @@ -718,7 +718,7 @@ mod tests { } #[test] - fn test_parse_numeric() -> Result<(), ParseError> { + fn test_parse_numeric() -> Result<(), Error> { use crate::format::Item::{Literal, Space}; use crate::format::Numeric::*; @@ -845,7 +845,7 @@ mod tests { } #[test] - fn test_parse_fixed() -> Result<(), ParseError> { + fn test_parse_fixed() -> Result<(), Error> { use crate::format::Fixed::*; use crate::format::Item::{Literal, Space}; @@ -914,7 +914,7 @@ mod tests { } #[test] - fn test_parse_fixed_nanosecond() -> Result<(), ParseError> { + fn test_parse_fixed_nanosecond() -> Result<(), Error> { use crate::format::Fixed::Nanosecond; use crate::format::InternalInternal::*; use crate::format::Item::Literal; @@ -1015,7 +1015,7 @@ mod tests { } #[test] - fn test_parse_fixed_timezone_offset() -> Result<(), ParseError> { + fn test_parse_fixed_timezone_offset() -> Result<(), Error> { use crate::format::Fixed::*; use crate::format::InternalInternal::*; use crate::format::Item::Literal; @@ -1452,7 +1452,7 @@ mod tests { #[test] #[rustfmt::skip] - fn test_parse_practical_examples() -> Result<(), ParseError> { + fn test_parse_practical_examples() -> Result<(), Error> { use crate::format::InternalInternal::*; use crate::format::Item::{Literal, Space}; use crate::format::Numeric::*; diff --git a/src/format/parsed.rs b/src/format/parsed.rs index b3fed34864..0a91f8e821 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -86,7 +86,7 @@ use crate::{DateTime, Datelike, Error, TimeDelta, Timelike, Weekday}; /// .set_second(40)? /// .set_offset(0)?; /// assert_eq!(parsed.to_datetime(), Err(Error::Inconsistent)); -/// # Ok::<(), chrono::ParseError>(()) +/// # Ok::<(), chrono::Error>(()) /// ``` /// /// The same using chrono's build-in parser for RFC 2822 (the [RFC2822 formatting item]) and @@ -117,7 +117,7 @@ use crate::{DateTime, Datelike, Error, TimeDelta, Timelike, Weekday}; /// // What is the weekday? /// assert_eq!(parsed.weekday(), Some(Weekday::Thu)); /// } -/// # Ok::<(), chrono::ParseError>(()) +/// # Ok::<(), chrono::Error>(()) /// ``` #[derive(Clone, PartialEq, Eq, Debug, Default, Hash)] pub struct Parsed { @@ -1121,12 +1121,12 @@ fn resolve_week_date( #[cfg(test)] mod tests { - use super::super::{ParseError, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE}; + use super::super::{IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE}; use super::Parsed; use crate::naive::{NaiveDate, NaiveTime}; use crate::offset::{FixedOffset, TimeZone, Utc}; - use crate::Datelike; use crate::Weekday::*; + use crate::{Datelike, Error}; #[test] fn test_parsed_set_fields() { @@ -1752,7 +1752,7 @@ mod tests { } #[test] - fn issue_551() -> Result<(), ParseError> { + fn issue_551() -> Result<(), Error> { use crate::Weekday; assert_eq!( NaiveDate::from_ymd(2002, 6, 3).unwrap(), diff --git a/src/format/strftime.rs b/src/format/strftime.rs index d928c7bac6..29a40addff 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -159,12 +159,14 @@ Notes: #[cfg(feature = "alloc")] extern crate alloc; +#[cfg(any(feature = "alloc", feature = "std"))] +use super::BAD_FORMAT; use super::{fixed, internal_fixed, num, num0, nums}; #[cfg(feature = "unstable-locales")] use super::{locales, Locale}; use super::{Fixed, InternalInternal, Item, Numeric, Pad}; #[cfg(any(feature = "alloc", feature = "std"))] -use super::{ParseError, BAD_FORMAT}; +use crate::Error; #[cfg(all(feature = "alloc", not(feature = "std"), not(test)))] use alloc::vec::Vec; @@ -327,10 +329,10 @@ impl<'a> StrftimeItems<'a> { /// parse(&mut parsed, "11 Jul 2023 9.00", fmt_items.as_slice().iter())?; /// let parsed_dt = parsed.to_naive_datetime_with_offset(0)?; /// assert_eq!(parsed_dt, datetime); - /// # Ok::<(), chrono::ParseError>(()) + /// # Ok::<(), chrono::Error>(()) /// ``` #[cfg(any(feature = "alloc", feature = "std"))] - pub fn parse(self) -> Result>, ParseError> { + pub fn parse(self) -> Result>, Error> { self.into_iter() .map(|item| match item == Item::Error { false => Ok(item), @@ -354,10 +356,10 @@ impl<'a> StrftimeItems<'a> { /// # Example /// /// ``` - /// use chrono::format::{Item, ParseError, StrftimeItems}; - /// use chrono::NaiveDate; + /// use chrono::format::{Item, StrftimeItems}; + /// use chrono::{Error, NaiveDate}; /// - /// fn format_items(date_fmt: &str, time_fmt: &str) -> Result>, ParseError> { + /// fn format_items(date_fmt: &str, time_fmt: &str) -> Result>, Error> { /// // `fmt_string` is dropped at the end of this function. /// let fmt_string = format!("{} {}", date_fmt, time_fmt); /// StrftimeItems::new(&fmt_string).parse_to_owned() @@ -370,10 +372,10 @@ impl<'a> StrftimeItems<'a> { /// datetime.format_with_items(fmt_items.as_slice().iter()).to_string(), /// "11 Jul 2023 9.00" /// ); - /// # Ok::<(), ParseError>(()) + /// # Ok::<(), Error>(()) /// ``` #[cfg(any(feature = "alloc", feature = "std"))] - pub fn parse_to_owned(self) -> Result>, ParseError> { + pub fn parse_to_owned(self) -> Result>, Error> { self.into_iter() .map(|item| match item == Item::Error { false => Ok(item.to_owned()), diff --git a/src/lib.rs b/src/lib.rs index daefd98193..af93d7c139 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -501,7 +501,7 @@ pub mod format; /// L10n locales. #[cfg(feature = "unstable-locales")] pub use format::Locale; -pub use format::{ParseError, ParseResult, SecondsFormat}; +pub use format::{ParseResult, SecondsFormat}; pub mod naive; #[doc(inline)] diff --git a/src/naive/date/mod.rs b/src/naive/date/mod.rs index 7c24cc830b..2f4ad558c3 100644 --- a/src/naive/date/mod.rs +++ b/src/naive/date/mod.rs @@ -30,8 +30,8 @@ use pure_rust_locales::Locale; #[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{ - parse, parse_and_remainder, write_hundreds, Item, Numeric, Pad, ParseError, ParseResult, - Parsed, StrftimeItems, + parse, parse_and_remainder, write_hundreds, Item, Numeric, Pad, ParseResult, Parsed, + StrftimeItems, }; use crate::month::Months; use crate::naive::{Days, IsoWeek, NaiveDateTime, NaiveTime, NaiveWeek}; @@ -2105,7 +2105,7 @@ impl fmt::Display for NaiveDate { /// assert!("foo".parse::().is_err()); /// ``` impl str::FromStr for NaiveDate { - type Err = ParseError; + type Err = Error; fn from_str(s: &str) -> ParseResult { const ITEMS: &[Item<'static>] = &[ diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 6c3dec0a40..b5b69aa41a 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -15,7 +15,7 @@ use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "alloc")] use crate::format::DelayedFormat; -use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems}; +use crate::format::{parse, parse_and_remainder, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime}; use crate::offset::Utc; @@ -1825,7 +1825,7 @@ impl fmt::Display for NaiveDateTime { /// assert!("foo".parse::().is_err()); /// ``` impl str::FromStr for NaiveDateTime { - type Err = ParseError; + type Err = Error; fn from_str(s: &str) -> ParseResult { const ITEMS: &[Item<'static>] = &[ diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 6ddfd5660f..184c1031f2 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -15,8 +15,8 @@ use rkyv::{Archive, Deserialize, Serialize}; #[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{ - parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseError, ParseResult, - Parsed, StrftimeItems, + parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseResult, Parsed, + StrftimeItems, }; use crate::{expect, try_ok_or}; use crate::{Error, FixedOffset, TimeDelta, Timelike}; @@ -1513,7 +1513,7 @@ impl fmt::Display for NaiveTime { /// assert!("foo".parse::().is_err()); /// ``` impl str::FromStr for NaiveTime { - type Err = ParseError; + type Err = Error; fn from_str(s: &str) -> ParseResult { const HOUR_AND_MINUTE: &[Item<'static>] = &[ diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index 2f16e1ff2c..00f2b1e9dc 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -11,7 +11,7 @@ use rkyv::{Archive, Deserialize, Serialize}; use super::{LocalResult, Offset, TimeZone}; use crate::format::{scan, OUT_OF_RANGE}; -use crate::{Error, NaiveDateTime, ParseError}; +use crate::{Error, NaiveDateTime}; /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. /// @@ -99,7 +99,8 @@ impl FixedOffset { /// Parsing a `str` into a `FixedOffset` uses the format [`%z`](crate::format::strftime). impl FromStr for FixedOffset { - type Err = ParseError; + type Err = Error; + fn from_str(s: &str) -> Result { let (_, offset) = scan::timezone_offset(s, scan::consume_colon_maybe, false, false, true)?; Self::east(offset).map_err(|_| OUT_OF_RANGE)