Match on tuples in format_fixed

This commit is contained in:
Paul Dicker 2023-10-01 14:41:39 +02:00 committed by Paul Dicker
parent f3d76c7bb0
commit 78e79dbabf

View File

@ -162,45 +162,34 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
fn format_fixed(&self, w: &mut impl Write, spec: &Fixed) -> fmt::Result { fn format_fixed(&self, w: &mut impl Write, spec: &Fixed) -> fmt::Result {
use self::Fixed::*; use Fixed::*;
use InternalInternal::*;
let ret = match *spec { match (spec, self.date, self.time, self.off.as_ref()) {
ShortMonthName => self.date.map(|d| { (ShortMonthName, Some(d), _, _) => {
w.write_str(short_months(self.locale)[d.month0() as usize])?; w.write_str(short_months(self.locale)[d.month0() as usize])
Ok(()) }
}), (LongMonthName, Some(d), _, _) => {
LongMonthName => self.date.map(|d| { w.write_str(long_months(self.locale)[d.month0() as usize])
w.write_str(long_months(self.locale)[d.month0() as usize])?; }
Ok(()) (ShortWeekdayName, Some(d), _, _) => w.write_str(
}), short_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize],
ShortWeekdayName => self.date.map(|d| { ),
w.write_str( (LongWeekdayName, Some(d), _, _) => {
short_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize], w.write_str(long_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize])
)?; }
Ok(()) (LowerAmPm, _, Some(t), _) => {
}),
LongWeekdayName => self.date.map(|d| {
w.write_str(
long_weekdays(self.locale)[d.weekday().num_days_from_sunday() as usize],
)?;
Ok(())
}),
LowerAmPm => self.time.map(|t| {
let ampm = if t.hour12().0 { am_pm(self.locale)[1] } else { am_pm(self.locale)[0] }; let ampm = if t.hour12().0 { am_pm(self.locale)[1] } else { am_pm(self.locale)[0] };
for c in ampm.chars().flat_map(|c| c.to_lowercase()) { for c in ampm.chars().flat_map(|c| c.to_lowercase()) {
w.write_char(c)? w.write_char(c)?
} }
Ok(()) Ok(())
}), }
UpperAmPm => self.time.map(|t| { (UpperAmPm, _, Some(t), _) => {
w.write_str(if t.hour12().0 { let ampm = if t.hour12().0 { am_pm(self.locale)[1] } else { am_pm(self.locale)[0] };
am_pm(self.locale)[1] w.write_str(ampm)
} else { }
am_pm(self.locale)[0] (Nanosecond, _, Some(t), _) => {
})?;
Ok(())
}),
Nanosecond => self.time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000; let nano = t.nanosecond() % 1_000_000_000;
if nano == 0 { if nano == 0 {
Ok(()) Ok(())
@ -214,114 +203,77 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
write!(w, "{:09}", nano) write!(w, "{:09}", nano)
} }
} }
}),
Nanosecond3 => self.time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
w.write_str(decimal_point(self.locale))?;
write!(w, "{:03}", nano / 1_000_000)
}),
Nanosecond6 => self.time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
w.write_str(decimal_point(self.locale))?;
write!(w, "{:06}", nano / 1_000)
}),
Nanosecond9 => self.time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
w.write_str(decimal_point(self.locale))?;
write!(w, "{:09}", nano)
}),
Internal(InternalFixed { val: InternalInternal::Nanosecond3NoDot }) => {
self.time.map(|t| {
let nano = t.nanosecond() % 1_000_000_000;
write!(w, "{:03}", nano / 1_000_000)
})
} }
Internal(InternalFixed { val: InternalInternal::Nanosecond6NoDot }) => { (Nanosecond3, _, Some(t), _) => {
self.time.map(|t| { w.write_str(decimal_point(self.locale))?;
let nano = t.nanosecond() % 1_000_000_000; write!(w, "{:03}", t.nanosecond() % 1_000_000_000 / 1_000_000)
write!(w, "{:06}", nano / 1_000)
})
} }
Internal(InternalFixed { val: InternalInternal::Nanosecond9NoDot }) => { (Nanosecond6, _, Some(t), _) => {
self.time.map(|t| { w.write_str(decimal_point(self.locale))?;
let nano = t.nanosecond() % 1_000_000_000; write!(w, "{:06}", t.nanosecond() % 1_000_000_000 / 1_000)
write!(w, "{:09}", nano)
})
} }
TimezoneName => self.off.as_ref().map(|(name, _)| { (Nanosecond9, _, Some(t), _) => {
w.write_str(name)?; w.write_str(decimal_point(self.locale))?;
Ok(()) write!(w, "{:09}", t.nanosecond() % 1_000_000_000)
}), }
TimezoneOffset | TimezoneOffsetZ => self.off.as_ref().map(|&(_, off)| { (Internal(InternalFixed { val: Nanosecond3NoDot }), _, Some(t), _) => {
OffsetFormat { write!(w, "{:03}", t.nanosecond() % 1_000_000_000 / 1_000_000)
}
(Internal(InternalFixed { val: Nanosecond6NoDot }), _, Some(t), _) => {
write!(w, "{:06}", t.nanosecond() % 1_000_000_000 / 1_000)
}
(Internal(InternalFixed { val: Nanosecond9NoDot }), _, Some(t), _) => {
write!(w, "{:09}", t.nanosecond() % 1_000_000_000)
}
(TimezoneName, _, _, Some((tz_name, _))) => write!(w, "{}", tz_name),
(TimezoneOffset | TimezoneOffsetZ, _, _, Some((_, off))) => {
let offset_format = OffsetFormat {
precision: OffsetPrecision::Minutes, precision: OffsetPrecision::Minutes,
colons: Colons::Maybe, colons: Colons::Maybe,
allow_zulu: *spec == TimezoneOffsetZ, allow_zulu: *spec == TimezoneOffsetZ,
padding: Pad::Zero, padding: Pad::Zero,
} };
.format(w, off) offset_format.format(w, *off)
}), }
TimezoneOffsetColon | TimezoneOffsetColonZ => self.off.as_ref().map(|&(_, off)| { (TimezoneOffsetColon | TimezoneOffsetColonZ, _, _, Some((_, off))) => {
OffsetFormat { let offset_format = OffsetFormat {
precision: OffsetPrecision::Minutes, precision: OffsetPrecision::Minutes,
colons: Colons::Colon, colons: Colons::Colon,
allow_zulu: *spec == TimezoneOffsetColonZ, allow_zulu: *spec == TimezoneOffsetColonZ,
padding: Pad::Zero, padding: Pad::Zero,
} };
.format(w, off) offset_format.format(w, *off)
}), }
TimezoneOffsetDoubleColon => self.off.as_ref().map(|&(_, off)| { (TimezoneOffsetDoubleColon, _, _, Some((_, off))) => {
OffsetFormat { let offset_format = OffsetFormat {
precision: OffsetPrecision::Seconds, precision: OffsetPrecision::Seconds,
colons: Colons::Colon, colons: Colons::Colon,
allow_zulu: false, allow_zulu: false,
padding: Pad::Zero, padding: Pad::Zero,
} };
.format(w, off) offset_format.format(w, *off)
}), }
TimezoneOffsetTripleColon => self.off.as_ref().map(|&(_, off)| { (TimezoneOffsetTripleColon, _, _, Some((_, off))) => {
OffsetFormat { let offset_format = OffsetFormat {
precision: OffsetPrecision::Hours, precision: OffsetPrecision::Hours,
colons: Colons::None, colons: Colons::None,
allow_zulu: false, allow_zulu: false,
padding: Pad::Zero, padding: Pad::Zero,
} };
.format(w, off) offset_format.format(w, *off)
}),
Internal(InternalFixed { val: InternalInternal::TimezoneOffsetPermissive }) => {
return Err(fmt::Error);
} }
RFC2822 => (RFC2822, Some(d), Some(t), Some((_, off))) => {
// same as `%a, %d %b %Y %H:%M:%S %z` write_rfc2822(w, crate::NaiveDateTime::new(d, t), *off)
{
if let (Some(d), Some(t), Some(&(_, off))) =
(self.date, self.time, self.off.as_ref())
{
Some(write_rfc2822(w, crate::NaiveDateTime::new(d, t), off))
} else {
None
}
} }
RFC3339 => (RFC3339, Some(d), Some(t), Some((_, off))) => write_rfc3339(
// same as `%Y-%m-%dT%H:%M:%S%.f%:z` w,
{ crate::NaiveDateTime::new(d, t),
if let (Some(d), Some(t), Some(&(_, off))) = *off,
(self.date, self.time, self.off.as_ref()) SecondsFormat::AutoSi,
{ false,
Some(write_rfc3339( ),
w, _ => Err(fmt::Error), // insufficient arguments for given format
crate::NaiveDateTime::new(d, t), }
off.fix(),
SecondsFormat::AutoSi,
false,
))
} else {
None
}
}
};
ret.unwrap_or(Err(fmt::Error)) // insufficient arguments for given format
} }
} }