| 1 |
//! Part of a format description. |
| 2 |
|
| 3 |
use crate::format_description::modifier; |
| 4 |
|
| 5 |
/// A component of a larger format description. |
| 6 |
#[non_exhaustive] |
| 7 |
#[allow(deprecated)] |
| 8 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 |
pub enum Component { |
| 10 |
/// Day of the month. |
| 11 |
Day(modifier::Day), |
| 12 |
/// Month of the year in the abbreviated form (e.g. "Jan"). |
| 13 |
MonthShort(modifier::MonthShort), |
| 14 |
/// Month of the year in the full form (e.g. "January"). |
| 15 |
MonthLong(modifier::MonthLong), |
| 16 |
/// Month of the year in the numerical form (e.g. "1" for January). |
| 17 |
MonthNumerical(modifier::MonthNumerical), |
| 18 |
/// Ordinal day of the year. |
| 19 |
Ordinal(modifier::Ordinal), |
| 20 |
/// Weekday in the abbreviated form (e.g. "Mon"). |
| 21 |
WeekdayShort(modifier::WeekdayShort), |
| 22 |
/// Weekday in the full form (e.g. "Monday"). |
| 23 |
WeekdayLong(modifier::WeekdayLong), |
| 24 |
/// Weekday number where Sunday is either 0 or 1 depending on the modifier. |
| 25 |
WeekdaySunday(modifier::WeekdaySunday), |
| 26 |
/// Weekday number where Monday is either 0 or 1 depending on the modifier. |
| 27 |
WeekdayMonday(modifier::WeekdayMonday), |
| 28 |
/// Week number of the year, where week 1 starts is the week beginning on Monday that contains |
| 29 |
/// January 4. |
| 30 |
WeekNumberIso(modifier::WeekNumberIso), |
| 31 |
/// Week number of the year, where week 1 starts on the first Sunday of the calendar year. |
| 32 |
WeekNumberSunday(modifier::WeekNumberSunday), |
| 33 |
/// Week number of the year, where week 1 starts on the first Monday of the calendar year. |
| 34 |
WeekNumberMonday(modifier::WeekNumberMonday), |
| 35 |
/// The calendar year. Supports the extended range. |
| 36 |
CalendarYearFullExtendedRange(modifier::CalendarYearFullExtendedRange), |
| 37 |
/// The calendar year. Does not support the extended range. |
| 38 |
CalendarYearFullStandardRange(modifier::CalendarYearFullStandardRange), |
| 39 |
/// The ISO week-based year. Supports the extended range. |
| 40 |
IsoYearFullExtendedRange(modifier::IsoYearFullExtendedRange), |
| 41 |
/// The ISO week-based year. Does not support the extended range. |
| 42 |
IsoYearFullStandardRange(modifier::IsoYearFullStandardRange), |
| 43 |
/// The century of the calendar year. Supports the extended range. |
| 44 |
CalendarYearCenturyExtendedRange(modifier::CalendarYearCenturyExtendedRange), |
| 45 |
/// The century of the calendar year. Does not support the extended range. |
| 46 |
CalendarYearCenturyStandardRange(modifier::CalendarYearCenturyStandardRange), |
| 47 |
/// The century of the ISO week-based year. Supports the extended range. |
| 48 |
IsoYearCenturyExtendedRange(modifier::IsoYearCenturyExtendedRange), |
| 49 |
/// The century of the ISO week-based year. Does not support the extended range. |
| 50 |
IsoYearCenturyStandardRange(modifier::IsoYearCenturyStandardRange), |
| 51 |
/// The last two digits of the calendar year. |
| 52 |
CalendarYearLastTwo(modifier::CalendarYearLastTwo), |
| 53 |
/// The last two digits of the ISO week-based year. |
| 54 |
IsoYearLastTwo(modifier::IsoYearLastTwo), |
| 55 |
/// Hour of the day using the 12-hour clock. |
| 56 |
Hour12(modifier::Hour12), |
| 57 |
/// Hour of the day using the 24-hour clock. |
| 58 |
Hour24(modifier::Hour24), |
| 59 |
/// Minute within the hour. |
| 60 |
Minute(modifier::Minute), |
| 61 |
/// AM/PM part of the time. |
| 62 |
Period(modifier::Period), |
| 63 |
/// Second within the minute. |
| 64 |
Second(modifier::Second), |
| 65 |
/// Subsecond within the second. |
| 66 |
Subsecond(modifier::Subsecond), |
| 67 |
/// Hour of the UTC offset. |
| 68 |
OffsetHour(modifier::OffsetHour), |
| 69 |
/// Minute within the hour of the UTC offset. |
| 70 |
OffsetMinute(modifier::OffsetMinute), |
| 71 |
/// Second within the minute of the UTC offset. |
| 72 |
OffsetSecond(modifier::OffsetSecond), |
| 73 |
/// A number of bytes to ignore when parsing. This has no effect on formatting. |
| 74 |
Ignore(modifier::Ignore), |
| 75 |
/// A Unix timestamp in seconds. |
| 76 |
UnixTimestampSecond(modifier::UnixTimestampSecond), |
| 77 |
/// A Unix timestamp in milliseconds. |
| 78 |
UnixTimestampMillisecond(modifier::UnixTimestampMillisecond), |
| 79 |
/// A Unix timestamp in microseconds. |
| 80 |
UnixTimestampMicrosecond(modifier::UnixTimestampMicrosecond), |
| 81 |
/// A Unix timestamp in nanoseconds. |
| 82 |
UnixTimestampNanosecond(modifier::UnixTimestampNanosecond), |
| 83 |
/// The end of input. Parsing this component will fail if there is any input remaining. This |
| 84 |
/// component neither affects formatting nor consumes any input when parsing. |
| 85 |
End(modifier::End), |
| 86 |
|
| 87 |
// Start of deprecated components that are no longer emitted by macros or parsers. They must |
| 88 |
// be maintained for backward compatibility, as downstream users could have constructed them |
| 89 |
// manually. |
| 90 |
/// Month of the year. |
| 91 |
#[deprecated( |
| 92 |
since = "0.3.48", |
| 93 |
note = "use `MonthShort`, `MonthLong`, or `MonthNumeric` instead" |
| 94 |
)] |
| 95 |
Month(modifier::Month), |
| 96 |
/// Day of the week. |
| 97 |
#[deprecated( |
| 98 |
since = "0.3.48", |
| 99 |
note = "use `WeekdayShort`, `WeekdayLong`, or `WeekdaySunday`, or `WeekdayMonday` instead" |
| 100 |
)] |
| 101 |
Weekday(modifier::Weekday), |
| 102 |
/// Week within the year. |
| 103 |
#[deprecated( |
| 104 |
since = "0.3.48", |
| 105 |
note = "use `WeekNumberIso`, `WeekNumberSunday`, or `WeekNumberMonday` instead" |
| 106 |
)] |
| 107 |
WeekNumber(modifier::WeekNumber), |
| 108 |
/// Hour of the day. |
| 109 |
#[deprecated(since = "0.3.48", note = "use `Hour12` or `Hour24` instead")] |
| 110 |
Hour(modifier::Hour), |
| 111 |
/// A Unix timestamp. |
| 112 |
#[deprecated( |
| 113 |
since = "0.3.48", |
| 114 |
note = "use `UnixTimestampSeconds`, `UnixTimestampMilliseconds`, \ |
| 115 |
`UnixTimestampMicroseconds`, or `UnixTimestampNanoseconds` instead" |
| 116 |
)] |
| 117 |
UnixTimestamp(modifier::UnixTimestamp), |
| 118 |
/// Year of the date. |
| 119 |
#[deprecated( |
| 120 |
since = "0.3.48", |
| 121 |
note = "use one of the various `Year*` components instead" |
| 122 |
)] |
| 123 |
Year(modifier::Year), |
| 124 |
} |
| 125 |
|
| 126 |
impl From<Component> for super::format_description_v3::FormatDescriptionV3Inner<'_> { |
| 127 |
#[inline] |
| 128 |
fn from(component: Component) -> Self { |
| 129 |
match component { |
| 130 |
Component::Day(modifier) => Self::Day(modifier), |
| 131 |
Component::MonthShort(modifier) => Self::MonthShort(modifier), |
| 132 |
Component::MonthLong(modifier) => Self::MonthLong(modifier), |
| 133 |
Component::MonthNumerical(modifier) => Self::MonthNumerical(modifier), |
| 134 |
Component::Ordinal(modifier) => Self::Ordinal(modifier), |
| 135 |
Component::WeekdayShort(modifier) => Self::WeekdayShort(modifier), |
| 136 |
Component::WeekdayLong(modifier) => Self::WeekdayLong(modifier), |
| 137 |
Component::WeekdaySunday(modifier) => Self::WeekdaySunday(modifier), |
| 138 |
Component::WeekdayMonday(modifier) => Self::WeekdayMonday(modifier), |
| 139 |
Component::WeekNumberIso(modifier) => Self::WeekNumberIso(modifier), |
| 140 |
Component::WeekNumberSunday(modifier) => Self::WeekNumberSunday(modifier), |
| 141 |
Component::WeekNumberMonday(modifier) => Self::WeekNumberMonday(modifier), |
| 142 |
Component::CalendarYearFullExtendedRange(modifier) => { |
| 143 |
Self::CalendarYearFullExtendedRange(modifier) |
| 144 |
} |
| 145 |
Component::CalendarYearFullStandardRange(modifier) => { |
| 146 |
Self::CalendarYearFullStandardRange(modifier) |
| 147 |
} |
| 148 |
Component::IsoYearFullExtendedRange(modifier) => { |
| 149 |
Self::IsoYearFullExtendedRange(modifier) |
| 150 |
} |
| 151 |
Component::IsoYearFullStandardRange(modifier) => { |
| 152 |
Self::IsoYearFullStandardRange(modifier) |
| 153 |
} |
| 154 |
Component::CalendarYearCenturyExtendedRange(modifier) => { |
| 155 |
Self::CalendarYearCenturyExtendedRange(modifier) |
| 156 |
} |
| 157 |
Component::CalendarYearCenturyStandardRange(modifier) => { |
| 158 |
Self::CalendarYearCenturyStandardRange(modifier) |
| 159 |
} |
| 160 |
Component::IsoYearCenturyExtendedRange(modifier) => { |
| 161 |
Self::IsoYearCenturyExtendedRange(modifier) |
| 162 |
} |
| 163 |
Component::IsoYearCenturyStandardRange(modifier) => { |
| 164 |
Self::IsoYearCenturyStandardRange(modifier) |
| 165 |
} |
| 166 |
Component::CalendarYearLastTwo(modifier) => Self::CalendarYearLastTwo(modifier), |
| 167 |
Component::IsoYearLastTwo(modifier) => Self::IsoYearLastTwo(modifier), |
| 168 |
Component::Hour12(modifier) => Self::Hour12(modifier), |
| 169 |
Component::Hour24(modifier) => Self::Hour24(modifier), |
| 170 |
Component::Minute(modifier) => Self::Minute(modifier), |
| 171 |
Component::Period(modifier) => Self::Period(modifier), |
| 172 |
Component::Second(modifier) => Self::Second(modifier), |
| 173 |
Component::Subsecond(modifier) => Self::Subsecond(modifier), |
| 174 |
Component::OffsetHour(modifier) => Self::OffsetHour(modifier), |
| 175 |
Component::OffsetMinute(modifier) => Self::OffsetMinute(modifier), |
| 176 |
Component::OffsetSecond(modifier) => Self::OffsetSecond(modifier), |
| 177 |
Component::Ignore(modifier) => Self::Ignore(modifier), |
| 178 |
Component::UnixTimestampSecond(modifier) => Self::UnixTimestampSecond(modifier), |
| 179 |
Component::UnixTimestampMillisecond(modifier) => { |
| 180 |
Self::UnixTimestampMillisecond(modifier) |
| 181 |
} |
| 182 |
Component::UnixTimestampMicrosecond(modifier) => { |
| 183 |
Self::UnixTimestampMicrosecond(modifier) |
| 184 |
} |
| 185 |
Component::UnixTimestampNanosecond(modifier) => Self::UnixTimestampNanosecond(modifier), |
| 186 |
Component::End(modifier) => Self::End(modifier), |
| 187 |
|
| 188 |
// Start of deprecated components. |
| 189 |
#[expect(deprecated)] |
| 190 |
Component::Month(modifier) => match modifier.repr { |
| 191 |
modifier::MonthRepr::Short => Self::MonthShort( |
| 192 |
modifier::MonthShort::default().with_case_sensitive(modifier.case_sensitive), |
| 193 |
), |
| 194 |
modifier::MonthRepr::Long => Self::MonthLong( |
| 195 |
modifier::MonthLong::default().with_case_sensitive(modifier.case_sensitive), |
| 196 |
), |
| 197 |
modifier::MonthRepr::Numerical => Self::MonthNumerical( |
| 198 |
modifier::MonthNumerical::default().with_padding(modifier.padding), |
| 199 |
), |
| 200 |
}, |
| 201 |
#[expect(deprecated)] |
| 202 |
Component::Weekday(modifier) => match modifier.repr { |
| 203 |
modifier::WeekdayRepr::Short => Self::WeekdayShort( |
| 204 |
modifier::WeekdayShort::default().with_case_sensitive(modifier.case_sensitive), |
| 205 |
), |
| 206 |
modifier::WeekdayRepr::Long => Self::WeekdayLong( |
| 207 |
modifier::WeekdayLong::default().with_case_sensitive(modifier.case_sensitive), |
| 208 |
), |
| 209 |
modifier::WeekdayRepr::Sunday => Self::WeekdaySunday( |
| 210 |
modifier::WeekdaySunday::default().with_one_indexed(modifier.one_indexed), |
| 211 |
), |
| 212 |
modifier::WeekdayRepr::Monday => Self::WeekdayMonday( |
| 213 |
modifier::WeekdayMonday::default().with_one_indexed(modifier.one_indexed), |
| 214 |
), |
| 215 |
}, |
| 216 |
#[expect(deprecated)] |
| 217 |
Component::WeekNumber(modifier) => match modifier.repr { |
| 218 |
modifier::WeekNumberRepr::Iso => Self::WeekNumberIso( |
| 219 |
modifier::WeekNumberIso::default().with_padding(modifier.padding), |
| 220 |
), |
| 221 |
modifier::WeekNumberRepr::Sunday => Self::WeekNumberSunday( |
| 222 |
modifier::WeekNumberSunday::default().with_padding(modifier.padding), |
| 223 |
), |
| 224 |
modifier::WeekNumberRepr::Monday => Self::WeekNumberMonday( |
| 225 |
modifier::WeekNumberMonday::default().with_padding(modifier.padding), |
| 226 |
), |
| 227 |
}, |
| 228 |
#[expect(deprecated)] |
| 229 |
Component::Hour(modifier) => { |
| 230 |
if modifier.is_12_hour_clock { |
| 231 |
Self::Hour12(modifier::Hour12::default().with_padding(modifier.padding)) |
| 232 |
} else { |
| 233 |
Self::Hour24(modifier::Hour24::default().with_padding(modifier.padding)) |
| 234 |
} |
| 235 |
} |
| 236 |
#[expect(deprecated)] |
| 237 |
Component::UnixTimestamp(modifier) => match modifier.precision { |
| 238 |
modifier::UnixTimestampPrecision::Second => Self::UnixTimestampSecond( |
| 239 |
modifier::UnixTimestampSecond::default() |
| 240 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 241 |
), |
| 242 |
modifier::UnixTimestampPrecision::Millisecond => Self::UnixTimestampMillisecond( |
| 243 |
modifier::UnixTimestampMillisecond::default() |
| 244 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 245 |
), |
| 246 |
modifier::UnixTimestampPrecision::Microsecond => Self::UnixTimestampMicrosecond( |
| 247 |
modifier::UnixTimestampMicrosecond::default() |
| 248 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 249 |
), |
| 250 |
modifier::UnixTimestampPrecision::Nanosecond => Self::UnixTimestampNanosecond( |
| 251 |
modifier::UnixTimestampNanosecond::default() |
| 252 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 253 |
), |
| 254 |
}, |
| 255 |
#[expect(deprecated)] |
| 256 |
Component::Year(modifier) => { |
| 257 |
match (modifier.iso_week_based, modifier.repr, modifier.range) { |
| 258 |
(true, modifier::YearRepr::Full, modifier::YearRange::Standard) => { |
| 259 |
Self::IsoYearFullStandardRange( |
| 260 |
modifier::IsoYearFullStandardRange::default() |
| 261 |
.with_padding(modifier.padding) |
| 262 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 263 |
) |
| 264 |
} |
| 265 |
(true, modifier::YearRepr::Full, modifier::YearRange::Extended) => { |
| 266 |
Self::IsoYearFullExtendedRange( |
| 267 |
modifier::IsoYearFullExtendedRange::default() |
| 268 |
.with_padding(modifier.padding) |
| 269 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 270 |
) |
| 271 |
} |
| 272 |
(false, modifier::YearRepr::Full, modifier::YearRange::Standard) => { |
| 273 |
Self::CalendarYearFullStandardRange( |
| 274 |
modifier::CalendarYearFullStandardRange::default() |
| 275 |
.with_padding(modifier.padding) |
| 276 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 277 |
) |
| 278 |
} |
| 279 |
(false, modifier::YearRepr::Full, modifier::YearRange::Extended) => { |
| 280 |
Self::CalendarYearFullExtendedRange( |
| 281 |
modifier::CalendarYearFullExtendedRange::default() |
| 282 |
.with_padding(modifier.padding) |
| 283 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 284 |
) |
| 285 |
} |
| 286 |
(true, modifier::YearRepr::Century, modifier::YearRange::Standard) => { |
| 287 |
Self::IsoYearCenturyStandardRange( |
| 288 |
modifier::IsoYearCenturyStandardRange::default() |
| 289 |
.with_padding(modifier.padding) |
| 290 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 291 |
) |
| 292 |
} |
| 293 |
(true, modifier::YearRepr::Century, modifier::YearRange::Extended) => { |
| 294 |
Self::IsoYearCenturyExtendedRange( |
| 295 |
modifier::IsoYearCenturyExtendedRange::default() |
| 296 |
.with_padding(modifier.padding) |
| 297 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 298 |
) |
| 299 |
} |
| 300 |
(false, modifier::YearRepr::Century, modifier::YearRange::Standard) => { |
| 301 |
Self::CalendarYearCenturyStandardRange( |
| 302 |
modifier::CalendarYearCenturyStandardRange::default() |
| 303 |
.with_padding(modifier.padding) |
| 304 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 305 |
) |
| 306 |
} |
| 307 |
(false, modifier::YearRepr::Century, modifier::YearRange::Extended) => { |
| 308 |
Self::CalendarYearCenturyExtendedRange( |
| 309 |
modifier::CalendarYearCenturyExtendedRange::default() |
| 310 |
.with_padding(modifier.padding) |
| 311 |
.with_sign_is_mandatory(modifier.sign_is_mandatory), |
| 312 |
) |
| 313 |
} |
| 314 |
(true, modifier::YearRepr::LastTwo, modifier::YearRange::Standard) => { |
| 315 |
Self::IsoYearLastTwo( |
| 316 |
modifier::IsoYearLastTwo::default().with_padding(modifier.padding), |
| 317 |
) |
| 318 |
} |
| 319 |
(true, modifier::YearRepr::LastTwo, modifier::YearRange::Extended) => { |
| 320 |
Self::IsoYearLastTwo( |
| 321 |
modifier::IsoYearLastTwo::default().with_padding(modifier.padding), |
| 322 |
) |
| 323 |
} |
| 324 |
(false, modifier::YearRepr::LastTwo, modifier::YearRange::Standard) => { |
| 325 |
Self::CalendarYearLastTwo( |
| 326 |
modifier::CalendarYearLastTwo::default().with_padding(modifier.padding), |
| 327 |
) |
| 328 |
} |
| 329 |
(false, modifier::YearRepr::LastTwo, modifier::YearRange::Extended) => { |
| 330 |
Self::CalendarYearLastTwo( |
| 331 |
modifier::CalendarYearLastTwo::default().with_padding(modifier.padding), |
| 332 |
) |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
} |