Skip to content

Commit c5baab4

Browse files
committed
add new variants to DateError
1 parent 63ac7ea commit c5baab4

File tree

10 files changed

+117
-61
lines changed

10 files changed

+117
-61
lines changed

components/calendar/src/any_calendar.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ mod tests {
16341634
100,
16351635
Month::new(13),
16361636
1,
1637-
DateError::UnknownMonthCode(Month::new(13).code()),
1637+
DateError::MonthNotInCalendar,
16381638
);
16391639
}
16401640

@@ -1653,7 +1653,7 @@ mod tests {
16531653
100,
16541654
Month::new(14),
16551655
1,
1656-
DateError::UnknownMonthCode(Month::new(14).code()),
1656+
DateError::MonthNotInCalendar,
16571657
);
16581658
}
16591659

@@ -1701,7 +1701,7 @@ mod tests {
17011701
100,
17021702
Month::new(14),
17031703
1,
1704-
DateError::UnknownMonthCode(Month::new(14).code()),
1704+
DateError::MonthNotInCalendar,
17051705
);
17061706
}
17071707

@@ -1731,7 +1731,7 @@ mod tests {
17311731
100,
17321732
Month::new(14),
17331733
1,
1734-
DateError::UnknownMonthCode(Month::new(14).code()),
1734+
DateError::MonthNotInCalendar,
17351735
);
17361736
}
17371737

@@ -1778,7 +1778,7 @@ mod tests {
17781778
100,
17791779
Month::new(13),
17801780
1,
1781-
DateError::UnknownMonthCode(Month::new(13).code()),
1781+
DateError::MonthNotInCalendar,
17821782
);
17831783
}
17841784

@@ -1796,7 +1796,7 @@ mod tests {
17961796
100,
17971797
Month::new(13),
17981798
1,
1799-
DateError::UnknownMonthCode(Month::new(13).code()),
1799+
DateError::MonthNotInCalendar,
18001800
);
18011801
}
18021802

@@ -1813,7 +1813,7 @@ mod tests {
18131813
4658,
18141814
Month::new(13),
18151815
1,
1816-
DateError::UnknownMonthCode(Month::new(13).code()),
1816+
DateError::MonthNotInCalendar,
18171817
);
18181818
}
18191819

@@ -1830,7 +1830,7 @@ mod tests {
18301830
10393,
18311831
Month::leap(0),
18321832
1,
1833-
DateError::UnknownMonthCode(Month::leap(0).code()),
1833+
DateError::MonthNotInCalendar,
18341834
);
18351835
}
18361836

@@ -1881,7 +1881,7 @@ mod tests {
18811881
2,
18821882
Month::new(13),
18831883
1,
1884-
DateError::UnknownMonthCode(Month::new(13).code()),
1884+
DateError::MonthNotInCalendar,
18851885
);
18861886
}
18871887

@@ -1954,7 +1954,7 @@ mod tests {
19541954
2,
19551955
Month::new(13),
19561956
1,
1957-
DateError::UnknownMonthCode(Month::new(13).code()),
1957+
DateError::MonthNotInCalendar,
19581958
);
19591959
}
19601960

@@ -1972,7 +1972,7 @@ mod tests {
19721972
100,
19731973
Month::new(50),
19741974
1,
1975-
DateError::UnknownMonthCode(Month::new(50).code()),
1975+
DateError::MonthNotInCalendar
19761976
);
19771977
}
19781978

@@ -1990,7 +1990,7 @@ mod tests {
19901990
100,
19911991
Month::new(50),
19921992
1,
1993-
DateError::UnknownMonthCode(Month::new(50).code()),
1993+
DateError::MonthNotInCalendar
19941994
);
19951995
}
19961996

@@ -2027,7 +2027,7 @@ mod tests {
20272027
100,
20282028
Month::new(50),
20292029
1,
2030-
DateError::UnknownMonthCode(Month::new(50).code()),
2030+
DateError::MonthNotInCalendar
20312031
);
20322032
}
20332033

@@ -2058,7 +2058,7 @@ mod tests {
20582058
100,
20592059
Month::new(50),
20602060
1,
2061-
DateError::UnknownMonthCode(Month::new(50).code()),
2061+
DateError::MonthNotInCalendar
20622062
);
20632063
}
20642064

