Skip to content

Commit 78e79db

Browse files
committed
Match on tuples in format_fixed
1 parent f3d76c7 commit 78e79db

File tree

1 file changed

+69
-117
lines changed

1 file changed

+69
-117
lines changed

src/format/formatting.rs

+69-117
Original file line numberDiff line numberDiff line change
@@ -162,45 +162,34 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
162162

163163
#[cfg(feature = "alloc")]
164164
fn format_fixed(&self, w: &mut impl Write, spec: &Fixed) -> fmt::Result {
165-
use self::Fixed::*;
165+
use Fixed::*;
166+
use InternalInternal::*;
166167

167-
let ret = match *spec {
168-
ShortMonthName => self.date.map(|d| {
169-
w.write_str(short_months(self.locale)[d.month0() as usize])?;
170-
Ok(())
171-
}),
172-
LongMonthName => self.date.map(|d| {
173-
w.write_str(long_months(self.locale)[d.month0() as usize])?;
174-
Ok(())
175-
}),
176-
ShortWeekdayName => self.date.map(|d| {
177-
w.write_str(
178-
short_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize],
179-
)?;
180-
Ok(())
181-
}),
182-
LongWeekdayName => self.date.map(|d| {
183-
w.write_str(
184-
long_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize],
185-
)?;
186-
Ok(())
187-
}),
188-
LowerAmPm => self.time.map(|t| {
168+
match (spec, self.date, self.time, self.off.as_ref()) {
169+
(ShortMonthName, Some(d), _, _) => {
170+
w.write_str(short_months(self.locale)[d.month0() as usize])
171+
}
172+
(LongMonthName, Some(d), _, _) => {
173+
w.write_str(long_months(self.locale)[d.month0() as usize])
174+
}
175+
(ShortWeekdayName, Some(d), _, _) => w.write_str(
176+
short_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize],
177+
),
178+
(LongWeekdayName, Some(d), _, _) => {
179+
w.write_str(long_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize])
180+
}
181+
(LowerAmPm, _, Some(t), _) => {
189182
let ampm = if t.hour12().0 { am_pm(self.locale)[1] } else { am_pm(self.locale)[0] };
190183
for c in ampm.chars().flat_map(|c| c.to_lowercase()) {
191184
w.write_char(c)?
192185
}
193186
Ok(())
194-
}),
195-
UpperAmPm => self.time.map(|t| {
196-
w.write_str(if t.hour12().0 {
197-
am_pm(self.locale)[1]
198-
} else {
199-
am_pm(self.locale)[0]
200-
})?;
201-
Ok(())
202-
}),
203-
Nanosecond => self.time.map(|t| {
187+
}
188+
(UpperAmPm, _, Some(t), _) => {
189+
let ampm = if t.hour12().0 { am_pm(self.locale)[1] } else { am_pm(self.locale)[0] };
190+
w.write_str(ampm)
191+
}
192+
(Nanosecond, _, Some(t), _) => {
204193
let nano = t.nanosecond() % 1_000_000_000;
205194
if nano == 0 {
206195
Ok(())
@@ -214,114 +203,77 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
214203
write!(w, "{:09}", nano)
215204
}
216205
}
217-
}),
218-
Nanosecond3 => self.time.map(|t| {
219-
let nano = t.nanosecond() % 1_000_000_000;
206+
}
207+
(Nanosecond3, _, Some(t), _) => {
220208
w.write_str(decimal_point(self.locale))?;
221-
write!(w, "{:03}", nano / 1_000_000)
222-
}),
223-
Nanosecond6 => self.time.map(|t| {
224-
let nano = t.nanosecond() % 1_000_000_000;
209+
write!(w, "{:03}", t.nanosecond() % 1_000_000_000 / 1_000_000)
210+
}
211+
(Nanosecond6, _, Some(t), _) => {
225212
w.write_str(decimal_point(self.locale))?;
226-
write!(w, "{:06}", nano / 1_000)
227-
}),
228-
Nanosecond9 => self.time.map(|t| {
229-
let nano = t.nanosecond() % 1_000_000_000;
213+
write!(w, "{:06}", t.nanosecond() % 1_000_000_000 / 1_000)
214+
}
215+
(Nanosecond9, _, Some(t), _) => {
230216
w.write_str(decimal_point(self.locale))?;
231-
write!(w, "{:09}", nano)
232-
}),
233-
Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => {
234-
self.time.map(|t| {
235-
let nano = t.nanosecond() % 1_000_000_000;
236-
write!(w, "{:03}", nano / 1_000_000)
237-
})
217+
write!(w, "{:09}", t.nanosecond() % 1_000_000_000)
238218
}
239-
Internal(InternalFixed { val: InternalInternal::Nanosecond6NoDot }) => {
240-
self.time.map(|t| {
241-
let nano = t.nanosecond() % 1_000_000_000;
242-
write!(w, "{:06}", nano / 1_000)
243-
})
219+
(Internal(InternalFixed { val: Nanosecond3NoDot }), _, Some(t), _) => {
220+
write!(w, "{:03}", t.nanosecond() % 1_000_000_000 / 1_000_000)
244221
}
245-
Internal(InternalFixed { val: InternalInternal::Nanosecond9NoDot }) => {
246-
self.time.map(|t| {
247-
let nano = t.nanosecond() % 1_000_000_000;
248-
write!(w, "{:09}", nano)
249-
})
222+
(Internal(InternalFixed { val: Nanosecond6NoDot }), _, Some(t), _) => {
223+
write!(w, "{:06}", t.nanosecond() % 1_000_000_000 / 1_000)
250224
}
251-
TimezoneName => self.off.as_ref().map(|(name, _)| {
252-
w.write_str(name)?;
253-
Ok(())
254-
}),
255-
TimezoneOffset | TimezoneOffsetZ => self.off.as_ref().map(|&(_, off)| {
256-
OffsetFormat {
225+
(Internal(InternalFixed { val: Nanosecond9NoDot }), _, Some(t), _) => {
226+
write!(w, "{:09}", t.nanosecond() % 1_000_000_000)
227+
}
228+
(TimezoneName, _, _, Some((tz_name, _))) => write!(w, "{}", tz_name),
229+
(TimezoneOffset | TimezoneOffsetZ, _, _, Some((_, off))) => {
230+
let offset_format = OffsetFormat {
257231
precision: OffsetPrecision::Minutes,
258232
colons: Colons::Maybe,
259233
allow_zulu: *spec == TimezoneOffsetZ,
260234
padding: Pad::Zero,
261-
}
262-
.format(w, off)
263-
}),
264-
TimezoneOffsetColon | TimezoneOffsetColonZ => self.off.as_ref().map(|&(_, off)| {
265-
OffsetFormat {
235+
};
236+
offset_format.format(w, *off)
237+
}
238+
(TimezoneOffsetColon | TimezoneOffsetColonZ, _, _, Some((_, off))) => {
239+
let offset_format = OffsetFormat {
266240
precision: OffsetPrecision::Minutes,
267241
colons: Colons::Colon,
268242
allow_zulu: *spec == TimezoneOffsetColonZ,
269243
padding: Pad::Zero,
270-
}
271-
.format(w, off)
272-
}),
273-
TimezoneOffsetDoubleColon => self.off.as_ref().map(|&(_, off)| {
274-
OffsetFormat {
244+
};
245+
offset_format.format(w, *off)
246+
}
247+
(TimezoneOffsetDoubleColon, _, _, Some((_, off))) => {
248+
let offset_format = OffsetFormat {
275249
precision: OffsetPrecision::Seconds,
276250
colons: Colons::Colon,
277251
allow_zulu: false,
278252
padding: Pad::Zero,
279-
}
280-
.format(w, off)
281-
}),
282-
TimezoneOffsetTripleColon => self.off.as_ref().map(|&(_, off)| {
283-
OffsetFormat {
253+
};
254+
offset_format.format(w, *off)
255+
}
256+
(TimezoneOffsetTripleColon, _, _, Some((_, off))) => {
257+
let offset_format = OffsetFormat {
284258
precision: OffsetPrecision::Hours,
285259
colons: Colons::None,
286260
allow_zulu: false,
287261
padding: Pad::Zero,
288-
}
289-
.format(w, off)
290-
}),
291-
Internal(InternalFixed { val: InternalInternal::TimezoneOffsetPermissive }) => {
292-
return Err(fmt::Error);
262+
};
263+
offset_format.format(w, *off)
293264
}
294-
RFC2822 =>
295-
// same as `%a, %d %b %Y %H:%M:%S %z`
296-
{
297-
if let (Some(d), Some(t), Some(&(_, off))) =
298-
(self.date, self.time, self.off.as_ref())
299-
{
300-
Some(write_rfc2822(w, crate::NaiveDateTime::new(d, t), off))
301-
} else {
302-
None
303-
}
304-
}
305-
RFC3339 =>
306-
// same as `%Y-%m-%dT%H:%M:%S%.f%:z`
307-
{
308-
if let (Some(d), Some(t), Some(&(_, off))) =
309-
(self.date, self.time, self.off.as_ref())
310-
{
311-
Some(write_rfc3339(
312-
w,
313-
crate::NaiveDateTime::new(d, t),
314-
off.fix(),
315-
SecondsFormat::AutoSi,
316-
false,
317-
))
318-
} else {
319-
None
320-
}
265+
(RFC2822, Some(d), Some(t), Some((_, off))) => {
266+
write_rfc2822(w, crate::NaiveDateTime::new(d, t), *off)
321267
}
322-
};
323-
324-
ret.unwrap_or(Err(fmt::Error)) // insufficient arguments for given format
268+
(RFC3339, Some(d), Some(t), Some((_, off))) => write_rfc3339(
269+
w,
270+
crate::NaiveDateTime::new(d, t),
271+
*off,
272+
SecondsFormat::AutoSi,
273+
false,
274+
),
275+
_ => Err(fmt::Error), // insufficient arguments for given format
276+
}
325277
}
326278
}
327279

0 commit comments

Comments
 (0)