-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zibi Braniecki
committed
May 10, 2022
1 parent
0740a0e
commit 59f4c3d
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This file is part of ICU4X. For terms of use, please see the file | ||
# called LICENSE at the top level of the ICU4X source tree | ||
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
||
[package] | ||
name = "icu_preferences" | ||
description = "API for resolving user preferences against unicode extensions" | ||
version = "0.0.1" | ||
authors = ["The ICU4X Project Developers"] | ||
edition = "2021" | ||
readme = "README.md" | ||
repository = "https://github.com/unicode-org/icu4x" | ||
license-file = "LICENSE" | ||
categories = ["internationalization"] | ||
# Keep this in sync with other crates unless there are exceptions | ||
include = [ | ||
"src/**/*", | ||
"examples/**/*", | ||
"benches/**/*", | ||
"tests/**/*", | ||
"Cargo.toml", | ||
"LICENSE", | ||
"README.md" | ||
] | ||
|
||
[dev-dependencies] | ||
icu_locid = { version = "0.6", path = "../../components/locid" } | ||
icu_datetime = { version = "0.6", path = "../../components/datetime" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[macro_use] | ||
mod macros; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use icu_datetime::options::preferences::HourCycle; | ||
use icu_locid::{unicode_ext_key, Locale}; | ||
|
||
preferences!( | ||
Preferences, | ||
ResolvedPreferences, | ||
{ | ||
hour_cycle => Option<HourCycle>, HourCycle, Some(unicode_ext_key!("hc")) | ||
} | ||
); | ||
|
||
#[test] | ||
fn it_works() { | ||
let l: Locale = "en-US-u-hc-h23".parse().unwrap(); | ||
|
||
let prefs = Preferences { hour_cycle: None }; | ||
|
||
let defaults = ResolvedPreferences { | ||
hour_cycle: HourCycle::H12, | ||
}; | ||
|
||
let resolved_prefs = ResolvedPreferences::try_from((&prefs, &l, &defaults)).unwrap(); | ||
|
||
assert_eq!(resolved_prefs.hour_cycle, HourCycle::H23); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#[macro_export] | ||
macro_rules! preferences { | ||
($name:ident, $resolved_name:ident, {$($key:ident => $pref:ty, $resolved:ty, $ue:expr),*}) => ( | ||
#[derive(Default)] | ||
pub struct $name { | ||
$( | ||
$key: $pref, | ||
)* | ||
} | ||
|
||
pub struct $resolved_name { | ||
$( | ||
$key: $resolved, | ||
)* | ||
} | ||
|
||
impl TryFrom<(&$name, &Locale, &$resolved_name)> for $resolved_name { | ||
type Error = (); | ||
|
||
fn try_from(input: (&$name, &Locale, &$resolved_name)) -> Result<Self, Self::Error> { | ||
let (prefs, locale, defaults) = input; | ||
let keywords = &locale.extensions.unicode.keywords; | ||
|
||
$( | ||
let mut $key = prefs.$key; | ||
if $key.is_none() { | ||
$key = $ue.and_then( | ||
|ue_key| keywords.get(&ue_key).map(TryInto::try_into) | ||
).transpose()?; | ||
} | ||
)* | ||
Ok($resolved_name { | ||
$( | ||
$key: $key.unwrap_or(defaults.$key), | ||
)* | ||
}) | ||
} | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use icu_datetime::options::preferences::HourCycle; | ||
use icu_locid::{unicode_ext_key, Locale}; | ||
use icu_preferences::preferences; | ||
|
||
#[derive(Clone, Copy)] | ||
enum Calendar { | ||
Gregory, | ||
} | ||
|
||
impl TryFrom<&icu_locid::extensions::unicode::Value> for Calendar { | ||
type Error = (); | ||
|
||
fn try_from(_i: &icu_locid::extensions::unicode::Value) -> Result<Self, Self::Error> { | ||
Ok(Calendar::Gregory) | ||
} | ||
} | ||
|
||
preferences!( | ||
DTFPreferences, | ||
ResolvedDTFPreferences, | ||
{ | ||
hour_cycle => Option<HourCycle>, HourCycle, Some(unicode_ext_key!("hc")), | ||
calendar => Option<Calendar>, Calendar, Some(unicode_ext_key!("ca")) | ||
} | ||
); | ||
|
||
struct DateTimeFormat { | ||
prefs: ResolvedDTFPreferences, | ||
} | ||
|
||
impl DateTimeFormat { | ||
pub fn new(locale: &Locale, prefs: &DTFPreferences) -> Self { | ||
let en_us_defaults = ResolvedDTFPreferences { | ||
hour_cycle: HourCycle::H12, | ||
calendar: Calendar::Gregory, | ||
}; | ||
|
||
let prefs = ResolvedDTFPreferences::try_from((prefs, locale, &en_us_defaults)).unwrap(); | ||
|
||
Self { prefs } | ||
} | ||
|
||
pub fn format(&self, _input: u64) -> String { | ||
match self.prefs.hour_cycle { | ||
HourCycle::H11 => "00:13 am", | ||
HourCycle::H12 => "12:13 am", | ||
HourCycle::H23 => "00:13", | ||
HourCycle::H24 => "24:13", | ||
} | ||
.to_string() | ||
} | ||
} | ||
|
||
#[test] | ||
fn dtf_default() { | ||
let loc: Locale = "en-US".parse().unwrap(); | ||
let prefs = DTFPreferences { | ||
hour_cycle: None, | ||
..Default::default() | ||
}; | ||
let dtf = DateTimeFormat::new(&loc, &prefs); | ||
assert_eq!(dtf.format(0), String::from("12:13 am")); | ||
} | ||
|
||
#[test] | ||
fn dtf_uext() { | ||
let loc: Locale = "en-US-u-hc-h11".parse().unwrap(); | ||
let prefs = DTFPreferences { | ||
hour_cycle: None, | ||
..Default::default() | ||
}; | ||
let dtf = DateTimeFormat::new(&loc, &prefs); | ||
assert_eq!(dtf.format(0), String::from("00:13 am")); | ||
} | ||
|
||
#[test] | ||
fn dtf_prefs() { | ||
let loc: Locale = "en-US-u-hc-h11".parse().unwrap(); | ||
let prefs = DTFPreferences { | ||
hour_cycle: Some(HourCycle::H24), | ||
..Default::default() | ||
}; | ||
let dtf = DateTimeFormat::new(&loc, &prefs); | ||
assert_eq!(dtf.format(0), String::from("24:13")); | ||
} |