@@ -2088,7 +2088,7 @@ mod tests {
20882088
100,
20892089
Month::new(50),
20902090
1,
2091-
DateError::UnknownMonthCode(Month::new(50).code()),
2091+
DateError::MonthNotInCalendar
20922092
);
20932093
}
20942094

@@ -2119,7 +2119,7 @@ mod tests {
21192119
100,
21202120
Month::new(50),
21212121
1,
2122-
DateError::UnknownMonthCode(Month::new(50).code()),
2122+
DateError::MonthNotInCalendar
21232123
);
21242124
}
21252125

@@ -2137,7 +2137,7 @@ mod tests {
21372137
100,
21382138
Month::new(13),
21392139
1,
2140-
DateError::UnknownMonthCode(Month::new(13).code()),
2140+
DateError::MonthNotInCalendar,
21412141
);
21422142
}
21432143
}

components/calendar/src/calendar_arithmetic.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,10 @@ impl<C: DateFieldsResolver> ArithmeticDate<C> {
233233
} else {
234234
calendar.year_info_from_extended(year)
235235
};
236-
let validated = Month::try_from_utf8(month_code.0.as_bytes()).map_err(|e| match e {
236+
let month_code = Month::try_from_utf8(month_code.0.as_bytes()).map_err(|e| match e {
237237
MonthCodeParseError::InvalidSyntax => DateError::UnknownMonthCode(month_code),
238238
})?;
239-
let month = calendar
240-
.ordinal_from_month(year, validated, Default::default())
241-
.map_err(|e| match e {
242-
MonthCodeError::NotInCalendar | MonthCodeError::NotInYear => {
243-
DateError::UnknownMonthCode(month_code)
244-
}
245-
})?;
239+
let month = calendar.ordinal_from_month(year, month_code, Default::default())?;
246240

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

@@ -579,13 +573,8 @@ impl<C: DateFieldsResolver> ArithmeticDate<C> {
579573
base_month,
580574
DateFromFieldsOptions::from_add_options(options),
581575
)
582-
.map_err(|e| {
583-
// TODO: Use a narrower error type here. For now, convert into DateError.
584-
match e {
585-
MonthCodeError::NotInCalendar => DateError::UnknownMonthCode(base_month.code()),
586-
MonthCodeError::NotInYear => DateError::UnknownMonthCode(base_month.code()),
587-
}
588-
})?;
576+
// TODO: Use a narrower error type here. For now, convert into DateError.
577+
.map_err(DateError::from)?;
589578
// 1. Let _endOfMonth_ be BalanceNonISODate(_calendar_, _y0_, _m0_ + _duration_.[[Months]] + 1, 0).
590579
let end_of_month = Self::new_balanced(y0, duration.add_months_to(m0) + 1, 0, cal);
591580
// 1. Let _baseDay_ be _parts_.[[Day]].

components/calendar/src/error.rs

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ use displaydoc::Display;
1414
#[non_exhaustive]
1515
pub enum DateError {
1616
/// A field is out of range for its domain.
17+
///
18+
/// # Examples
19+
///
20+
/// ```
21+
/// use icu::calendar::Date;
22+
/// use icu::calendar::error::DateError;
23+
/// use icu::calendar::Iso;
24+
/// use icu::calendar::types::MonthCode;
25+
///
26+
/// let err = Date::try_new_from_codes(None, 2000, MonthCode::new_normal(2).unwrap(), 30, Iso)
27+
/// .expect_err("no Feb 30 in the ISO calendar");
28+
///
29+
/// assert!(matches!(err, DateError::Range { field: "day", .. }));
30+
/// ```
1731
#[displaydoc("The {field} = {value} argument is out of range {min}..={max}")]
1832
Range {
1933
/// The field that is out of range, such as "year"
@@ -26,41 +40,60 @@ pub enum DateError {
2640
max: i32,
2741
},
2842
/// The era code is invalid for the calendar.
29-
#[displaydoc("Unknown era")]
43+
#[displaydoc("Unknown era or invalid syntax")]
3044
UnknownEra,
31-
/// The month code is invalid for the calendar or year.
45+
/// The month code syntax is invalid.
3246
///
3347
/// # Examples
3448
///
3549
/// ```
50+
/// use icu::calendar::error::DateError;
51+
/// use icu::calendar::types::MonthCode;
52+
/// use icu::calendar::Date;
53+
/// use icu::calendar::Iso;
54+
///
55+
/// let err = Date::try_new_from_codes(None, 2000, MonthCode("Jan".parse().unwrap()), 1, Iso)
56+
/// .expect_err("month code is invalid");
57+
///
58+
/// assert!(matches!(err, DateError::UnknownMonthCode(..)));
59+
/// ```
60+
#[displaydoc("Invalid month code syntax")]
61+
UnknownMonthCode(MonthCode),
62+
/// The specified month code does not exist in this calendar.
63+
///
64+
/// # Examples
65+
///
66+
/// ```
67+
/// use icu::calendar::Date;
68+
/// use icu::calendar::DateError;
3669
/// use icu::calendar::cal::Hebrew;
3770
/// use icu::calendar::types::Month;
71+
///
72+
/// let err = Date::try_new_from_codes(None, 5783, Month::new(13).code(), 1, Hebrew)
73+
/// .expect_err("no month `M13` in Hebrew");
74+
/// assert_eq!(err, DateError::MonthNotInCalendar);
75+
/// ```
76+
#[displaydoc("The specified month code does not exist in this calendar")]
77+
MonthNotInCalendar,
78+
/// The specified month code does not exist in this calendar.
79+
///
80+
/// # Examples
81+
///
82+
/// ```
3883
/// use icu::calendar::Date;
3984
/// use icu::calendar::DateError;
40-
/// use tinystr::tinystr;
41-
///
42-
/// Date::try_new_from_codes(
43-
/// None,
44-
/// 5784,
45-
/// Month::leap(5).code(),
46-
/// 1,
47-
/// Hebrew,
48-
/// )
49-
/// .expect("5784 is a leap year");
85+
/// use icu::calendar::cal::Hebrew;
86+
/// use icu::calendar::types::Month;
5087
///
51-
/// let err = Date::try_new_from_codes(
52-
/// None,
53-
/// 5785,
54-
/// Month::leap(5).code(),
55-
/// 1,
56-
/// Hebrew,
57-
/// )
58-
/// .expect_err("5785 is a common year");
88+
/// Date::try_new_from_codes(None, 5784, Month::leap(5).code(), 1, Hebrew)
89+
/// .expect("5784 is a leap year");
5990
///
60-
/// assert!(matches!(err, DateError::UnknownMonthCode(_)));
91+
/// let err = Date::try_new_from_codes(None, 5785, Month::leap(5).code(), 1, Hebrew)
92+
/// .expect_err("5785 is a common year");
93+
/// assert_eq!(err, DateError::MonthNotInYear);
6194
/// ```
62-
#[displaydoc("Unknown month code {0:?}")]
63-
UnknownMonthCode(MonthCode),
95+
#[displaydoc("The specified month code exists in calendar, but not for this year")]
96+
MonthNotInYear,
6497
}
6598

6699
impl core::error::Error for DateError {}
@@ -217,8 +250,7 @@ mod unstable {
217250
/// use icu::calendar::error::DateFromFieldsError;
218251
/// use icu::calendar::types::DateFields;
219252
/// use icu::calendar::Date;
220-
/// use tinystr::tinystr;
221-
///
253+
///
222254
/// let mut fields = DateFields::default();
223255
/// fields.extended_year = Some(5783);
224256
/// fields.month_code = Some(b"M06");
@@ -246,8 +278,7 @@ mod unstable {
246278
/// use icu::calendar::error::DateFromFieldsError;
247279
/// use icu::calendar::types::DateFields;
248280
/// use icu::calendar::Date;
249-
/// use tinystr::tinystr;
250-
///
281+
///
251282
/// let mut fields = DateFields::default();
252283
///
253284
/// fields.ordinal_month = Some(3);
@@ -333,6 +364,16 @@ pub(crate) enum MonthCodeError {
333364
NotInYear,
334365
}
335366

367+
impl From<MonthCodeError> for DateError {
368+
#[inline]
369+
fn from(value: MonthCodeError) -> Self {
370+
match value {
371+
MonthCodeError::NotInCalendar => DateError::MonthNotInCalendar,
372+
MonthCodeError::NotInYear => DateError::MonthNotInYear,
373+
}
374+
}
375+
}
376+
336377
impl From<MonthCodeError> for DateFromFieldsError {
337378
#[inline]
338379
fn from(value: MonthCodeError) -> Self {

ffi/capi/bindings/c/CalendarError.d.h

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/CalendarError.d.hpp

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/CalendarError.hpp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/dart/CalendarError.g.dart

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/CalendarError.d.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)