refactor: move larger match operations into their own functions

This commit is contained in:
Erica Marigold 2024-05-12 21:52:01 +05:30
parent 5be5d28ceb
commit a378ed1183
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1

View file

@ -166,7 +166,7 @@ fn parse_fixed(
date: Option<&NaiveDate>,
time: Option<&NaiveTime>,
off: Option<&(String, FixedOffset)>,
spec: Fixed,
spec: &Fixed,
locale: &str,
) -> Option<Result<(), Error>> {
use self::Fixed::*;
@ -244,36 +244,18 @@ fn parse_fixed(
None
}
}
spec => parse_fixed(w, date, time, off, spec, locale),
spec => todo!("Support for formatting fixed format {:?} is yet to be implemented!", spec),
}
}
/// This function is nearly entirely copied from chrono's format()
/// internal formats (3, 6 and 9-digits nanoseconds) have been disabled due to lack of access to chrono internals
pub fn format_l10n<'a, I>(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
time: Option<&NaiveTime>,
off: Option<&(String, FixedOffset)>,
items: I,
locale: &str,
) -> fmt::Result
where
I: Iterator<Item = Item<'a>>,
{
let locale = locale.to_lowercase().replace('_', "-");
for item in items {
match item {
Item::Literal(s) | Item::Space(s) => write!(w, "{}", s)?,
Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => write!(w, "{}", s)?,
Item::Numeric(spec, pad) => {
fn parse_numeric(date: Option<&NaiveDate>, time: Option<&NaiveTime>, off: Option<&(String, FixedOffset)>, spec: &Numeric) -> (usize, Option<i64>) {
use self::Numeric::*;
let week_from_sun = |d: &NaiveDate| (d.ordinal() as i32 - d.weekday().num_days_from_sunday() as i32 + 7) / 7;
let week_from_mon = |d: &NaiveDate| (d.ordinal() as i32 - d.weekday().num_days_from_monday() as i32 + 7) / 7;
let (width, v) = match spec {
match spec {
Year => (4, date.map(|d| i64::from(d.year()))),
YearDiv100 => (2, date.map(|d| div_floor(i64::from(d.year()), 100))),
YearMod100 => (2, date.map(|d| mod_floor(i64::from(d.year()), 100))),
@ -303,8 +285,32 @@ where
),
// for the future expansion
Internal(_) => (1, None),
_ => todo!(),
};
spec => todo!("Support for formatting numeric format {:?} is yet to be implemented!", spec),
}
}
/// This function is nearly entirely copied from chrono's format()
/// internal formats (3, 6 and 9-digits nanoseconds) have been disabled due to lack of access to chrono internals
pub fn format_l10n<'a, I>(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
time: Option<&NaiveTime>,
off: Option<&(String, FixedOffset)>,
items: I,
locale: &str,
) -> fmt::Result
where
I: Iterator<Item = Item<'a>>,
{
let locale = locale.to_lowercase().replace('_', "-");
for item in items {
match item {
Item::Literal(s) | Item::Space(s) => write!(w, "{}", s)?,
Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => write!(w, "{}", s)?,
Item::Numeric(spec, pad) => {
use self::Numeric::{IsoYear, Year};
let (width, v) = parse_numeric(date, time, off, &spec);
if let Some(v) = v {
if (spec == Year || spec == IsoYear) && !(0..10_000).contains(&v) {
@ -326,15 +332,7 @@ where
}
}
Item::Fixed(spec) => {
let ret = parse_fixed(w, date, time, off, spec, &locale);
match ret {
Some(ret) => ret?,
None => return Err(fmt::Error), // insufficient arguments for given format
}
}
Item::Fixed(spec) => parse_fixed(w, date, time, off, &spec, &locale).ok_or(fmt::Error)??,
Item::Error => return Err(fmt::Error),
}
}