Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions components/calendar/src/any_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ mod tests {
100,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -1653,7 +1653,7 @@ mod tests {
100,
Month::new(14),
1,
DateError::UnknownMonthCode(Month::new(14).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -1701,7 +1701,7 @@ mod tests {
100,
Month::new(14),
1,
DateError::UnknownMonthCode(Month::new(14).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -1731,7 +1731,7 @@ mod tests {
100,
Month::new(14),
1,
DateError::UnknownMonthCode(Month::new(14).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -1778,7 +1778,7 @@ mod tests {
100,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -1796,7 +1796,7 @@ mod tests {
100,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -1813,7 +1813,7 @@ mod tests {
4658,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -1830,7 +1830,7 @@ mod tests {
10393,
Month::leap(0),
1,
DateError::UnknownMonthCode(Month::leap(0).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -1881,7 +1881,7 @@ mod tests {
2,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -1954,7 +1954,7 @@ mod tests {
2,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -1972,7 +1972,7 @@ mod tests {
100,
Month::new(50),
1,
DateError::UnknownMonthCode(Month::new(50).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -1990,7 +1990,7 @@ mod tests {
100,
Month::new(50),
1,
DateError::UnknownMonthCode(Month::new(50).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -2027,7 +2027,7 @@ mod tests {
100,
Month::new(50),
1,
DateError::UnknownMonthCode(Month::new(50).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -2058,7 +2058,7 @@ mod tests {
100,
Month::new(50),
1,
DateError::UnknownMonthCode(Month::new(50).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -2088,7 +2088,7 @@ mod tests {
100,
Month::new(50),
1,
DateError::UnknownMonthCode(Month::new(50).code()),
DateError::MonthNotInCalendar,
);
}

Expand Down Expand Up @@ -2119,7 +2119,7 @@ mod tests {
100,
Month::new(50),
1,
DateError::UnknownMonthCode(Month::new(50).code()),
DateError::MonthNotInCalendar,
);
}

Expand All @@ -2137,7 +2137,7 @@ mod tests {
100,
Month::new(13),
1,
DateError::UnknownMonthCode(Month::new(13).code()),
DateError::MonthNotInCalendar,
);
}
}
19 changes: 4 additions & 15 deletions components/calendar/src/calendar_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,10 @@ impl<C: DateFieldsResolver> ArithmeticDate<C> {
} else {
calendar.year_info_from_extended(year)
};
let validated = Month::try_from_utf8(month_code.0.as_bytes()).map_err(|e| match e {
let month_code = Month::try_from_utf8(month_code.0.as_bytes()).map_err(|e| match e {
MonthCodeParseError::InvalidSyntax => DateError::UnknownMonthCode(month_code),
})?;
let month = calendar
.ordinal_from_month(year, validated, Default::default())
.map_err(|e| match e {
MonthCodeError::NotInCalendar | MonthCodeError::NotInYear => {
DateError::UnknownMonthCode(month_code)
}
})?;
let month = calendar.ordinal_from_month(year, month_code, Default::default())?;

let day = range_check(day, "day", 1..=C::days_in_provided_month(year, month))?;

Expand Down Expand Up @@ -579,13 +573,8 @@ impl<C: DateFieldsResolver> ArithmeticDate<C> {
base_month,
DateFromFieldsOptions::from_add_options(options),
)
.map_err(|e| {
// TODO: Use a narrower error type here. For now, convert into DateError.
match e {
MonthCodeError::NotInCalendar => DateError::UnknownMonthCode(base_month.code()),
MonthCodeError::NotInYear => DateError::UnknownMonthCode(base_month.code()),
}
})?;
// TODO: Use a narrower error type here. For now, convert into DateError.
.map_err(DateError::from)?;
// 1. Let _endOfMonth_ be BalanceNonISODate(_calendar_, _y0_, _m0_ + _duration_.[[Months]] + 1, 0).
let end_of_month = Self::new_balanced(y0, duration.add_months_to(m0) + 1, 0, cal);
// 1. Let _baseDay_ be _parts_.[[Day]].
Expand Down
122 changes: 92 additions & 30 deletions components/calendar/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ use displaydoc::Display;
#[non_exhaustive]
pub enum DateError {
/// A field is out of range for its domain.
///
/// # Examples
///
/// ```
/// use icu::calendar::Date;
/// use icu::calendar::error::DateError;
/// use icu::calendar::Iso;
/// use icu::calendar::types::MonthCode;
///
/// let err = Date::try_new_from_codes(None, 2000, MonthCode::new_normal(2).unwrap(), 30, Iso)
/// .expect_err("no Feb 30 in the ISO calendar");
///
/// assert!(matches!(err, DateError::Range { field: "day", .. }));
/// ```
#[displaydoc("The {field} = {value} argument is out of range {min}..={max}")]
Range {
/// The field that is out of range, such as "year"
Expand All @@ -26,41 +40,60 @@ pub enum DateError {
max: i32,
},
/// The era code is invalid for the calendar.
#[displaydoc("Unknown era")]
#[displaydoc("Unknown era or invalid syntax")]
UnknownEra,
/// The month code is invalid for the calendar or year.
/// The month code syntax is invalid.
///
/// # Examples
///
/// ```
/// use icu::calendar::error::DateError;
/// use icu::calendar::types::MonthCode;
/// use icu::calendar::Date;
/// use icu::calendar::Iso;
///
/// let err = Date::try_new_from_codes(None, 2000, MonthCode("Jan".parse().unwrap()), 1, Iso)
/// .expect_err("month code is invalid");
///
/// assert!(matches!(err, DateError::UnknownMonthCode(..)));
/// ```
#[displaydoc("Invalid month code syntax")]
UnknownMonthCode(MonthCode),
/// The specified month code does not exist in this calendar.
///
/// # Examples
///
/// ```
/// use icu::calendar::Date;
/// use icu::calendar::DateError;
/// use icu::calendar::cal::Hebrew;
/// use icu::calendar::types::Month;
///
/// let err = Date::try_new_from_codes(None, 5783, Month::new(13).code(), 1, Hebrew)
/// .expect_err("no month `M13` in Hebrew");
/// assert_eq!(err, DateError::MonthNotInCalendar);
/// ```
#[displaydoc("The specified month code does not exist in this calendar")]
MonthNotInCalendar,
/// The specified month code does not exist in this calendar.
///
/// # Examples
///
/// ```
/// use icu::calendar::Date;
/// use icu::calendar::DateError;
/// use tinystr::tinystr;
///
/// Date::try_new_from_codes(
/// None,
/// 5784,
/// Month::leap(5).code(),
/// 1,
/// Hebrew,
/// )
/// .expect("5784 is a leap year");
/// use icu::calendar::cal::Hebrew;
/// use icu::calendar::types::Month;
///
/// let err = Date::try_new_from_codes(
/// None,
/// 5785,
/// Month::leap(5).code(),
/// 1,
/// Hebrew,
/// )
/// .expect_err("5785 is a common year");
/// Date::try_new_from_codes(None, 5784, Month::leap(5).code(), 1, Hebrew)
/// .expect("5784 is a leap year");
///
/// assert!(matches!(err, DateError::UnknownMonthCode(_)));
/// let err = Date::try_new_from_codes(None, 5785, Month::leap(5).code(), 1, Hebrew)
/// .expect_err("5785 is a common year");
/// assert_eq!(err, DateError::MonthNotInYear);
/// ```
#[displaydoc("Unknown month code {0:?}")]
UnknownMonthCode(MonthCode),
#[displaydoc("The specified month code exists in calendar, but not for this year")]
MonthNotInYear,
}

impl core::error::Error for DateError {}
Expand Down Expand Up @@ -107,11 +140,21 @@ mod unstable {
///
/// assert!(matches!(
/// err,
/// DateFromFieldsError::Range(RangeError { field: "month", .. })
/// ));
/// DateFromFieldsError::Range { field: "month", .. })
/// );
/// ```
#[displaydoc("{0}")]
Range(RangeError),
#[displaydoc("The {field} = {value} argument is out of range {min}..={max}")]
#[non_exhaustive]
Range {
/// The field that is out of range, such as "year"
field: &'static str,
/// The actual value
value: i32,
/// The minimum value (inclusive). This might not be tight.
min: i32,
/// The maximum value (inclusive). This might not be tight.
max: i32,
},
/// The era code is invalid for the calendar.
#[displaydoc("Unknown era or invalid syntax")]
UnknownEra,
Expand Down Expand Up @@ -217,7 +260,6 @@ mod unstable {
/// use icu::calendar::error::DateFromFieldsError;
/// use icu::calendar::types::DateFields;
/// use icu::calendar::Date;
/// use tinystr::tinystr;
///
/// let mut fields = DateFields::default();
/// fields.extended_year = Some(5783);
Expand Down Expand Up @@ -246,7 +288,6 @@ mod unstable {
/// use icu::calendar::error::DateFromFieldsError;
/// use icu::calendar::types::DateFields;
/// use icu::calendar::Date;
/// use tinystr::tinystr;
///
/// let mut fields = DateFields::default();
///
Expand Down Expand Up @@ -287,7 +328,18 @@ mod unstable {
impl From<RangeError> for DateFromFieldsError {
#[inline]
fn from(value: RangeError) -> Self {
DateFromFieldsError::Range(value)
let RangeError {
field,
value,
min,
max,
} = value;
DateFromFieldsError::Range {
field,
value,
min,
max,
}
}
}
}
Expand Down Expand Up @@ -333,6 +385,16 @@ pub(crate) enum MonthCodeError {
NotInYear,
}

impl From<MonthCodeError> for DateError {
#[inline]
fn from(value: MonthCodeError) -> Self {
match value {
MonthCodeError::NotInCalendar => DateError::MonthNotInCalendar,
MonthCodeError::NotInYear => DateError::MonthNotInYear,
}
}
}

impl From<MonthCodeError> for DateFromFieldsError {
#[inline]
fn from(value: MonthCodeError) -> Self {
Expand Down
2 changes: 2 additions & 0 deletions ffi/capi/bindings/c/CalendarError.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ffi/capi/bindings/cpp/icu4x/CalendarError.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading