Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Forked at: aaee912
Parent branch: chronotope/main
  • Loading branch information
cecton committed Jul 12, 2020
1 parent 19f54ab commit 870a7cc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 66 deletions.
87 changes: 22 additions & 65 deletions src/format/locales.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
#[cfg(feature = "locales")]
mod with_locales {
use super::super::{FormatError, FormatErrorKind, FormatResult};

macro_rules! locale_match {
($locale:expr => $($item:ident)::+) => {{
match $locale {
"en_US" => Ok(pure_rust_locales::en_US::$($item)::+),
"fr_BE" => Ok(pure_rust_locales::fr_BE::$($item)::+),
// TODO: all the locales are available
_ => Err(FormatError(FormatErrorKind::UnknownLocale)),
}
}}
}

pub(crate) fn short_months(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::ABMON)
}

pub(crate) fn long_months(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::MON)
}

pub(crate) fn short_weekdays(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::ABDAY)
}

pub(crate) fn long_weekdays(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::DAY)
}
use super::{FormatError, FormatErrorKind, FormatResult};

macro_rules! locale_match {
($locale:expr => $($item:ident)::+) => {{
match $locale {
"en_US" => Ok(pure_rust_locales::en_US::$($item)::+),
"fr_BE" => Ok(pure_rust_locales::fr_BE::$($item)::+),
// TODO: all the locales are available
_ => Err(FormatError(FormatErrorKind::UnknownLocale)),
}
}}
}

#[cfg(not(feature = "locales"))]
mod without_locales {
use super::super::FormatResult;

pub(crate) fn short_months(_locale: &str) -> FormatResult<&[&'static str]> {
Ok(&["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
}

pub(crate) fn long_months(_locale: &str) -> FormatResult<&[&'static str]> {
Ok(&[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
])
}

pub(crate) fn short_weekdays(_locale: &str) -> FormatResult<&[&'static str]> {
Ok(&["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"])
}
pub(crate) fn short_months(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::ABMON)
}

pub(crate) fn long_weekdays(_locale: &str) -> FormatResult<&[&'static str]> {
Ok(&["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"])
}
pub(crate) fn long_months(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::MON)
}

#[cfg(feature = "locales")]
pub(crate) use self::with_locales::*;
pub(crate) fn short_weekdays(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::ABDAY)
}

#[cfg(not(feature = "locales"))]
pub(crate) use self::without_locales::*;
pub(crate) fn long_weekdays(locale: &str) -> FormatResult<&[&'static str]> {
locale_match!(locale => LC_TIME::DAY)
}
29 changes: 28 additions & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use offset::{FixedOffset, Offset};
use {Datelike, Timelike};
use {ParseWeekdayError, Weekday};

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(all(feature = "locales", any(feature = "alloc", feature = "std", test)))]
mod locales;

pub use self::parse::parse;
Expand Down Expand Up @@ -464,10 +464,37 @@ fn format_inner<'a>(
locale: &str,
) -> FormatResult<()> {
// full and abbreviated month and weekday names
#[cfg(feature = "locales")]
let short_months = locales::short_months(locale)?;
#[cfg(feature = "locales")]
let long_months = locales::long_months(locale)?;
#[cfg(feature = "locales")]
let short_weekdays = locales::short_weekdays(locale)?;
#[cfg(feature = "locales")]
let long_weekdays = locales::long_weekdays(locale)?;
#[cfg(not(feature = "locales"))]
let short_months =
&["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
#[cfg(not(feature = "locales"))]
let long_months = &[
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
#[cfg(not(feature = "locales"))]
let short_weekdays = &["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
#[cfg(not(feature = "locales"))]
let long_weekdays =
&["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

use core::fmt::Write;

Expand Down

0 comments on commit 870a7cc

Please sign in to comment.