1//! A version 3 format description.
2//!
3//! Unlike versions 1 and 2, this is opaque so as to permit any changes necessary without breaking
4//! downstream users. Other than `FormatDescriptionV3`, all items are internal.
56#[cfg(feature = "alloc")]
7use alloc::boxed::Box;
8use core::fmt;
910use crate::format_description::modifier;
1112/// A complete description of how to format and parse a type.
13///
14/// Both for forwards compatibility and to enable optimizations, this type is deliberately opaque
15/// and cannot be constructed by users of the crate. Instead, it is returned by the
16/// `format_description!` macro (when `version=3` is used) as well as the `parse_borrowed` and
17/// `parse_owned` methods.
18#[derive(#[automatically_derived]
impl<'a> ::core::clone::Clone for FormatDescriptionV3<'a> {
#[inline]
fn clone(&self) -> FormatDescriptionV3<'a> {
FormatDescriptionV3 {
inner: ::core::clone::Clone::clone(&self.inner),
max_bytes_needed: ::core::clone::Clone::clone(&self.max_bytes_needed),
}
}
}Clone)]
19pub struct FormatDescriptionV3<'a> {
20/// The inner `enum` that controls all business logic.
21pub(crate) inner: FormatDescriptionV3Inner<'a>,
22/// The maximum number of bytes that are needed to format any value using this format
23 /// description.
24#[cfg(feature = "formatting")]
25pub(crate) max_bytes_needed: usize,
26}
2728impl fmt::Debugfor FormatDescriptionV3<'_> {
29#[inline]
30fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31self.inner.fmt(f)
32 }
33}
3435impl FormatDescriptionV3<'_> {
36/// Convert the format description to an owned version, enabling it to be stored without regard
37 /// for lifetime.
38#[cfg(feature = "alloc")]
39 #[inline]
40pub fn to_owned(self) -> FormatDescriptionV3<'static> {
41FormatDescriptionV3 {
42 inner: self.inner.into_owned(),
43#[cfg(feature = "formatting")]
44max_bytes_needed: self.max_bytes_needed,
45 }
46 }
47}
4849/// The inner `enum` of a version 3 format description. Controls all business logic.
50// public via `crate::format_description::__private` for macro use
51#[non_exhaustive]
52#[derive(#[automatically_derived]
impl<'a> ::core::clone::Clone for FormatDescriptionV3Inner<'a> {
#[inline]
fn clone(&self) -> FormatDescriptionV3Inner<'a> {
match self {
FormatDescriptionV3Inner::Day(__self_0) =>
FormatDescriptionV3Inner::Day(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::MonthShort(__self_0) =>
FormatDescriptionV3Inner::MonthShort(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::MonthLong(__self_0) =>
FormatDescriptionV3Inner::MonthLong(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::MonthNumerical(__self_0) =>
FormatDescriptionV3Inner::MonthNumerical(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Ordinal(__self_0) =>
FormatDescriptionV3Inner::Ordinal(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekdayShort(__self_0) =>
FormatDescriptionV3Inner::WeekdayShort(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekdayLong(__self_0) =>
FormatDescriptionV3Inner::WeekdayLong(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekdaySunday(__self_0) =>
FormatDescriptionV3Inner::WeekdaySunday(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekdayMonday(__self_0) =>
FormatDescriptionV3Inner::WeekdayMonday(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekNumberIso(__self_0) =>
FormatDescriptionV3Inner::WeekNumberIso(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekNumberSunday(__self_0) =>
FormatDescriptionV3Inner::WeekNumberSunday(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::WeekNumberMonday(__self_0) =>
FormatDescriptionV3Inner::WeekNumberMonday(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::CalendarYearFullExtendedRange(__self_0)
=>
FormatDescriptionV3Inner::CalendarYearFullExtendedRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::CalendarYearFullStandardRange(__self_0)
=>
FormatDescriptionV3Inner::CalendarYearFullStandardRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::IsoYearFullExtendedRange(__self_0) =>
FormatDescriptionV3Inner::IsoYearFullExtendedRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::IsoYearFullStandardRange(__self_0) =>
FormatDescriptionV3Inner::IsoYearFullStandardRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::CalendarYearCenturyExtendedRange(__self_0)
=>
FormatDescriptionV3Inner::CalendarYearCenturyExtendedRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::CalendarYearCenturyStandardRange(__self_0)
=>
FormatDescriptionV3Inner::CalendarYearCenturyStandardRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::IsoYearCenturyExtendedRange(__self_0) =>
FormatDescriptionV3Inner::IsoYearCenturyExtendedRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::IsoYearCenturyStandardRange(__self_0) =>
FormatDescriptionV3Inner::IsoYearCenturyStandardRange(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::CalendarYearLastTwo(__self_0) =>
FormatDescriptionV3Inner::CalendarYearLastTwo(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::IsoYearLastTwo(__self_0) =>
FormatDescriptionV3Inner::IsoYearLastTwo(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Hour12(__self_0) =>
FormatDescriptionV3Inner::Hour12(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Hour24(__self_0) =>
FormatDescriptionV3Inner::Hour24(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Minute(__self_0) =>
FormatDescriptionV3Inner::Minute(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Period(__self_0) =>
FormatDescriptionV3Inner::Period(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Second(__self_0) =>
FormatDescriptionV3Inner::Second(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Subsecond(__self_0) =>
FormatDescriptionV3Inner::Subsecond(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::OffsetHour(__self_0) =>
FormatDescriptionV3Inner::OffsetHour(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::OffsetMinute(__self_0) =>
FormatDescriptionV3Inner::OffsetMinute(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::OffsetSecond(__self_0) =>
FormatDescriptionV3Inner::OffsetSecond(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::Ignore(__self_0) =>
FormatDescriptionV3Inner::Ignore(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::UnixTimestampSecond(__self_0) =>
FormatDescriptionV3Inner::UnixTimestampSecond(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::UnixTimestampMillisecond(__self_0) =>
FormatDescriptionV3Inner::UnixTimestampMillisecond(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::UnixTimestampMicrosecond(__self_0) =>
FormatDescriptionV3Inner::UnixTimestampMicrosecond(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::UnixTimestampNanosecond(__self_0) =>
FormatDescriptionV3Inner::UnixTimestampNanosecond(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::End(__self_0) =>
FormatDescriptionV3Inner::End(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::BorrowedLiteral(__self_0) =>
FormatDescriptionV3Inner::BorrowedLiteral(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::BorrowedCompound(__self_0) =>
FormatDescriptionV3Inner::BorrowedCompound(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::BorrowedOptional {
format: __self_0, item: __self_1 } =>
FormatDescriptionV3Inner::BorrowedOptional {
format: ::core::clone::Clone::clone(__self_0),
item: ::core::clone::Clone::clone(__self_1),
},
FormatDescriptionV3Inner::BorrowedFirst(__self_0) =>
FormatDescriptionV3Inner::BorrowedFirst(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::OwnedLiteral(__self_0) =>
FormatDescriptionV3Inner::OwnedLiteral(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::OwnedCompound(__self_0) =>
FormatDescriptionV3Inner::OwnedCompound(::core::clone::Clone::clone(__self_0)),
FormatDescriptionV3Inner::OwnedOptional {
format: __self_0, item: __self_1 } =>
FormatDescriptionV3Inner::OwnedOptional {
format: ::core::clone::Clone::clone(__self_0),
item: ::core::clone::Clone::clone(__self_1),
},
FormatDescriptionV3Inner::OwnedFirst(__self_0) =>
FormatDescriptionV3Inner::OwnedFirst(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
53pub enum FormatDescriptionV3Inner<'a> {
54/// Day of the month.
55Day(modifier::Day),
56/// Month of the year in the abbreviated form (e.g. "Jan").
57MonthShort(modifier::MonthShort),
58/// Month of the year in the full form (e.g. "January").
59MonthLong(modifier::MonthLong),
60/// Month of the year in the numerical form (e.g. "1" for January).
61MonthNumerical(modifier::MonthNumerical),
62/// Ordinal day of the year.
63Ordinal(modifier::Ordinal),
64/// Weekday in the abbreviated form (e.g. "Mon").
65WeekdayShort(modifier::WeekdayShort),
66/// Weekday in the full form (e.g. "Monday").
67WeekdayLong(modifier::WeekdayLong),
68/// Weekday number where Sunday is either 0 or 1 depending on the modifier.
69WeekdaySunday(modifier::WeekdaySunday),
70/// Weekday number where Monday is either 0 or 1 depending on the modifier.
71WeekdayMonday(modifier::WeekdayMonday),
72/// Week number of the year, where week 1 starts is the week beginning on Monday that contains
73 /// January 4.
74WeekNumberIso(modifier::WeekNumberIso),
75/// Week number of the year, where week 1 starts on the first Sunday of the calendar year.
76WeekNumberSunday(modifier::WeekNumberSunday),
77/// Week number of the year, where week 1 starts on the first Monday of the calendar year.
78WeekNumberMonday(modifier::WeekNumberMonday),
79/// The calendar year. Supports the extended range.
80CalendarYearFullExtendedRange(modifier::CalendarYearFullExtendedRange),
81/// The calendar year. Does not support the extended range.
82CalendarYearFullStandardRange(modifier::CalendarYearFullStandardRange),
83/// The ISO week-based year. Supports the extended range.
84IsoYearFullExtendedRange(modifier::IsoYearFullExtendedRange),
85/// The ISO week-based year. Does not support the extended range.
86IsoYearFullStandardRange(modifier::IsoYearFullStandardRange),
87/// The century of the calendar year. Supports the extended range.
88CalendarYearCenturyExtendedRange(modifier::CalendarYearCenturyExtendedRange),
89/// The century of the calendar year. Does not support the extended range.
90CalendarYearCenturyStandardRange(modifier::CalendarYearCenturyStandardRange),
91/// The century of the ISO week-based year. Supports the extended range.
92IsoYearCenturyExtendedRange(modifier::IsoYearCenturyExtendedRange),
93/// The century of the ISO week-based year. Does not support the extended range.
94IsoYearCenturyStandardRange(modifier::IsoYearCenturyStandardRange),
95/// The last two digits of the calendar year.
96CalendarYearLastTwo(modifier::CalendarYearLastTwo),
97/// The last two digits of the ISO week-based year.
98IsoYearLastTwo(modifier::IsoYearLastTwo),
99/// Hour of the day using the 12-hour clock.
100Hour12(modifier::Hour12),
101/// Hour of the day using the 24-hour clock.
102Hour24(modifier::Hour24),
103/// Minute within the hour.
104Minute(modifier::Minute),
105/// AM/PM part of the time.
106Period(modifier::Period),
107/// Second within the minute.
108Second(modifier::Second),
109/// Subsecond within the second.
110Subsecond(modifier::Subsecond),
111/// Hour of the UTC offset.
112OffsetHour(modifier::OffsetHour),
113/// Minute within the hour of the UTC offset.
114OffsetMinute(modifier::OffsetMinute),
115/// Second within the minute of the UTC offset.
116OffsetSecond(modifier::OffsetSecond),
117/// A number of bytes to ignore when parsing. This has no effect on formatting.
118Ignore(modifier::Ignore),
119/// A Unix timestamp in seconds.
120UnixTimestampSecond(modifier::UnixTimestampSecond),
121/// A Unix timestamp in milliseconds.
122UnixTimestampMillisecond(modifier::UnixTimestampMillisecond),
123/// A Unix timestamp in microseconds.
124UnixTimestampMicrosecond(modifier::UnixTimestampMicrosecond),
125/// A Unix timestamp in nanoseconds.
126UnixTimestampNanosecond(modifier::UnixTimestampNanosecond),
127/// The end of input. Parsing this component will fail if there is any input remaining. This
128 /// component neither affects formatting nor consumes any input when parsing.
129End(modifier::End),
130/// A string that is formatted as-is.
131BorrowedLiteral(&'a str),
132/// A series of literals or components that collectively form a partial or complete description.
133BorrowedCompound(&'a [Self]),
134/// An item that may or may not be present when parsing. If parsing fails, there will be no
135 /// effect on the resulting `struct`.
136BorrowedOptional {
137/// Whether the item should be formatted.
138format: bool,
139/// The item in question.
140item: &'a Self,
141 },
142/// A series of items where, when parsing, the first successful parse is used. When formatting,
143 /// the first item is used. If no items are present, both formatting and parsing are no-ops.
144BorrowedFirst(&'a [Self]),
145/// A string that is formatted as-is.
146#[cfg(feature = "alloc")]
147OwnedLiteral(Box<str>),
148/// A series of literals or components that collectively form a partial or complete description.
149#[cfg(feature = "alloc")]
150OwnedCompound(Box<[Self]>),
151/// An item that may or may not be present when parsing. If parsing fails, there will be no
152 /// effect on the resulting `struct`.
153#[cfg(feature = "alloc")]
154OwnedOptional {
155/// Whether the item should be formatted.
156format: bool,
157/// The item in question.
158item: Box<Self>,
159 },
160/// A series of items where, when parsing, the first successful parse is used. When formatting,
161 /// the first item is used. If no items are present, both formatting and parsing are no-ops.
162#[cfg(feature = "alloc")]
163OwnedFirst(Box<[Self]>),
164}
165166impl fmt::Debugfor FormatDescriptionV3Inner<'_> {
167#[inline]
168fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169match self {
170Self::Day(modifier) => modifier.fmt(f),
171Self::MonthShort(modifier) => modifier.fmt(f),
172Self::MonthLong(modifier) => modifier.fmt(f),
173Self::MonthNumerical(modifier) => modifier.fmt(f),
174Self::Ordinal(modifier) => modifier.fmt(f),
175Self::WeekdayShort(modifier) => modifier.fmt(f),
176Self::WeekdayLong(modifier) => modifier.fmt(f),
177Self::WeekdaySunday(modifier) => modifier.fmt(f),
178Self::WeekdayMonday(modifier) => modifier.fmt(f),
179Self::WeekNumberIso(modifier) => modifier.fmt(f),
180Self::WeekNumberSunday(modifier) => modifier.fmt(f),
181Self::WeekNumberMonday(modifier) => modifier.fmt(f),
182Self::CalendarYearFullExtendedRange(modifier) => modifier.fmt(f),
183Self::CalendarYearFullStandardRange(modifier) => modifier.fmt(f),
184Self::IsoYearFullExtendedRange(modifier) => modifier.fmt(f),
185Self::IsoYearFullStandardRange(modifier) => modifier.fmt(f),
186Self::CalendarYearCenturyExtendedRange(modifier) => modifier.fmt(f),
187Self::CalendarYearCenturyStandardRange(modifier) => modifier.fmt(f),
188Self::IsoYearCenturyExtendedRange(modifier) => modifier.fmt(f),
189Self::IsoYearCenturyStandardRange(modifier) => modifier.fmt(f),
190Self::CalendarYearLastTwo(modifier) => modifier.fmt(f),
191Self::IsoYearLastTwo(modifier) => modifier.fmt(f),
192Self::Hour12(modifier) => modifier.fmt(f),
193Self::Hour24(modifier) => modifier.fmt(f),
194Self::Minute(modifier) => modifier.fmt(f),
195Self::Period(modifier) => modifier.fmt(f),
196Self::Second(modifier) => modifier.fmt(f),
197Self::Subsecond(modifier) => modifier.fmt(f),
198Self::OffsetHour(modifier) => modifier.fmt(f),
199Self::OffsetMinute(modifier) => modifier.fmt(f),
200Self::OffsetSecond(modifier) => modifier.fmt(f),
201Self::Ignore(modifier) => modifier.fmt(f),
202Self::UnixTimestampSecond(modifier) => modifier.fmt(f),
203Self::UnixTimestampMillisecond(modifier) => modifier.fmt(f),
204Self::UnixTimestampMicrosecond(modifier) => modifier.fmt(f),
205Self::UnixTimestampNanosecond(modifier) => modifier.fmt(f),
206Self::End(modifier) => modifier.fmt(f),
207Self::BorrowedLiteral(literal) => f.debug_tuple("Literal").field(literal).finish(),
208Self::BorrowedCompound(compound) => f.debug_tuple("Compound").field(compound).finish(),
209Self::BorrowedOptional {
210 format: should_format,
211 item,
212 } => f213 .debug_struct("Optional")
214 .field("should_format", should_format)
215 .field("item", item)
216 .finish(),
217Self::BorrowedFirst(items) => f.debug_tuple("First").field(items).finish(),
218#[cfg(feature = "alloc")]
219Self::OwnedLiteral(literal) => f.debug_tuple("Literal").field(literal).finish(),
220#[cfg(feature = "alloc")]
221Self::OwnedCompound(compound) => f.debug_tuple("Compound").field(compound).finish(),
222#[cfg(feature = "alloc")]
223Self::OwnedOptional {
224 format: should_format,
225 item,
226 } => f227 .debug_struct("Optional")
228 .field("should_format", should_format)
229 .field("item", item)
230 .finish(),
231#[cfg(feature = "alloc")]
232Self::OwnedFirst(items) => f.debug_tuple("First").field(items).finish(),
233 }
234 }
235}
236237impl<'a> FormatDescriptionV3Inner<'a> {
238/// Recursively convert to an owned version, doing so in-place when possible.
239#[cfg(feature = "alloc")]
240fn make_owned_in_place(&mut self) {
241use alloc::borrow::ToOwnedas _;
242use alloc::boxed::Box;
243244match self {
245Self::BorrowedLiteral(literal) => {
246*self = Self::OwnedLiteral(literal.to_owned().into_boxed_str());
247 }
248Self::BorrowedCompound(compound) => {
249*self = Self::OwnedCompound(
250compound251 .iter()
252 .cloned()
253 .map(|item| item.into_owned())
254 .collect(),
255 );
256 }
257Self::BorrowedOptional { format, item } => {
258*self = Self::OwnedOptional {
259 format: *format,
260 item: Box::new(item.clone().into_owned()),
261 };
262 }
263Self::BorrowedFirst(items) => {
264*self = Self::OwnedFirst(
265items266 .iter()
267 .cloned()
268 .map(|item| item.into_owned())
269 .collect(),
270 );
271 }
272Self::OwnedCompound(compound) => {
273for item in compound {
274 item.make_owned_in_place();
275 }
276 }
277Self::OwnedOptional { format: _, item } => {
278item.make_owned_in_place();
279 }
280Self::OwnedFirst(items) => {
281for item in items {
282 item.make_owned_in_place();
283 }
284 }
285 FormatDescriptionV3Inner::Day(_)
286 | FormatDescriptionV3Inner::MonthShort(_)
287 | FormatDescriptionV3Inner::MonthLong(_)
288 | FormatDescriptionV3Inner::MonthNumerical(_)
289 | FormatDescriptionV3Inner::Ordinal(_)
290 | FormatDescriptionV3Inner::WeekdayShort(_)
291 | FormatDescriptionV3Inner::WeekdayLong(_)
292 | FormatDescriptionV3Inner::WeekdaySunday(_)
293 | FormatDescriptionV3Inner::WeekdayMonday(_)
294 | FormatDescriptionV3Inner::WeekNumberIso(_)
295 | FormatDescriptionV3Inner::WeekNumberSunday(_)
296 | FormatDescriptionV3Inner::WeekNumberMonday(_)
297 | FormatDescriptionV3Inner::CalendarYearFullExtendedRange(_)
298 | FormatDescriptionV3Inner::CalendarYearFullStandardRange(_)
299 | FormatDescriptionV3Inner::IsoYearFullExtendedRange(_)
300 | FormatDescriptionV3Inner::IsoYearFullStandardRange(_)
301 | FormatDescriptionV3Inner::CalendarYearCenturyExtendedRange(_)
302 | FormatDescriptionV3Inner::CalendarYearCenturyStandardRange(_)
303 | FormatDescriptionV3Inner::IsoYearCenturyExtendedRange(_)
304 | FormatDescriptionV3Inner::IsoYearCenturyStandardRange(_)
305 | FormatDescriptionV3Inner::CalendarYearLastTwo(_)
306 | FormatDescriptionV3Inner::IsoYearLastTwo(_)
307 | FormatDescriptionV3Inner::Hour12(_)
308 | FormatDescriptionV3Inner::Hour24(_)
309 | FormatDescriptionV3Inner::Minute(_)
310 | FormatDescriptionV3Inner::Period(_)
311 | FormatDescriptionV3Inner::Second(_)
312 | FormatDescriptionV3Inner::Subsecond(_)
313 | FormatDescriptionV3Inner::OffsetHour(_)
314 | FormatDescriptionV3Inner::OffsetMinute(_)
315 | FormatDescriptionV3Inner::OffsetSecond(_)
316 | FormatDescriptionV3Inner::Ignore(_)
317 | FormatDescriptionV3Inner::UnixTimestampSecond(_)
318 | FormatDescriptionV3Inner::UnixTimestampMillisecond(_)
319 | FormatDescriptionV3Inner::UnixTimestampMicrosecond(_)
320 | FormatDescriptionV3Inner::UnixTimestampNanosecond(_)
321 | FormatDescriptionV3Inner::End(_)
322 | FormatDescriptionV3Inner::OwnedLiteral(_) => {
323// no-op, as these variants do not contain any references
324}
325 }
326 }
327328/// Convert the format description to an owned version in place, replacing borrowed
329 /// components with their owned equivalents.
330#[cfg(feature = "alloc")]
331pub(super) fn into_owned(mut self) -> FormatDescriptionV3Inner<'static> {
332self.make_owned_in_place();
333334// Safety: `make_owned_in_place` recursively eliminates all variants that contain
335 // references, so we can transmute between lifetimes freely. ADTs do not vary in layout when
336 // only the lifetime differs.
337unsafe {
338 core::mem::transmute::<FormatDescriptionV3Inner<'a>, FormatDescriptionV3Inner<'static>>(
339self,
340 )
341 }
342 }
343344/// Convert the inner `enum` to a `FormatDescriptionV3`.
345#[inline]
346pub const fn into_opaque(self) -> FormatDescriptionV3<'a> {
347FormatDescriptionV3 {
348#[cfg(feature = "formatting")]
349max_bytes_needed: self.max_bytes_needed(),
350 inner: self,
351 }
352 }
353354/// Obtain the maximum number of bytes that are needed to format any value using this format
355 /// description.
356#[cfg(feature = "formatting")]
357const fn max_bytes_needed(&self) -> usize {
358match self {
359Self::Day(_) => 2,
360Self::MonthShort(_) => 3,
361Self::MonthLong(_) => 9,
362Self::MonthNumerical(_) => 2,
363Self::Ordinal(_) => 3,
364Self::WeekdayShort(_) => 3,
365Self::WeekdayLong(_) => 9,
366Self::WeekdaySunday(_) | Self::WeekdayMonday(_) => 1,
367Self::WeekNumberIso(_) | Self::WeekNumberSunday(_) | Self::WeekNumberMonday(_) => 2,
368Self::CalendarYearFullExtendedRange(_) => 7,
369Self::CalendarYearFullStandardRange(_) => 5,
370Self::IsoYearFullExtendedRange(_) => 7,
371Self::IsoYearFullStandardRange(_) => 5,
372Self::CalendarYearCenturyExtendedRange(_) => 5,
373Self::CalendarYearCenturyStandardRange(_) => 3,
374Self::IsoYearCenturyExtendedRange(_) => 5,
375Self::IsoYearCenturyStandardRange(_) => 3,
376Self::CalendarYearLastTwo(_) => 2,
377Self::IsoYearLastTwo(_) => 2,
378Self::Hour12(_) | Self::Hour24(_) => 2,
379Self::Minute(_) | Self::Period(_) | Self::Second(_) => 2,
380Self::Subsecond(modifier) => match modifier.digits {
381 modifier::SubsecondDigits::One => 1,
382 modifier::SubsecondDigits::Two => 2,
383 modifier::SubsecondDigits::Three => 3,
384 modifier::SubsecondDigits::Four => 4,
385 modifier::SubsecondDigits::Five => 5,
386 modifier::SubsecondDigits::Six => 6,
387 modifier::SubsecondDigits::Seven => 7,
388 modifier::SubsecondDigits::Eight => 8,
389 modifier::SubsecondDigits::Nine => 9,
390 modifier::SubsecondDigits::OneOrMore => 9,
391 },
392Self::OffsetHour(_) => 3,
393Self::OffsetMinute(_) | Self::OffsetSecond(_) => 2,
394#[cfg(feature = "large-dates")]
395Self::UnixTimestampSecond(_) => 15,
396#[cfg(not(feature = "large-dates"))]
397Self::UnixTimestampSecond(_) => 13,
398#[cfg(feature = "large-dates")]
399Self::UnixTimestampMillisecond(_) => 18,
400#[cfg(not(feature = "large-dates"))]
401Self::UnixTimestampMillisecond(_) => 16,
402#[cfg(feature = "large-dates")]
403Self::UnixTimestampMicrosecond(_) => 21,
404#[cfg(not(feature = "large-dates"))]
405Self::UnixTimestampMicrosecond(_) => 19,
406#[cfg(feature = "large-dates")]
407Self::UnixTimestampNanosecond(_) => 24,
408#[cfg(not(feature = "large-dates"))]
409Self::UnixTimestampNanosecond(_) => 22,
410Self::Ignore(_) | Self::End(_) => 0,
411 FormatDescriptionV3Inner::BorrowedLiteral(s) => s.len(),
412 FormatDescriptionV3Inner::BorrowedCompound(items) => {
413let mut max_bytes_needed = 0;
414let mut idx = 0;
415while idx < items.len() {
416 max_bytes_needed += items[idx].max_bytes_needed();
417 idx += 1;
418 }
419max_bytes_needed420 }
421 FormatDescriptionV3Inner::BorrowedOptional { format, item } => {
422if *format {
423item.max_bytes_needed()
424 } else {
4250
426}
427 }
428 FormatDescriptionV3Inner::BorrowedFirst(items) => {
429if items.is_empty() {
4300
431} else {
432items[0].max_bytes_needed()
433 }
434 }
435 FormatDescriptionV3Inner::OwnedLiteral(s) => s.len(),
436 FormatDescriptionV3Inner::OwnedCompound(items) => {
437let mut max_bytes_needed = 0;
438let mut idx = 0;
439while idx < items.len() {
440 max_bytes_needed += items[idx].max_bytes_needed();
441 idx += 1;
442 }
443max_bytes_needed444 }
445 FormatDescriptionV3Inner::OwnedOptional { format, item } => {
446if *format {
447item.max_bytes_needed()
448 } else {
4490
450}
451 }
452 FormatDescriptionV3Inner::OwnedFirst(items) => {
453if items.is_empty() {
4540
455} else {
456items[0].max_bytes_needed()
457 }
458 }
459 }
460 }
461}
462463/// A component of a larger format description.
464// public via `crate::format_description::__private` for macro use
465#[non_exhaustive]
466#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Component {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Component::Day(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Day",
&__self_0),
Component::MonthShort(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MonthShort", &__self_0),
Component::MonthLong(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MonthLong", &__self_0),
Component::MonthNumerical(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MonthNumerical", &__self_0),
Component::Ordinal(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Ordinal", &__self_0),
Component::WeekdayShort(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekdayShort", &__self_0),
Component::WeekdayLong(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekdayLong", &__self_0),
Component::WeekdaySunday(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekdaySunday", &__self_0),
Component::WeekdayMonday(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekdayMonday", &__self_0),
Component::WeekNumberIso(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekNumberIso", &__self_0),
Component::WeekNumberSunday(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekNumberSunday", &__self_0),
Component::WeekNumberMonday(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WeekNumberMonday", &__self_0),
Component::CalendarYearFullExtendedRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CalendarYearFullExtendedRange", &__self_0),
Component::CalendarYearFullStandardRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CalendarYearFullStandardRange", &__self_0),
Component::IsoYearFullExtendedRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"IsoYearFullExtendedRange", &__self_0),
Component::IsoYearFullStandardRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"IsoYearFullStandardRange", &__self_0),
Component::CalendarYearCenturyExtendedRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CalendarYearCenturyExtendedRange", &__self_0),
Component::CalendarYearCenturyStandardRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CalendarYearCenturyStandardRange", &__self_0),
Component::IsoYearCenturyExtendedRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"IsoYearCenturyExtendedRange", &__self_0),
Component::IsoYearCenturyStandardRange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"IsoYearCenturyStandardRange", &__self_0),
Component::CalendarYearLastTwo(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"CalendarYearLastTwo", &__self_0),
Component::IsoYearLastTwo(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"IsoYearLastTwo", &__self_0),
Component::Hour12(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Hour12",
&__self_0),
Component::Hour24(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Hour24",
&__self_0),
Component::Minute(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Minute",
&__self_0),
Component::Period(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Period",
&__self_0),
Component::Second(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Second",
&__self_0),
Component::Subsecond(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Subsecond", &__self_0),
Component::OffsetHour(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"OffsetHour", &__self_0),
Component::OffsetMinute(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"OffsetMinute", &__self_0),
Component::OffsetSecond(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"OffsetSecond", &__self_0),
Component::Ignore(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Ignore",
&__self_0),
Component::UnixTimestampSecond(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UnixTimestampSecond", &__self_0),
Component::UnixTimestampMillisecond(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UnixTimestampMillisecond", &__self_0),
Component::UnixTimestampMicrosecond(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UnixTimestampMicrosecond", &__self_0),
Component::UnixTimestampNanosecond(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UnixTimestampNanosecond", &__self_0),
Component::End(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "End",
&__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Component {
#[inline]
fn clone(&self) -> Component {
let _: ::core::clone::AssertParamIsClone<modifier::Day>;
let _: ::core::clone::AssertParamIsClone<modifier::MonthShort>;
let _: ::core::clone::AssertParamIsClone<modifier::MonthLong>;
let _: ::core::clone::AssertParamIsClone<modifier::MonthNumerical>;
let _: ::core::clone::AssertParamIsClone<modifier::Ordinal>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekdayShort>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekdayLong>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekdaySunday>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekdayMonday>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekNumberIso>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekNumberSunday>;
let _: ::core::clone::AssertParamIsClone<modifier::WeekNumberMonday>;
let _:
::core::clone::AssertParamIsClone<modifier::CalendarYearFullExtendedRange>;
let _:
::core::clone::AssertParamIsClone<modifier::CalendarYearFullStandardRange>;
let _:
::core::clone::AssertParamIsClone<modifier::IsoYearFullExtendedRange>;
let _:
::core::clone::AssertParamIsClone<modifier::IsoYearFullStandardRange>;
let _:
::core::clone::AssertParamIsClone<modifier::CalendarYearCenturyExtendedRange>;
let _:
::core::clone::AssertParamIsClone<modifier::CalendarYearCenturyStandardRange>;
let _:
::core::clone::AssertParamIsClone<modifier::IsoYearCenturyExtendedRange>;
let _:
::core::clone::AssertParamIsClone<modifier::IsoYearCenturyStandardRange>;
let _:
::core::clone::AssertParamIsClone<modifier::CalendarYearLastTwo>;
let _: ::core::clone::AssertParamIsClone<modifier::IsoYearLastTwo>;
let _: ::core::clone::AssertParamIsClone<modifier::Hour12>;
let _: ::core::clone::AssertParamIsClone<modifier::Hour24>;
let _: ::core::clone::AssertParamIsClone<modifier::Minute>;
let _: ::core::clone::AssertParamIsClone<modifier::Period>;
let _: ::core::clone::AssertParamIsClone<modifier::Second>;
let _: ::core::clone::AssertParamIsClone<modifier::Subsecond>;
let _: ::core::clone::AssertParamIsClone<modifier::OffsetHour>;
let _: ::core::clone::AssertParamIsClone<modifier::OffsetMinute>;
let _: ::core::clone::AssertParamIsClone<modifier::OffsetSecond>;
let _: ::core::clone::AssertParamIsClone<modifier::Ignore>;
let _:
::core::clone::AssertParamIsClone<modifier::UnixTimestampSecond>;
let _:
::core::clone::AssertParamIsClone<modifier::UnixTimestampMillisecond>;
let _:
::core::clone::AssertParamIsClone<modifier::UnixTimestampMicrosecond>;
let _:
::core::clone::AssertParamIsClone<modifier::UnixTimestampNanosecond>;
let _: ::core::clone::AssertParamIsClone<modifier::End>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Component { }Copy)]
467pub enum Component {
468/// Day of the month.
469Day(modifier::Day),
470/// Month of the year in the abbreviated form (e.g. "Jan").
471MonthShort(modifier::MonthShort),
472/// Month of the year in the full form (e.g. "January").
473MonthLong(modifier::MonthLong),
474/// Month of the year in the numerical form (e.g. "1" for January).
475MonthNumerical(modifier::MonthNumerical),
476/// Ordinal day of the year.
477Ordinal(modifier::Ordinal),
478/// Weekday in the abbreviated form (e.g. "Mon").
479WeekdayShort(modifier::WeekdayShort),
480/// Weekday in the full form (e.g. "Monday").
481WeekdayLong(modifier::WeekdayLong),
482/// Weekday number where Sunday is either 0 or 1 depending on the modifier.
483WeekdaySunday(modifier::WeekdaySunday),
484/// Weekday number where Monday is either 0 or 1 depending on the modifier.
485WeekdayMonday(modifier::WeekdayMonday),
486/// Week number of the year, where week 1 starts is the week beginning on Monday that contains
487 /// January 4.
488WeekNumberIso(modifier::WeekNumberIso),
489/// Week number of the year, where week 1 starts on the first Sunday of the calendar year.
490WeekNumberSunday(modifier::WeekNumberSunday),
491/// Week number of the year, where week 1 starts on the first Monday of the calendar year.
492WeekNumberMonday(modifier::WeekNumberMonday),
493/// The calendar year. Supports the extended range.
494CalendarYearFullExtendedRange(modifier::CalendarYearFullExtendedRange),
495/// The calendar year. Does not support the extended range.
496CalendarYearFullStandardRange(modifier::CalendarYearFullStandardRange),
497/// The ISO week-based year. Supports the extended range.
498IsoYearFullExtendedRange(modifier::IsoYearFullExtendedRange),
499/// The ISO week-based year. Does not support the extended range.
500IsoYearFullStandardRange(modifier::IsoYearFullStandardRange),
501/// The century of the calendar year. Supports the extended range.
502CalendarYearCenturyExtendedRange(modifier::CalendarYearCenturyExtendedRange),
503/// The century of the calendar year. Does not support the extended range.
504CalendarYearCenturyStandardRange(modifier::CalendarYearCenturyStandardRange),
505/// The century of the ISO week-based year. Supports the extended range.
506IsoYearCenturyExtendedRange(modifier::IsoYearCenturyExtendedRange),
507/// The century of the ISO week-based year. Does not support the extended range.
508IsoYearCenturyStandardRange(modifier::IsoYearCenturyStandardRange),
509/// The last two digits of the calendar year.
510CalendarYearLastTwo(modifier::CalendarYearLastTwo),
511/// The last two digits of the ISO week-based year.
512IsoYearLastTwo(modifier::IsoYearLastTwo),
513/// Hour of the day using the 12-hour clock.
514Hour12(modifier::Hour12),
515/// Hour of the day using the 24-hour clock.
516Hour24(modifier::Hour24),
517/// Minute within the hour.
518Minute(modifier::Minute),
519/// AM/PM part of the time.
520Period(modifier::Period),
521/// Second within the minute.
522Second(modifier::Second),
523/// Subsecond within the second.
524Subsecond(modifier::Subsecond),
525/// Hour of the UTC offset.
526OffsetHour(modifier::OffsetHour),
527/// Minute within the hour of the UTC offset.
528OffsetMinute(modifier::OffsetMinute),
529/// Second within the minute of the UTC offset.
530OffsetSecond(modifier::OffsetSecond),
531/// A number of bytes to ignore when parsing. This has no effect on formatting.
532Ignore(modifier::Ignore),
533/// A Unix timestamp in seconds.
534UnixTimestampSecond(modifier::UnixTimestampSecond),
535/// A Unix timestamp in milliseconds.
536UnixTimestampMillisecond(modifier::UnixTimestampMillisecond),
537/// A Unix timestamp in microseconds.
538UnixTimestampMicrosecond(modifier::UnixTimestampMicrosecond),
539/// A Unix timestamp in nanoseconds.
540UnixTimestampNanosecond(modifier::UnixTimestampNanosecond),
541/// The end of input. Parsing this component will fail if there is any input remaining. This
542 /// component neither affects formatting nor consumes any input when parsing.
543End(modifier::End),
544}