mirror of
https://github.com/0x5eal/chrono-lc.git
synced 2025-01-05 18:39:12 +00:00
refactor: move larger match operations into their own functions
This commit is contained in:
parent
5be5d28ceb
commit
a378ed1183
1 changed files with 46 additions and 48 deletions
66
src/lib.rs
66
src/lib.rs
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue