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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
//! Parsing implementations for all [`Component`](crate::format_description::Component)s.
use core::num::{NonZeroU16, NonZeroU8};
use crate::format_description::modifier;
#[cfg(feature = "large-dates")]
use crate::parsing::combinator::n_to_m_digits_padded;
use crate::parsing::combinator::{
any_digit, exactly_n_digits, exactly_n_digits_padded, first_match, opt, sign,
};
use crate::parsing::ParsedItem;
use crate::{Month, Weekday};
// region: date components
/// Parse the "year" component of a `Date`.
pub(crate) fn parse_year(input: &[u8], modifiers: modifier::Year) -> Option<ParsedItem<'_, i32>> {
match modifiers.repr {
modifier::YearRepr::Full => {
let ParsedItem(input, sign) = opt(sign)(input);
#[cfg(not(feature = "large-dates"))]
let ParsedItem(input, year) =
exactly_n_digits_padded::<4, u32>(modifiers.padding)(input)?;
#[cfg(feature = "large-dates")]
let ParsedItem(input, year) =
n_to_m_digits_padded::<4, 6, u32>(modifiers.padding)(input)?;
match sign {
Some(b'-') => Some(ParsedItem(input, -(year as i32))),
None if modifiers.sign_is_mandatory || year >= 10_000 => None,
_ => Some(ParsedItem(input, year as i32)),
}
}
modifier::YearRepr::LastTwo => {
Some(exactly_n_digits_padded::<2, u32>(modifiers.padding)(input)?.map(|v| v as i32))
}
}
}
/// Parse the "month" component of a `Date`.
pub(crate) fn parse_month(
input: &[u8],
modifiers: modifier::Month,
) -> Option<ParsedItem<'_, Month>> {
use Month::*;
let ParsedItem(remaining, value) = first_match(
match modifiers.repr {
modifier::MonthRepr::Numerical => {
return exactly_n_digits_padded::<2, _>(modifiers.padding)(input)?
.flat_map(|n| Month::from_number(n).ok());
}
modifier::MonthRepr::Long => [
(b"January".as_slice(), January),
(b"February".as_slice(), February),
(b"March".as_slice(), March),
(b"April".as_slice(), April),
(b"May".as_slice(), May),
(b"June".as_slice(), June),
(b"July".as_slice(), July),
(b"August".as_slice(), August),
(b"September".as_slice(), September),
(b"October".as_slice(), October),
(b"November".as_slice(), November),
(b"December".as_slice(), December),
],
modifier::MonthRepr::Short => [
(b"Jan".as_slice(), January),
(b"Feb".as_slice(), February),
(b"Mar".as_slice(), March),
(b"Apr".as_slice(), April),
(b"May".as_slice(), May),
(b"Jun".as_slice(), June),
(b"Jul".as_slice(), July),
(b"Aug".as_slice(), August),
(b"Sep".as_slice(), September),
(b"Oct".as_slice(), October),
(b"Nov".as_slice(), November),
(b"Dec".as_slice(), December),
],
},
modifiers.case_sensitive,
)(input)?;
Some(ParsedItem(remaining, value))
}
/// Parse the "week number" component of a `Date`.
pub(crate) fn parse_week_number(
input: &[u8],
modifiers: modifier::WeekNumber,
) -> Option<ParsedItem<'_, u8>> {
exactly_n_digits_padded::<2, _>(modifiers.padding)(input)
}
/// Parse the "weekday" component of a `Date`.
pub(crate) fn parse_weekday(
input: &[u8],
modifiers: modifier::Weekday,
) -> Option<ParsedItem<'_, Weekday>> {
first_match(
match (modifiers.repr, modifiers.one_indexed) {
(modifier::WeekdayRepr::Short, _) => [
(b"Mon".as_slice(), Weekday::Monday),
(b"Tue".as_slice(), Weekday::Tuesday),
(b"Wed".as_slice(), Weekday::Wednesday),
(b"Thu".as_slice(), Weekday::Thursday),
(b"Fri".as_slice(), Weekday::Friday),
(b"Sat".as_slice(), Weekday::Saturday),
(b"Sun".as_slice(), Weekday::Sunday),
],
(modifier::WeekdayRepr::Long, _) => [
(b"Monday".as_slice(), Weekday::Monday),
(b"Tuesday".as_slice(), Weekday::Tuesday),
(b"Wednesday".as_slice(), Weekday::Wednesday),
(b"Thursday".as_slice(), Weekday::Thursday),
(b"Friday".as_slice(), Weekday::Friday),
(b"Saturday".as_slice(), Weekday::Saturday),
(b"Sunday".as_slice(), Weekday::Sunday),
],
(modifier::WeekdayRepr::Sunday, false) => [
(b"1".as_slice(), Weekday::Monday),
(b"2".as_slice(), Weekday::Tuesday),
(b"3".as_slice(), Weekday::Wednesday),
(b"4".as_slice(), Weekday::Thursday),
(b"5".as_slice(), Weekday::Friday),
(b"6".as_slice(), Weekday::Saturday),
(b"0".as_slice(), Weekday::Sunday),
],
(modifier::WeekdayRepr::Sunday, true) => [
(b"2".as_slice(), Weekday::Monday),
(b"3".as_slice(), Weekday::Tuesday),
(b"4".as_slice(), Weekday::Wednesday),
(b"5".as_slice(), Weekday::Thursday),
(b"6".as_slice(), Weekday::Friday),
(b"7".as_slice(), Weekday::Saturday),
(b"1".as_slice(), Weekday::Sunday),
],
(modifier::WeekdayRepr::Monday, false) => [
(b"0".as_slice(), Weekday::Monday),
(b"1".as_slice(), Weekday::Tuesday),
(b"2".as_slice(), Weekday::Wednesday),
(b"3".as_slice(), Weekday::Thursday),
(b"4".as_slice(), Weekday::Friday),
(b"5".as_slice(), Weekday::Saturday),
(b"6".as_slice(), Weekday::Sunday),
],
(modifier::WeekdayRepr::Monday, true) => [
(b"1".as_slice(), Weekday::Monday),
(b"2".as_slice(), Weekday::Tuesday),
(b"3".as_slice(), Weekday::Wednesday),
(b"4".as_slice(), Weekday::Thursday),
(b"5".as_slice(), Weekday::Friday),
(b"6".as_slice(), Weekday::Saturday),
(b"7".as_slice(), Weekday::Sunday),
],
},
modifiers.case_sensitive,
)(input)
}
/// Parse the "ordinal" component of a `Date`.
pub(crate) fn parse_ordinal(
input: &[u8],
modifiers: modifier::Ordinal,
) -> Option<ParsedItem<'_, NonZeroU16>> {
exactly_n_digits_padded::<3, _>(modifiers.padding)(input)
}
/// Parse the "day" component of a `Date`.
pub(crate) fn parse_day(
input: &[u8],
modifiers: modifier::Day,
) -> Option<ParsedItem<'_, NonZeroU8>> {
exactly_n_digits_padded::<2, _>(modifiers.padding)(input)
}
// endregion date components
// region: time components
/// Indicate whether the hour is "am" or "pm".
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Period {
#[allow(clippy::missing_docs_in_private_items)]
Am,
#[allow(clippy::missing_docs_in_private_items)]
Pm,
}
/// Parse the "hour" component of a `Time`.
pub(crate) fn parse_hour(input: &[u8], modifiers: modifier::Hour) -> Option<ParsedItem<'_, u8>> {
exactly_n_digits_padded::<2, _>(modifiers.padding)(input)
}
/// Parse the "minute" component of a `Time`.
pub(crate) fn parse_minute(
input: &[u8],
modifiers: modifier::Minute,
) -> Option<ParsedItem<'_, u8>> {
exactly_n_digits_padded::<2, _>(modifiers.padding)(input)
}
/// Parse the "second" component of a `Time`.
pub(crate) fn parse_second(
input: &[u8],
modifiers: modifier::Second,
) -> Option<ParsedItem<'_, u8>> {
exactly_n_digits_padded::<2, _>(modifiers.padding)(input)
}
/// Parse the "period" component of a `Time`. Required if the hour is on a 12-hour clock.
pub(crate) fn parse_period(
input: &[u8],
modifiers: modifier::Period,
) -> Option<ParsedItem<'_, Period>> {
first_match(
if modifiers.is_uppercase {
[
(b"AM".as_slice(), Period::Am),
(b"PM".as_slice(), Period::Pm),
]
} else {
[
(b"am".as_slice(), Period::Am),
(b"pm".as_slice(), Period::Pm),
]
},
modifiers.case_sensitive,
)(input)
}
/// Parse the "subsecond" component of a `Time`.
pub(crate) fn parse_subsecond(
input: &[u8],
modifiers: modifier::Subsecond,
) -> Option<ParsedItem<'_, u32>> {
use modifier::SubsecondDigits::*;
Some(match modifiers.digits {
One => exactly_n_digits::<1, u32>(input)?.map(|v| v * 100_000_000),
Two => exactly_n_digits::<2, u32>(input)?.map(|v| v * 10_000_000),
Three => exactly_n_digits::<3, u32>(input)?.map(|v| v * 1_000_000),
Four => exactly_n_digits::<4, u32>(input)?.map(|v| v * 100_000),
Five => exactly_n_digits::<5, u32>(input)?.map(|v| v * 10_000),
Six => exactly_n_digits::<6, u32>(input)?.map(|v| v * 1_000),
Seven => exactly_n_digits::<7, u32>(input)?.map(|v| v * 100),
Eight => exactly_n_digits::<8, u32>(input)?.map(|v| v * 10),
Nine => exactly_n_digits::<9, _>(input)?,
OneOrMore => {
let ParsedItem(mut input, mut value) =
any_digit(input)?.map(|v| (v - b'0') as u32 * 100_000_000);
let mut multiplier = 10_000_000;
while let Some(ParsedItem(new_input, digit)) = any_digit(input) {
value += (digit - b'0') as u32 * multiplier;
input = new_input;
multiplier /= 10;
}
ParsedItem(input, value)
}
})
}
// endregion time components
// region: offset components
/// Parse the "hour" component of a `UtcOffset`.
pub(crate) fn parse_offset_hour(
input: &[u8],
modifiers: modifier::OffsetHour,
) -> Option<ParsedItem<'_, i8>> {
let ParsedItem(input, sign) = opt(sign)(input);
let ParsedItem(input, hour) = exactly_n_digits_padded::<2, u8>(modifiers.padding)(input)?;
match sign {
Some(b'-') => Some(ParsedItem(input, -(hour as i8))),
None if modifiers.sign_is_mandatory => None,
_ => Some(ParsedItem(input, hour as i8)),
}
}
/// Parse the "minute" component of a `UtcOffset`.
pub(crate) fn parse_offset_minute(
input: &[u8],
modifiers: modifier::OffsetMinute,
) -> Option<ParsedItem<'_, i8>> {
Some(
exactly_n_digits_padded::<2, u8>(modifiers.padding)(input)?
.map(|offset_minute| offset_minute as _),
)
}
/// Parse the "second" component of a `UtcOffset`.
pub(crate) fn parse_offset_second(
input: &[u8],
modifiers: modifier::OffsetSecond,
) -> Option<ParsedItem<'_, i8>> {
Some(
exactly_n_digits_padded::<2, u8>(modifiers.padding)(input)?
.map(|offset_second| offset_second as _),
)
}
// endregion offset components
|