Debian Codegraph @

debian/rust-vendor/icu_datetime/src/pattern/mod.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 //! Lower-level, power-user APIs for formatting datetimes with pattern strings.
6 //!
7 //! ❗ This module forgoes most internationalization functionality of the datetime crate.
8 //! It assumes that the pattern is already localized for the customer's locale. Most clients
9 //! should use [`DateTimeFormatter`] instead of directly formatting with patterns.
10 //!
11 //! [`DateTimeFormatter`]: crate::DateTimeFormatter
12
13 mod formatter;
14 mod names;
15 #[expect(clippy::module_inception)] // the file pattern.rs should contain DateTimePattern
16 mod pattern;
17
18 pub use crate::error::ErrorField;
19 use crate::unchecked::MissingInputFieldKind;
20 pub use formatter::DateTimePatternFormatter;
21 pub use formatter::FormattedDateTimePattern;
22 use icu_calendar::AnyCalendarKind;
23 use icu_calendar::types::MonthCode;
24 use icu_pattern::SinglePlaceholderPattern;
25 pub use names::DateTimeNames;
26 pub(crate) use names::DateTimeNamesMetadata;
27 pub use names::DayPeriodNameLength;
28 pub use names::FixedCalendarDateTimeNames;
29 pub use names::MonthNameLength;
30 pub(crate) use names::RawDateTimeNames;
31 pub(crate) use names::RawDateTimeNamesBorrowed;
32 pub(crate) use names::TimeZoneDataPayloadsBorrowed;
33 pub use names::WeekdayNameLength;
34 pub use names::YearNameLength;
35 pub use pattern::DateTimePattern;
36 use tinystr::TinyStr16;
37
38 #[cfg(doc)]
39 use icu_calendar::types::CyclicYear;
40 #[cfg(doc)]
41 use icu_decimal::DecimalFormatter;
42 #[cfg(doc)]
43 use writeable::TryWriteable;
44
45 pub(crate) enum GetNameForMonthError {
46 InvalidMonthCode,
47 InvalidFieldLength,
48 NotLoaded,
49 }
50 pub(crate) enum GetNameForWeekdayError {
51 InvalidFieldLength,
52 NotLoaded,
53 }
54
55 pub(crate) enum GetNameForEraError {
56 InvalidEraCode,
57 InvalidFieldLength,
58 NotLoaded,
59 }
60
61 pub(crate) enum GetNameForCyclicYearError {
62 InvalidYearNumber { max: u8 },
63 InvalidFieldLength,
64 NotLoaded,
65 }
66
67 pub(crate) enum GetNameForDayPeriodError {
68 InvalidFieldLength,
69 NotLoaded,
70 }
71
72 /// Internal enum to represent the kinds of month symbols for interpolation
73 pub(crate) enum MonthPlaceholderValue<'a> {
74 PlainString(&'a str),
75 Numeric,
76 NumericPattern(&'a SinglePlaceholderPattern, i8),
77 StringPattern(&'a str, &'a SinglePlaceholderPattern),
78 }
79
80 /// Error returned from [`FixedCalendarDateTimeNames`]'s pattern load methods.
81 #[derive(Debug, Clone, Copy, PartialEq, displaydoc::Display)]
82 #[non_exhaustive]
83 pub enum PatternLoadError {
84 /// A field conflicts with a previous field.
85 ///
86 /// Fields conflict if they require the same type of data, for example the
87 /// `EEE` and `EEEE` fields (short vs long weekday) conflict, or the `M`
88 /// and `L` (format vs standalone month) conflict.
89 #[displaydoc("A field {field:?} conflicts with a previously loaded field {previous_field:?}.")]
90 ConflictingField {
91 /// The field that was not able to be loaded.
92 field: ErrorField,
93 /// The field that prevented the new field from being loaded.
94 previous_field: ErrorField,
95 },
96 /// The field symbol is not supported in that length.
97 ///
98 /// Some fields, such as `O` are not defined for all lengths (e.g. `OO`).
99 #[displaydoc("The field {0:?} symbol is not supported in that length.")]
100 UnsupportedLength(ErrorField),
101 /// The specific formatter does not support this field.
102 ///
103 /// This happens for example when trying to load a month field
104 /// on a [`FixedCalendarDateTimeNames<Gregorian, ZoneFieldSet>`].
105 #[displaydoc("The specific formatter does not support the field {0:?}.")]
106 FormatterTooSpecific(ErrorField),
107 /// An error arising from the [`data provider`](icu_provider) for loading names.
108 #[displaydoc("Problem loading data for field {1:?}: {0}")]
109 Data(icu_provider::DataError, ErrorField),
110 }
111
112 impl core::error::Error for PatternLoadError {}
113
114 /// Error returned from constructors that map from [`AnyCalendar`](icu_calendar::AnyCalendar) to a formatter.
115 #[derive(Debug, Clone, Copy, PartialEq, displaydoc::Display)]
116 #[displaydoc("The calendar {kind:?} is not supported in DateTimeFormatter")]
117 #[non_exhaustive]
118 pub struct UnsupportedCalendarError {
119 /// The calendar kind that is not supported.
120 pub kind: AnyCalendarKind,
121 }
122
123 impl core::error::Error for UnsupportedCalendarError {}
124
125 #[non_exhaustive]
126 #[derive(Debug, PartialEq, Copy, Clone, displaydoc::Display)]
127 /// Error for the [`TryWriteable`] implementation of [`FormattedDateTimePattern`].
128 ///
129 /// There are 3 general conditions for these errors to occur:
130 ///
131 /// 1. Invariants of **unchecked functions** are not upheld
132 /// 2. Invariants of **locale data** are not upheld
133 /// 3. Invariants of **trait impls** are not upheld (including [scaffolding traits])
134 ///
135 /// It is not always possible to distinguish the source of the errors. Each variant is documented
136 /// with rules of thumb for when they might occur.
137 ///
138 /// [unstable scaffolding traits]: crate::scaffold
139 pub enum FormattedDateTimePatternError {
140 /// The [`MonthCode`] of the input is not valid for this calendar.
141 ///
142 /// Error conditions:
143 ///
144 /// - **Locale data:** for example, datetime names don't match the formatter's calendar
145 /// - **Trait impls:** for example, the date returns fields for the wrong calendar
146 ///
147 /// The output will contain the raw [`MonthCode`] as a fallback value.
148 #[displaydoc("Invalid month {0:?}")]
149 InvalidMonthCode(MonthCode),
150 /// The era code of the input is not valid for this calendar.
151 ///
152 /// Same error conditions as [`FormattedDateTimePatternError::InvalidMonthCode`].
153 ///
154 /// The output will contain the era code as the fallback.
155 #[displaydoc("Invalid era {0:?}")]
156 InvalidEra(TinyStr16),
157 /// The [`CyclicYear::year`] of the input is not valid for this calendar.
158 ///
159 /// Same error conditions as [`FormattedDateTimePatternError::InvalidMonthCode`].
160 ///
161 /// The output will contain [`CyclicYear::related_iso`] as a fallback value.
162 #[displaydoc("Invalid cyclic year {value} (maximum {max})")]
163 InvalidCyclicYear {
164 /// Value
165 value: u8,
166 /// Max
167 max: u8,
168 },
169
170 /// The localized names for a field have not been loaded.
171 ///
172 /// Error conditions:
173 ///
174 /// - **Unchecked functions:** for example, the pattern in [`with_pattern_unchecked`] contains fields that haven't been loaded
175 /// - **Trait impls:** for example, a custom field set does not include the correct names data
176 ///
177 /// The output will contain fallback values using field identifiers (such as `tue` for `Weekday::Tuesday`,
178 /// `M02` for month 2, etc.).
179 ///
180 /// [`with_pattern_unchecked`]: FixedCalendarDateTimeNames::with_pattern_unchecked
181 #[displaydoc("Names for {0:?} not loaded")]
182 NamesNotLoaded(ErrorField),
183 /// The [`DecimalFormatter`] has not been loaded.
184 ///
185 /// Same error conditions as [`FormattedDateTimePatternError::NamesNotLoaded`].
186 ///
187 /// The output will contain fallback values using Latin numerals.
188 #[displaydoc("DecimalFormatter not loaded")]
189 DecimalFormatterNotLoaded,
190
191 /// An input field (such as "hour" or "month") is missing.
192 ///
193 /// Error conditions:
194 ///
195 /// - **Unchecked functions:** for example, the pattern in [`with_pattern_unchecked`] contains fields that aren't in the fieldset
196 /// - **Trait impls:** for example, a custom field set does not require the correct fields
197 ///
198 /// The output will contain the string `{X}` instead, where `X` is the symbol for which the input is missing.
199 ///
200 /// [`with_pattern_unchecked`]: FixedCalendarDateTimeNames::with_pattern_unchecked
201 #[displaydoc("Incomplete input, missing value for {0:?}")]
202 MissingInputField(MissingInputFieldKind),
203
204 /// The pattern contains a field symbol for which formatting is unsupported.
205 ///
206 /// Error conditions:
207 ///
208 /// - **Unchecked functions:** for example, the pattern in [`with_pattern_unchecked`] contains an unsupported field
209 ///
210 /// The output will contain the string `{unsupported:X}`, where `X` is the symbol of the unsupported field.
211 ///
212 /// [`with_pattern_unchecked`]: FixedCalendarDateTimeNames::with_pattern_unchecked
213 #[displaydoc("Unsupported field {0:?}")]
214 UnsupportedField(ErrorField),
215 /// The pattern contains a field that has a valid symbol but invalid length.
216 ///
217 /// Same error conditions as [`FormattedDateTimePatternError::UnsupportedField`].
218 ///
219 /// The output will contain fallback values similar to [`FormattedDateTimePatternError::NamesNotLoaded`].
220 #[displaydoc("Field length for {0:?} is invalid")]
221 UnsupportedLength(ErrorField),
222 }