1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#[cfg(feature = "alloc")]
use alloc::string::String;
use crate::format_description::modifier;
#[cfg(feature = "alloc")]
use crate::{error::InvalidFormatDescription, format_description::modifier::Modifiers};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Component {
Day(modifier::Day),
Month(modifier::Month),
Ordinal(modifier::Ordinal),
Weekday(modifier::Weekday),
WeekNumber(modifier::WeekNumber),
Year(modifier::Year),
Hour(modifier::Hour),
Minute(modifier::Minute),
Period(modifier::Period),
Second(modifier::Second),
Subsecond(modifier::Subsecond),
OffsetHour(modifier::OffsetHour),
OffsetMinute(modifier::OffsetMinute),
OffsetSecond(modifier::OffsetSecond),
}
#[cfg(feature = "alloc")]
pub(crate) enum NakedComponent {
Day,
Month,
Ordinal,
Weekday,
WeekNumber,
Year,
Hour,
Minute,
Period,
Second,
Subsecond,
OffsetHour,
OffsetMinute,
OffsetSecond,
}
#[cfg(feature = "alloc")]
impl NakedComponent {
pub(crate) fn parse(
component_name: &[u8],
component_index: usize,
) -> Result<Self, InvalidFormatDescription> {
match component_name {
b"day" => Ok(Self::Day),
b"month" => Ok(Self::Month),
b"ordinal" => Ok(Self::Ordinal),
b"weekday" => Ok(Self::Weekday),
b"week_number" => Ok(Self::WeekNumber),
b"year" => Ok(Self::Year),
b"hour" => Ok(Self::Hour),
b"minute" => Ok(Self::Minute),
b"period" => Ok(Self::Period),
b"second" => Ok(Self::Second),
b"subsecond" => Ok(Self::Subsecond),
b"offset_hour" => Ok(Self::OffsetHour),
b"offset_minute" => Ok(Self::OffsetMinute),
b"offset_second" => Ok(Self::OffsetSecond),
b"" => Err(InvalidFormatDescription::MissingComponentName {
index: component_index,
}),
_ => Err(InvalidFormatDescription::InvalidComponentName {
name: String::from_utf8_lossy(component_name).into_owned(),
index: component_index,
}),
}
}
pub(crate) fn attach_modifiers(self, modifiers: &Modifiers) -> Component {
match self {
Self::Day => Component::Day(modifier::Day {
padding: modifiers.padding.unwrap_or_default(),
}),
Self::Month => Component::Month(modifier::Month {
padding: modifiers.padding.unwrap_or_default(),
repr: modifiers.month_repr.unwrap_or_default(),
case_sensitive: modifiers.case_sensitive.unwrap_or(true),
}),
Self::Ordinal => Component::Ordinal(modifier::Ordinal {
padding: modifiers.padding.unwrap_or_default(),
}),
Self::Weekday => Component::Weekday(modifier::Weekday {
repr: modifiers.weekday_repr.unwrap_or_default(),
one_indexed: modifiers.weekday_is_one_indexed.unwrap_or(true),
case_sensitive: modifiers.case_sensitive.unwrap_or(true),
}),
Self::WeekNumber => Component::WeekNumber(modifier::WeekNumber {
padding: modifiers.padding.unwrap_or_default(),
repr: modifiers.week_number_repr.unwrap_or_default(),
}),
Self::Year => Component::Year(modifier::Year {
padding: modifiers.padding.unwrap_or_default(),
repr: modifiers.year_repr.unwrap_or_default(),
iso_week_based: modifiers.year_is_iso_week_based.unwrap_or_default(),
sign_is_mandatory: modifiers.sign_is_mandatory.unwrap_or_default(),
}),
Self::Hour => Component::Hour(modifier::Hour {
padding: modifiers.padding.unwrap_or_default(),
is_12_hour_clock: modifiers.hour_is_12_hour_clock.unwrap_or_default(),
}),
Self::Minute => Component::Minute(modifier::Minute {
padding: modifiers.padding.unwrap_or_default(),
}),
Self::Period => Component::Period(modifier::Period {
is_uppercase: modifiers.period_is_uppercase.unwrap_or(true),
case_sensitive: modifiers.case_sensitive.unwrap_or(true),
}),
Self::Second => Component::Second(modifier::Second {
padding: modifiers.padding.unwrap_or_default(),
}),
Self::Subsecond => Component::Subsecond(modifier::Subsecond {
digits: modifiers.subsecond_digits.unwrap_or_default(),
}),
Self::OffsetHour => Component::OffsetHour(modifier::OffsetHour {
sign_is_mandatory: modifiers.sign_is_mandatory.unwrap_or_default(),
padding: modifiers.padding.unwrap_or_default(),
}),
Self::OffsetMinute => Component::OffsetMinute(modifier::OffsetMinute {
padding: modifiers.padding.unwrap_or_default(),
}),
Self::OffsetSecond => Component::OffsetSecond(modifier::OffsetSecond {
padding: modifiers.padding.unwrap_or_default(),
}),
}
}
}