Skip to content

Commit 016c46b

Browse files
authored
Rename Date::try_from_codes to Date::try_new (#7773)
Fixes #7769 Mostly just implements the result of the discussion. Nothing interesting, though I did notice that FFI still has `from_codes_in_calendar`. I think keeping that is actually the right call, since over FFI we do accept string codes. ## Changelog: This is a modification of the changelog in #7764, both PRs should be included in the changelog entry icu_calendar: Add `Date::try_new`, which replaces `Date::try_new_from_codes`, and takes typed year/month values. - New methods: `Date::try_new` (and primarily-internal `Calendar::new_date`) - New types: `InputYear`, `DateNewError`
1 parent 7244442 commit 016c46b

File tree

32 files changed

+162
-154
lines changed

32 files changed

+162
-154
lines changed

components/calendar/benches/date.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ fn date_benches(c: &mut Criterion) {
168168
"calendar/hebrew",
169169
&fxs,
170170
icu::calendar::cal::Hebrew,
171-
|y, m, d, c| {
172-
Date::try_from_codes(types::YearInput::Extended(y), types::Month::new(m), d, c).unwrap()
173-
},
171+
|y, m, d, _c| Date::try_new_hebrew_v2(y, types::Month::new(m), d).unwrap(),
174172
);
175173

176174
bench_calendar(

components/calendar/src/any_calendar.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ macro_rules! make_any_calendar {
6969
type Year = $crate::types::YearInfo;
7070
type DateCompatibilityError = $crate::error::MismatchedCalendarError;
7171

72-
fn from_codes2(
72+
fn new_date(
7373
&self,
7474
year: $crate::types::YearInput,
7575
month: $crate::types::Month,
7676
day: u8,
77-
) -> Result<Self::DateInner, $crate::error::DateFromCodesError> {
77+
) -> Result<Self::DateInner, $crate::error::DateNewError> {
7878
Ok(match self {
7979
$(
80-
&Self::$variant(ref c) => $any_date_ident::$variant(c.from_codes2(year, month, day)?),
80+
&Self::$variant(ref c) => $any_date_ident::$variant(c.new_date(year, month, day)?),
8181
)+
8282
})
8383
}
@@ -856,7 +856,7 @@ impl<C: IntoAnyCalendar> Date<C> {
856856
#[cfg(test)]
857857
mod tests {
858858
use super::*;
859-
use crate::error::DateFromCodesError;
859+
use crate::error::DateNewError;
860860
use crate::types::{Month, YearInput};
861861
use crate::Ref;
862862

@@ -871,9 +871,9 @@ mod tests {
871871
let input = if let Some((era, _)) = era {
872872
YearInput::EraYear(era, year)
873873
} else {
874-
YearInput::Extended(year)
874+
year.into()
875875
};
876-
let date = Date::try_from_codes(input, month, day, calendar).unwrap_or_else(|e| {
876+
let date = Date::try_new(input, month, day, calendar).unwrap_or_else(|e| {
877877
panic!(
878878
"Failed to construct date for {} with {era:?}, {year}, {month:?}, {day}: {e:?}",
879879
calendar.debug_name(),
@@ -923,14 +923,14 @@ mod tests {
923923
year: i32,
924924
month: Month,
925925
day: u8,
926-
error: DateFromCodesError,
926+
error: DateNewError,
927927
) {
928928
let input = if let Some((era, _)) = era {
929929
YearInput::EraYear(era, year)
930930
} else {
931-
YearInput::Extended(year)
931+
year.into()
932932
};
933-
let date = Date::try_from_codes(input, month, day, calendar);
933+
let date = Date::try_new(input, month, day, calendar);
934934
assert_eq!(
935935
date,
936936
Err(error),
@@ -952,7 +952,7 @@ mod tests {
952952
100,
953953
Month::new(13),
954954
1,
955-
DateFromCodesError::MonthNotInCalendar,
955+
DateNewError::MonthNotInCalendar,
956956
);
957957
}
958958

@@ -971,7 +971,7 @@ mod tests {
971971
100,
972972
Month::new(14),
973973
1,
974-
DateFromCodesError::MonthNotInCalendar,
974+
DateNewError::MonthNotInCalendar,
975975
);
976976
}
977977

@@ -984,7 +984,7 @@ mod tests {
984984
single_test_roundtrip(ethiopian, None, -100, Month::new(3), 1);
985985
single_test_roundtrip(ethiopian, Some(("am", Some(1))), 2000, Month::new(13), 1);
986986
single_test_roundtrip(ethiopian, Some(("aa", Some(0))), 5400, Month::new(3), 1);
987-
// Since #6910, the era range is not enforced in try_from_codes
987+
// Since #6910, the era range is not enforced in try_new
988988
/*
989989
single_test_error(
990990
ethiopian,
@@ -1019,7 +1019,7 @@ mod tests {
10191019
100,
10201020
Month::new(14),
10211021
1,
1022-
DateFromCodesError::MonthNotInCalendar,
1022+
DateNewError::MonthNotInCalendar,
10231023
);
10241024
}
10251025

@@ -1049,7 +1049,7 @@ mod tests {
10491049
100,
10501050
Month::new(14),
10511051
1,
1052-
DateFromCodesError::MonthNotInCalendar,
1052+
DateNewError::MonthNotInCalendar,
10531053
);
10541054
}
10551055

@@ -1061,7 +1061,7 @@ mod tests {
10611061
single_test_roundtrip(gregorian, None, 2000, Month::new(3), 1);
10621062
single_test_roundtrip(gregorian, None, -100, Month::new(3), 1);
10631063
single_test_roundtrip(gregorian, Some(("bce", Some(0))), 100, Month::new(3), 1);
1064-
// Since #6910, the era range is not enforced in try_from_codes
1064+
// Since #6910, the era range is not enforced in try_new
10651065
/*
10661066
single_test_error(
10671067
gregorian,
@@ -1096,7 +1096,7 @@ mod tests {
10961096
100,
10971097
Month::new(13),
10981098
1,
1099-
DateFromCodesError::MonthNotInCalendar,
1099+
DateNewError::MonthNotInCalendar,
11001100
);
11011101
}
11021102

@@ -1114,7 +1114,7 @@ mod tests {
11141114
100,
11151115
Month::new(13),
11161116
1,
1117-
DateFromCodesError::MonthNotInCalendar,
1117+
DateNewError::MonthNotInCalendar,
11181118
);
11191119
}
11201120

@@ -1131,7 +1131,7 @@ mod tests {
11311131
4658,
11321132
Month::new(13),
11331133
1,
1134-
DateFromCodesError::MonthNotInCalendar,
1134+
DateNewError::MonthNotInCalendar,
11351135
);
11361136
}
11371137

@@ -1148,7 +1148,7 @@ mod tests {
11481148
9393,
11491149
Month::leap(0),
11501150
1,
1151-
DateFromCodesError::MonthNotInCalendar,
1151+
DateNewError::MonthNotInCalendar,
11521152
);
11531153
}
11541154

@@ -1164,7 +1164,7 @@ mod tests {
11641164
single_test_roundtrip(japanese, None, -100, Month::new(3), 1);
11651165
single_test_roundtrip(japanese, None, 2024, Month::new(3), 1);
11661166
single_test_roundtrip(japanese, Some(("bce", Some(0))), 10, Month::new(3), 1);
1167-
// Since #6910, the era range is not enforced in try_from_codes
1167+
// Since #6910, the era range is not enforced in try_new
11681168
/*
11691169
single_test_error(
11701170
japanese,
@@ -1199,7 +1199,7 @@ mod tests {
11991199
2,
12001200
Month::new(13),
12011201
1,
1202-
DateFromCodesError::MonthNotInCalendar,
1202+
DateNewError::MonthNotInCalendar,
12031203
);
12041204
}
12051205

@@ -1217,7 +1217,7 @@ mod tests {
12171217
100,
12181218
Month::new(50),
12191219
1,
1220-
DateFromCodesError::MonthNotInCalendar,
1220+
DateNewError::MonthNotInCalendar,
12211221
);
12221222
}
12231223

@@ -1235,7 +1235,7 @@ mod tests {
12351235
100,
12361236
Month::new(50),
12371237
1,
1238-
DateFromCodesError::MonthNotInCalendar,
1238+
DateNewError::MonthNotInCalendar,
12391239
);
12401240
}
12411241

@@ -1276,7 +1276,7 @@ mod tests {
12761276
100,
12771277
Month::new(50),
12781278
1,
1279-
DateFromCodesError::MonthNotInCalendar,
1279+
DateNewError::MonthNotInCalendar,
12801280
);
12811281
}
12821282

@@ -1306,7 +1306,7 @@ mod tests {
13061306
100,
13071307
Month::new(50),
13081308
1,
1309-
DateFromCodesError::MonthNotInCalendar,
1309+
DateNewError::MonthNotInCalendar,
13101310
);
13111311
}
13121312

@@ -1337,7 +1337,7 @@ mod tests {
13371337
100,
13381338
Month::new(50),
13391339
1,
1340-
DateFromCodesError::MonthNotInCalendar,
1340+
DateNewError::MonthNotInCalendar,
13411341
);
13421342
}
13431343

@@ -1355,7 +1355,7 @@ mod tests {
13551355
100,
13561356
Month::new(13),
13571357
1,
1358-
DateFromCodesError::MonthNotInCalendar,
1358+
DateNewError::MonthNotInCalendar,
13591359
);
13601360
}
13611361
}

components/calendar/src/cal/abstract_gregorian.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::cal::iso::{IsoDateInner, IsoEra};
66
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver};
77
use crate::error::{
8-
DateAddError, DateFromCodesError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
8+
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, UnknownEraError,
99
};
1010
use crate::options::DateFromFieldsOptions;
1111
use crate::options::{DateAddOptions, DateDifferenceOptions};
@@ -86,12 +86,12 @@ impl<Y: GregorianYears> Calendar for AbstractGregorian<Y> {
8686
type Year = EraYear;
8787
type DateCompatibilityError = core::convert::Infallible;
8888

89-
fn from_codes2(
89+
fn new_date(
9090
&self,
9191
year: types::YearInput,
9292
month: types::Month,
9393
day: u8,
94-
) -> Result<Self::DateInner, DateFromCodesError> {
94+
) -> Result<Self::DateInner, DateNewError> {
9595
ArithmeticDate::from_input_year_month_code_day(year, month, day, self)
9696
.map(ArithmeticDate::cast)
9797
}
@@ -219,15 +219,15 @@ macro_rules! impl_with_abstract_gregorian {
219219
type DateInner = $inner_date_ty;
220220
type Year = types::EraYear;
221221
type DateCompatibilityError = core::convert::Infallible;
222-
fn from_codes2(
222+
fn new_date(
223223
&self,
224224
year: types::YearInput,
225225
month: types::Month,
226226
day: u8,
227-
) -> Result<Self::DateInner, crate::error::DateFromCodesError> {
227+
) -> Result<Self::DateInner, crate::error::DateNewError> {
228228
let $self_ident = self;
229229
crate::cal::abstract_gregorian::AbstractGregorian($eras_expr)
230-
.from_codes2(year, month, day)
230+
.new_date(year, month, day)
231231
.map($inner_date_ty)
232232
}
233233

components/calendar/src/cal/coptic.rs

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

55
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver};
66
use crate::error::{
7-
DateAddError, DateFromCodesError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
7+
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, UnknownEraError,
88
};
99
use crate::options::DateFromFieldsOptions;
1010
use crate::options::{DateAddOptions, DateDifferenceOptions};
@@ -131,12 +131,12 @@ impl Calendar for Coptic {
131131
type Year = types::EraYear;
132132
type DateCompatibilityError = core::convert::Infallible;
133133

134-
fn from_codes2(
134+
fn new_date(
135135
&self,
136136
year: types::YearInput,
137137
month: types::Month,
138138
day: u8,
139-
) -> Result<Self::DateInner, DateFromCodesError> {
139+
) -> Result<Self::DateInner, DateNewError> {
140140
ArithmeticDate::from_input_year_month_code_day(year, month, day, self).map(CopticDateInner)
141141
}
142142

components/calendar/src/cal/east_asian_traditional.rs

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

55
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver, PackWithMD, ToExtendedYear};
66
use crate::error::{
7-
DateAddError, DateError, DateFromCodesError, DateFromFieldsError, EcmaReferenceYearError,
7+
DateAddError, DateError, DateFromFieldsError, DateNewError, EcmaReferenceYearError,
88
LunisolarDateError, MonthError, UnknownEraError,
99
};
1010
use crate::options::{DateAddOptions, DateDifferenceOptions};
@@ -694,12 +694,12 @@ impl<R: Rules> Calendar for EastAsianTraditional<R> {
694694
type Year = types::CyclicYear;
695695
type DateCompatibilityError = R::DateCompatibilityError;
696696

697-
fn from_codes2(
697+
fn new_date(
698698
&self,
699699
year: types::YearInput,
700700
month: types::Month,
701701
day: u8,
702-
) -> Result<Self::DateInner, DateFromCodesError> {
702+
) -> Result<Self::DateInner, DateNewError> {
703703
ArithmeticDate::from_input_year_month_code_day(year, month, day, self).map(ChineseDateInner)
704704
}
705705

components/calendar/src/cal/ethiopian.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::cal::coptic::CopticDateInner;
66
use crate::cal::Coptic;
77
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver};
88
use crate::error::{
9-
DateAddError, DateFromCodesError, DateFromFieldsError, EcmaReferenceYearError, UnknownEraError,
9+
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, UnknownEraError,
1010
};
1111
use crate::options::DateFromFieldsOptions;
1212
use crate::options::{DateAddOptions, DateDifferenceOptions};
@@ -145,12 +145,12 @@ impl Calendar for Ethiopian {
145145
type Year = <Coptic as Calendar>::Year;
146146
type DateCompatibilityError = <Coptic as Calendar>::DateCompatibilityError;
147147

148-
fn from_codes2(
148+
fn new_date(
149149
&self,
150150
year: types::YearInput,
151151
month: types::Month,
152152
day: u8,
153-
) -> Result<Self::DateInner, DateFromCodesError> {
153+
) -> Result<Self::DateInner, DateNewError> {
154154
ArithmeticDate::from_input_year_month_code_day(year, month, day, self)
155155
.map(ArithmeticDate::cast)
156156
.map(CopticDateInner)

components/calendar/src/cal/hebrew.rs

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

55
use crate::calendar_arithmetic::{ArithmeticDate, DateFieldsResolver, PackWithMD, ToExtendedYear};
66
use crate::error::{
7-
DateAddError, DateFromCodesError, DateFromFieldsError, EcmaReferenceYearError,
8-
LunisolarDateError, MonthError, UnknownEraError,
7+
DateAddError, DateFromFieldsError, DateNewError, EcmaReferenceYearError, LunisolarDateError,
8+
MonthError, UnknownEraError,
99
};
1010
use crate::options::{DateAddOptions, DateDifferenceOptions};
1111
use crate::options::{DateFromFieldsOptions, Overflow};
@@ -256,12 +256,12 @@ impl Calendar for Hebrew {
256256
type Year = types::EraYear;
257257
type DateCompatibilityError = core::convert::Infallible;
258258

259-
fn from_codes2(
259+
fn new_date(
260260
&self,
261261
year: types::YearInput,
262262
month: Month,
263263
day: u8,
264-
) -> Result<Self::DateInner, DateFromCodesError> {
264+
) -> Result<Self::DateInner, DateNewError> {
265265
ArithmeticDate::from_input_year_month_code_day(year, month, day, self).map(HebrewDateInner)
266266
}
267267

@@ -528,7 +528,7 @@ mod tests {
528528
assert_eq!(date.day_of_month().0, d, "{date:?}");
529529

530530
assert_eq!(
531-
Date::try_from_codes(
531+
Date::try_new(
532532
types::YearInput::EraYear(&date.era_year().era, date.era_year().year),
533533
date.month().to_input(),
534534
date.day_of_month().0,

0 commit comments

Comments
 (0)