Skip to content

Commit

Permalink
Replace ParseError with Error
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 12, 2024
1 parent 81bfe78 commit 3aefc88
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 46 deletions.
12 changes: 6 additions & 6 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1651,10 +1651,10 @@ where
/// "2012-12-12 12:12:12Z".parse::<DateTime<Utc>>()?;
/// "2012-12-12 12:12:12+0000".parse::<DateTime<Utc>>()?;
/// "2012-12-12 12:12:12+00:00".parse::<DateTime<Utc>>()?;
/// # Ok::<(), chrono::ParseError>(())
/// # Ok::<(), chrono::Error>(())
/// ```
impl str::FromStr for DateTime<Utc> {
type Err = ParseError;
type Err = Error;

fn from_str(s: &str) -> ParseResult<DateTime<Utc>> {
s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&Utc))
Expand All @@ -1672,11 +1672,11 @@ impl str::FromStr for DateTime<Utc> {
/// "2012-12-12 12:12:12Z".parse::<DateTime<Local>>()?;
/// "2012-12-12 12:12:12+0000".parse::<DateTime<Local>>()?;
/// "2012-12-12 12:12:12+00:00".parse::<DateTime<Local>>()?;
/// # Ok::<(), chrono::ParseError>(())
/// # Ok::<(), chrono::Error>(())
/// ```
#[cfg(feature = "clock")]
impl str::FromStr for DateTime<Local> {
type Err = ParseError;
type Err = Error;

fn from_str(s: &str) -> ParseResult<DateTime<Local>> {
s.parse::<DateTime<FixedOffset>>().map(|dt| dt.with_timezone(&Local))
Expand Down
9 changes: 3 additions & 6 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))]
Expand Down Expand Up @@ -378,11 +378,8 @@ impl<'a> Item<'a> {
}
}

/// An error from the `parse` function.
pub type ParseError = Error;

/// Same as `Result<T, ParseError>`.
pub type ParseResult<T> = Result<T, ParseError>;
/// Same as `Result<T, Error>`.
pub type ParseResult<T> = Result<T, Error>;

// to be used in this module and submodules
pub(crate) const OUT_OF_RANGE: Error = Error::InvalidArgument;
Expand Down
20 changes: 10 additions & 10 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Item = B>,
B: Borrow<Item<'a>>,
Expand Down Expand Up @@ -521,10 +521,10 @@ where
/// "2012-12-12T12:12:12Z".parse::<DateTime<FixedOffset>>()?;
/// "2012-12-12 12:12:12Z".parse::<DateTime<FixedOffset>>()?;
/// "2012- 12-12T12: 12:12Z".parse::<DateTime<FixedOffset>>()?;
/// # Ok::<(), chrono::ParseError>(())
/// # Ok::<(), chrono::Error>(())
/// ```
impl str::FromStr for DateTime<FixedOffset> {
type Err = ParseError;
type Err = Error;

fn from_str(s: &str) -> ParseResult<DateTime<FixedOffset>> {
let mut parsed = Parsed::default();
Expand Down Expand Up @@ -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::*;

Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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::*;
Expand Down
10 changes: 5 additions & 5 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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(),
Expand Down
18 changes: 10 additions & 8 deletions src/format/strftime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Vec<Item<'a>>, ParseError> {
pub fn parse(self) -> Result<Vec<Item<'a>>, Error> {
self.into_iter()
.map(|item| match item == Item::Error {
false => Ok(item),
Expand All @@ -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<Vec<Item<'static>>, ParseError> {
/// fn format_items(date_fmt: &str, time_fmt: &str) -> Result<Vec<Item<'static>>, 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()
Expand All @@ -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<Vec<Item<'static>>, ParseError> {
pub fn parse_to_owned(self) -> Result<Vec<Item<'static>>, Error> {
self.into_iter()
.map(|item| match item == Item::Error {
false => Ok(item.to_owned()),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 3 additions & 3 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -2105,7 +2105,7 @@ impl fmt::Display for NaiveDate {
/// assert!("foo".parse::<NaiveDate>().is_err());
/// ```
impl str::FromStr for NaiveDate {
type Err = ParseError;
type Err = Error;

fn from_str(s: &str) -> ParseResult<NaiveDate> {
const ITEMS: &[Item<'static>] = &[
Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1825,7 +1825,7 @@ impl fmt::Display for NaiveDateTime {
/// assert!("foo".parse::<NaiveDateTime>().is_err());
/// ```
impl str::FromStr for NaiveDateTime {
type Err = ParseError;
type Err = Error;

fn from_str(s: &str) -> ParseResult<NaiveDateTime> {
const ITEMS: &[Item<'static>] = &[
Expand Down
6 changes: 3 additions & 3 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1513,7 +1513,7 @@ impl fmt::Display for NaiveTime {
/// assert!("foo".parse::<NaiveTime>().is_err());
/// ```
impl str::FromStr for NaiveTime {
type Err = ParseError;
type Err = Error;

fn from_str(s: &str) -> ParseResult<NaiveTime> {
const HOUR_AND_MINUTE: &[Item<'static>] = &[
Expand Down
5 changes: 3 additions & 2 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Self, Self::Err> {
let (_, offset) = scan::timezone_offset(s, scan::consume_colon_maybe, false, false, true)?;
Self::east(offset).map_err(|_| OUT_OF_RANGE)
Expand Down

0 comments on commit 3aefc88

Please sign in to comment.