Skip to content

Commit 310e2bc

Browse files
committed
flatten DateFromFieldsError::Range
1 parent 2cf1b7f commit 310e2bc

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

components/calendar/src/error.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ pub enum DateFromFieldsError {
108108
/// ```
109109
/// use icu_calendar::Date;
110110
/// use icu_calendar::error::DateFromFieldsError;
111-
/// use icu_calendar::error::RangeError;
112111
/// use icu_calendar::Iso;
113112
/// use icu_calendar::types::DateFields;
114113
///
@@ -122,10 +121,20 @@ pub enum DateFromFieldsError {
122121
/// )
123122
/// .expect_err("no month 13 in the ISO calendar");
124123
///
125-
/// assert!(matches!(err, DateFromFieldsError::Range(RangeError { field: "month", .. })));
124+
/// assert!(matches!(err, DateFromFieldsError::Range { field: "month", .. }));
126125
/// ```
127-
#[displaydoc("{0}")]
128-
Range(RangeError),
126+
#[displaydoc("The {field} = {value} argument is out of range {min}..={max}")]
127+
#[non_exhaustive]
128+
Range {
129+
/// The field that is out of range, such as "year"
130+
field: &'static str,
131+
/// The actual value
132+
value: i32,
133+
/// The minimum value (inclusive). This might not be tight.
134+
min: i32,
135+
/// The maximum value (inclusive). This might not be tight.
136+
max: i32,
137+
},
129138
/// The era code is invalid for the calendar.
130139
#[displaydoc("Unknown era or invalid syntax")]
131140
UnknownEra,
@@ -343,13 +352,6 @@ pub enum DateFromFieldsError {
343352

344353
impl core::error::Error for DateFromFieldsError {}
345354

346-
impl From<RangeError> for DateFromFieldsError {
347-
#[inline]
348-
fn from(value: RangeError) -> Self {
349-
DateFromFieldsError::Range(value)
350-
}
351-
}
352-
353355
/// Internal narrow error type for functions that only fail on unknown eras
354356
pub(crate) struct UnknownEraError;
355357

@@ -473,6 +475,24 @@ impl From<RangeError> for DateError {
473475
}
474476
}
475477

478+
impl From<RangeError> for DateFromFieldsError {
479+
#[inline]
480+
fn from(value: RangeError) -> Self {
481+
let RangeError {
482+
field,
483+
value,
484+
min,
485+
max,
486+
} = value;
487+
DateFromFieldsError::Range {
488+
field,
489+
value,
490+
min,
491+
max,
492+
}
493+
}
494+
}
495+
476496
pub(crate) fn range_check_with_overflow<T: Ord + Into<i32> + Copy>(
477497
value: T,
478498
field: &'static str,

ffi/capi/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl From<icu_calendar::DateError> for CalendarError {
204204
impl From<icu_calendar::error::DateFromFieldsError> for CalendarDateFromFieldsError {
205205
fn from(e: icu_calendar::error::DateFromFieldsError) -> Self {
206206
match e {
207-
icu_calendar::error::DateFromFieldsError::Range(_) => Self::OutOfRange,
207+
icu_calendar::error::DateFromFieldsError::Range { .. } => Self::OutOfRange,
208208
icu_calendar::error::DateFromFieldsError::UnknownEra => Self::UnknownEra,
209209
icu_calendar::error::DateFromFieldsError::MonthCodeInvalidSyntax => {
210210
Self::MonthCodeInvalidSyntax

0 commit comments

Comments
 (0)