Skip to content

Commit 5eae8bf

Browse files
committed
Move DateFields::month over to byte slice
1 parent 632422a commit 5eae8bf

File tree

11 files changed

+53
-64
lines changed

11 files changed

+53
-64
lines changed

components/calendar/src/cal/chinese.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ mod test {
16421642
fn test_from_fields_constrain() {
16431643
let fields = DateFields {
16441644
day: Some(31),
1645-
month_code: Some(MonthCode("M01".parse().unwrap())),
1645+
month_code: Some(b"M01"),
16461646
extended_year: Some(1972),
16471647
..Default::default()
16481648
};
@@ -1662,7 +1662,7 @@ mod test {
16621662
// 2022 did not have M01L, the month should be constrained back down
16631663
let fields = DateFields {
16641664
day: Some(1),
1665-
month_code: Some(MonthCode("M01L".parse().unwrap())),
1665+
month_code: Some(b"M01L"),
16661666
extended_year: Some(2022),
16671667
..Default::default()
16681668
};

components/calendar/src/cal/coptic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl Date<Coptic> {
275275
mod tests {
276276
use super::*;
277277
use crate::options::{DateFromFieldsOptions, MissingFieldsStrategy, Overflow};
278-
use crate::types::{DateFields, MonthCode};
278+
use crate::types::DateFields;
279279

280280
#[test]
281281
fn test_coptic_regression() {
@@ -291,7 +291,7 @@ mod tests {
291291
// M13-7 is not a real day, however this should resolve to M12-6
292292
// with Overflow::Constrain
293293
let fields = DateFields {
294-
month_code: Some(MonthCode("M13".parse().unwrap())),
294+
month_code: Some(b"M13"),
295295
day: Some(7),
296296
..Default::default()
297297
};

components/calendar/src/calendar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait Calendar: crate::cal::scaffold::UnstableSealed {
5454
} else {
5555
fields.extended_year = Some(year);
5656
}
57-
fields.month_code = Some(month_code);
57+
fields.month_code = Some(month_code.0.as_bytes());
5858
fields.day = Some(day);
5959
let options = DateFromFieldsOptions {
6060
overflow: Some(Overflow::Reject),

components/calendar/src/calendar_arithmetic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ where
734734
MissingFieldsStrategy::Ecma => {
735735
match (fields.month_code, fields.ordinal_month) {
736736
(Some(month_code), None) => {
737-
let validated = month_code.validated()?;
737+
let validated = ValidMonthCode::from_bytes(month_code)?;
738738
valid_month_code = Some(validated);
739739
cal.reference_year_from_month_day(validated, day)?
740740
}
@@ -766,7 +766,7 @@ where
766766
Some(month_code) => {
767767
let validated = match valid_month_code {
768768
Some(validated) => validated,
769-
None => month_code.validated()?,
769+
None => ValidMonthCode::from_bytes(month_code)?,
770770
};
771771
let computed_month = cal.ordinal_month_from_code(&year, validated, options)?;
772772
if let Some(ordinal_month) = fields.ordinal_month {

components/calendar/src/error.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ pub enum DateFromFieldsError {
8989
/// use icu_calendar::Iso;
9090
/// use icu_calendar::types::MonthCode;
9191
/// use icu_calendar::types::DateFields;
92-
/// use tinystr::tinystr;
9392
///
9493
/// let mut fields = DateFields::default();
9594
/// fields.extended_year = Some(2000);
96-
/// fields.month_code = Some(MonthCode(tinystr!(4, "????")));
95+
/// fields.month_code = Some(b"????");
9796
/// fields.day = Some(1);
9897
///
9998
/// let err = Date::try_from_fields(
@@ -114,12 +113,10 @@ pub enum DateFromFieldsError {
114113
/// use icu_calendar::error::DateFromFieldsError;
115114
/// use icu_calendar::cal::Hebrew;
116115
/// use icu_calendar::types::DateFields;
117-
/// use icu_calendar::types::MonthCode;
118-
/// use tinystr::tinystr;
119116
///
120117
/// let mut fields = DateFields::default();
121118
/// fields.extended_year = Some(5783);
122-
/// fields.month_code = Some(MonthCode::new_normal(13).unwrap());
119+
/// fields.month_code = Some(b"M13");
123120
/// fields.day = Some(1);
124121
///
125122
/// let err = Date::try_from_fields(
@@ -141,12 +138,10 @@ pub enum DateFromFieldsError {
141138
/// use icu_calendar::error::DateFromFieldsError;
142139
/// use icu_calendar::cal::Hebrew;
143140
/// use icu_calendar::types::DateFields;
144-
/// use icu_calendar::types::MonthCode;
145-
/// use tinystr::tinystr;
146141
///
147142
/// let mut fields = DateFields::default();
148143
/// fields.extended_year = Some(5783);
149-
/// fields.month_code = Some(MonthCode::new_leap(5).unwrap());
144+
/// fields.month_code = Some(b"M05L");
150145
/// fields.day = Some(1);
151146
///
152147
/// let err = Date::try_from_fields(
@@ -204,12 +199,11 @@ pub enum DateFromFieldsError {
204199
/// use icu_calendar::error::DateFromFieldsError;
205200
/// use icu_calendar::cal::Hebrew;
206201
/// use icu_calendar::types::DateFields;
207-
/// use icu_calendar::types::MonthCode;
208202
/// use tinystr::tinystr;
209203
///
210204
/// let mut fields = DateFields::default();
211205
/// fields.extended_year = Some(5783);
212-
/// fields.month_code = Some(MonthCode(tinystr!(4, "M06")));
206+
/// fields.month_code = Some(b"M06");
213207
/// fields.ordinal_month = Some(6);
214208
/// fields.day = Some(1);
215209
///
@@ -242,7 +236,6 @@ pub enum DateFromFieldsError {
242236
/// use icu_calendar::error::DateFromFieldsError;
243237
/// use icu_calendar::cal::Hebrew;
244238
/// use icu_calendar::types::DateFields;
245-
/// use icu_calendar::types::MonthCode;
246239
/// use tinystr::tinystr;
247240
///
248241
/// let mut fields = DateFields::default();

components/calendar/src/options.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,13 @@ pub struct DateFromFieldsOptions {
5454
/// ```
5555
/// use icu::calendar::Date;
5656
/// use icu::calendar::types::DateFields;
57-
/// use icu::calendar::types::MonthCode;
5857
/// use icu::calendar::options::MissingFieldsStrategy;
5958
/// use icu::calendar::options::DateFromFieldsOptions;
6059
/// use icu::calendar::Iso;
6160
///
6261
/// // These options are missing a year.
6362
/// let mut fields = DateFields::default();
64-
/// fields.month_code = MonthCode::new_normal(2);
63+
/// fields.month_code = Some(b"M02");
6564
/// fields.day = Some(1);
6665
///
6766
/// let options_default = DateFromFieldsOptions::default();
@@ -232,16 +231,14 @@ pub enum Overflow {
232231
/// use icu_calendar::options::DateFromFieldsOptions;
233232
/// use icu_calendar::options::Overflow;
234233
/// use icu_calendar::types::DateFields;
235-
/// use icu_calendar::types::MonthCode;
236-
/// use tinystr::tinystr;
237234
///
238235
/// let mut options = DateFromFieldsOptions::default();
239236
/// options.overflow = Some(Overflow::Constrain);
240237
///
241238
/// // 5784, a leap year, contains M05L, but the day is too big.
242239
/// let mut fields = DateFields::default();
243240
/// fields.extended_year = Some(5784);
244-
/// fields.month_code = Some(MonthCode(tinystr!(4, "M05L")));
241+
/// fields.month_code = Some(b"M05L");
245242
/// fields.day = Some(50);
246243
///
247244
/// let date = Date::try_from_fields(
@@ -253,7 +250,7 @@ pub enum Overflow {
253250
///
254251
/// // Constrained to the 30th day of M05L of year 5784
255252
/// assert_eq!(date.year().extended_year(), 5784);
256-
/// assert_eq!(date.month().standard_code, MonthCode(tinystr!(4, "M05L")));
253+
/// assert_eq!(date.month().standard_code.0, "M05L");
257254
/// assert_eq!(date.day_of_month().0, 30);
258255
///
259256
/// // 5785, a common year, does not contain M05L.
@@ -268,7 +265,7 @@ pub enum Overflow {
268265
///
269266
/// // Constrained to the 29th day of M06 of year 5785
270267
/// assert_eq!(date.year().extended_year(), 5785);
271-
/// assert_eq!(date.month().standard_code, MonthCode(tinystr!(4, "M06")));
268+
/// assert_eq!(date.month().standard_code.0, "M06");
272269
/// assert_eq!(date.day_of_month().0, 29);
273270
/// ```
274271
Constrain,
@@ -283,7 +280,6 @@ pub enum Overflow {
283280
/// use icu_calendar::options::DateFromFieldsOptions;
284281
/// use icu_calendar::options::Overflow;
285282
/// use icu_calendar::types::DateFields;
286-
/// use icu_calendar::types::MonthCode;
287283
/// use tinystr::tinystr;
288284
///
289285
/// let mut options = DateFromFieldsOptions::default();
@@ -292,7 +288,7 @@ pub enum Overflow {
292288
/// // 5784, a leap year, contains M05L, but the day is too big.
293289
/// let mut fields = DateFields::default();
294290
/// fields.extended_year = Some(5784);
295-
/// fields.month_code = Some(MonthCode(tinystr!(4, "M05L")));
291+
/// fields.month_code = Some(b"M05L");
296292
/// fields.day = Some(50);
297293
///
298294
/// let err = Date::try_from_fields(
@@ -384,11 +380,7 @@ pub enum MissingFieldsStrategy {
384380

385381
#[cfg(test)]
386382
mod tests {
387-
use crate::{
388-
error::DateFromFieldsError,
389-
types::{DateFields, MonthCode},
390-
Date, Gregorian,
391-
};
383+
use crate::{error::DateFromFieldsError, types::DateFields, Date, Gregorian};
392384
use itertools::Itertools;
393385
use std::collections::{BTreeMap, BTreeSet};
394386

@@ -462,9 +454,7 @@ mod tests {
462454
field_fns.insert("era", &|fields| fields.era = Some(b"ad"));
463455
field_fns.insert("era_year", &|fields| fields.era_year = Some(2000));
464456
field_fns.insert("extended_year", &|fields| fields.extended_year = Some(2000));
465-
field_fns.insert("month_code", &|fields| {
466-
fields.month_code = MonthCode::new_normal(4)
467-
});
457+
field_fns.insert("month_code", &|fields| fields.month_code = Some(b"M04"));
468458
field_fns.insert("ordinal_month", &|fields| fields.ordinal_month = Some(4));
469459
field_fns.insert("day", &|fields| fields.day = Some(20));
470460

components/calendar/src/tests/not_enough_fields.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::error::DateFromFieldsError;
66
use crate::options::{DateFromFieldsOptions, MissingFieldsStrategy, Overflow};
7-
use crate::types::{DateFields, MonthCode};
7+
use crate::types::DateFields;
88
use crate::Date;
99

1010
#[test]
@@ -16,8 +16,8 @@ fn test_from_fields_not_enough_fields() {
1616
let big_u8 = Some(u8::MAX);
1717
let small_u8 = Some(1);
1818
let small_i32 = Some(5000);
19-
let valid_month_code = MonthCode::new_normal(1);
20-
let invalid_month_code = MonthCode::new_normal(99);
19+
let valid_month_code: Option<&[_]> = Some(b"M01");
20+
let invalid_month_code: Option<&[_]> = Some(b"M99");
2121

2222
// We want to ensure that most NotEnoughFields cases return NotEnoughFields
2323
// even when we're providing out-of-range values, so that

components/calendar/src/types.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct DateFields<'a> {
3636
/// refer to the same year.
3737
pub extended_year: Option<i32>,
3838
/// The [`MonthCode`] representing a valid month in this calendar year.
39-
pub month_code: Option<MonthCode>,
39+
pub month_code: Option<&'a [u8]>,
4040
/// See [`MonthInfo::ordinal`].
4141
///
4242
/// If both this and [`Self::month_code`] are set, they must refer to
@@ -292,6 +292,15 @@ pub(crate) struct ValidMonthCode {
292292
}
293293

294294
impl ValidMonthCode {
295+
#[inline]
296+
pub(crate) fn from_bytes(x: &[u8]) -> Result<Self, MonthCodeParseError> {
297+
let Ok(s) = TinyStr4::try_from_utf8(x) else {
298+
return Err(MonthCodeParseError::InvalidSyntax);
299+
};
300+
301+
MonthCode(s).validated()
302+
}
303+
295304
/// Create a new ValidMonthCode without checking that the number is between 0 and 99
296305
#[inline]
297306
pub(crate) fn new_unchecked(number: u8, is_leap: bool) -> Self {

components/calendar/tests/exhaustive.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,28 @@ fn check_round_trip() {
3939
fn check_from_fields() {
4040
fn test<C: Calendar>(cal: C) {
4141
let cal = Ref(&cal);
42+
43+
let codes = (1..19)
44+
.flat_map(|i| {
45+
[
46+
types::MonthCode::new_normal(i).unwrap(),
47+
types::MonthCode::new_leap(i).unwrap(),
48+
]
49+
.into_iter()
50+
})
51+
.collect::<Vec<_>>();
4252
for year in -MAGNITUDE..=MAGNITUDE {
4353
if year % 50000 == 0 {
4454
println!("{} {year:?}", cal.as_calendar().debug_name());
4555
}
4656
for overflow in [options::Overflow::Constrain, options::Overflow::Reject] {
4757
let mut options = options::DateFromFieldsOptions::default();
4858
options.overflow = Some(overflow);
49-
for mut fields in (1..19)
50-
.flat_map(|i| {
51-
[
52-
types::MonthCode::new_normal(i).unwrap(),
53-
types::MonthCode::new_leap(i).unwrap(),
54-
]
55-
.into_iter()
56-
})
59+
for mut fields in codes
60+
.iter()
5761
.map(|m| {
5862
let mut fields = types::DateFields::default();
59-
fields.month_code = Some(m);
63+
fields.month_code = Some(m.0.as_bytes());
6064
fields
6165
})
6266
.chain((1..20).map(|m| {

components/calendar/tests/reference_year.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ where
2727
let date = Date::from_rata_die(rd, Ref(&cal));
2828
let month_day = (date.month().standard_code, date.day_of_month().0);
2929
let mut fields = DateFields::default();
30-
fields.month_code = Some(month_day.0);
30+
fields.month_code = Some(month_day.0 .0.as_bytes());
3131
fields.day = Some(month_day.1);
3232
let mut options = DateFromFieldsOptions::default();
3333
options.missing_fields_strategy = Some(MissingFieldsStrategy::Ecma);
@@ -50,10 +50,11 @@ where
5050
valid_day_number = day_number;
5151
}
5252
let mut fields = DateFields::default();
53-
fields.month_code = match is_leap {
53+
let mc = match is_leap {
5454
false => MonthCode::new_normal(month_number),
5555
true => MonthCode::new_leap(month_number),
5656
};
57+
fields.month_code = mc.as_ref().map(|m| m.0.as_bytes());
5758
fields.day = Some(day_number);
5859
let mut options = DateFromFieldsOptions::default();
5960
options.overflow = Some(Overflow::Constrain);
@@ -81,7 +82,7 @@ where
8182
// Test round-trip (to valid day number)
8283
assert_eq!(
8384
fields.month_code.unwrap(),
84-
reference_date.month().standard_code,
85+
reference_date.month().standard_code.0.as_bytes(),
8586
"{fields:?} {cal:?}"
8687
);
8788
assert_eq!(

0 commit comments

Comments
 (0)