Skip to content

Commit

Permalink
Update locale_core::preferences to stakeholder alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Oct 24, 2024
1 parent 257c9bb commit 9bcc486
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 659 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions components/locale_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ litemap = { workspace = true, features = ["alloc"] }
tinystr = { workspace = true, features = ["alloc"] }
writeable = { workspace = true }

databake = { workspace = true, features = ["derive"], optional = true}
databake = { workspace = true, features = ["derive"], optional = true }
serde = { workspace = true, features = ["alloc", "derive"], optional = true }
zerovec = { workspace = true, optional = true }

[dev-dependencies]
iai = { workspace = true }
icu = { path = "../../components/icu", default-features = false }
icu_provider = { workspace = true }
icu_benchmark_macros = { path = "../../tools/benchmark/macros" }
litemap = { path = "../../utils/litemap", features = ["testing"]}
litemap = { path = "../../utils/litemap", features = ["testing"] }
postcard = { workspace = true, features = ["use-std"] }
potential_utf = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand All @@ -51,7 +52,7 @@ zerovec = ["dep:zerovec", "tinystr/zerovec"]
bench = ["serde"]

[lib]
bench = false # This option is required for Benchmark CI
bench = false # This option is required for Benchmark CI

[package.metadata.cargo-all-features]
# Bench feature gets tested separately and is only relevant for CI
Expand Down
166 changes: 82 additions & 84 deletions components/locale_core/src/preferences/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,6 @@
//! The crate is intended primarily to be used by components constructors to normalize the format
//! of ingesting preferences across all of [`ICU4X`].
//!
//! # Examples:
//!
//! ```
//! use icu::locale::preferences::{
//! preferences,
//! extensions::unicode::keywords::HourCycle,
//! };
//! use icu::locale::LanguageIdentifier;
//!
//! pub fn get_defaults(lid: &Option<LanguageIdentifier>) -> ExampleComponentResolvedPreferences {
//! unimplemented!()
//! }
//!
//! preferences!(
//! ExampleComponentPreferences,
//! ExampleComponentResolvedPreferences,
//! {
//! hour_cycle: HourCycle
//! }
//! );
//!
//! pub struct ExampleComponent {
//! resolved_prefs: ExampleComponentResolvedPreferences,
//! }
//!
//! impl ExampleComponent {
//! pub fn new(prefs: ExampleComponentPreferences) -> Self {
//! // Retrieve the default values for the given [`LanguageIdentifier`].
//! let mut resolved_prefs = get_defaults(&prefs.lid);
//!
//! // Resolve them against provided preferences.
//! resolved_prefs.extend(prefs);
//!
//! Self { resolved_prefs }
//! }
//! }
//! ```
//!
//! [`ICU4X`]: ../icu/index.html
//! [`Locale`]: crate::Locale
Expand Down Expand Up @@ -88,7 +50,6 @@ macro_rules! __preferences {
(
$(#[$doc:meta])*
$name:ident,
$resolved_name:ident,
{
$(
$(#[$key_doc:meta])*
Expand All @@ -100,33 +61,23 @@ macro_rules! __preferences {
$(#[$doc])*
#[non_exhaustive]
pub struct $name {
#[doc = concat!("The locale that these `", stringify!($name), "` use.")]
pub lid: Option<$crate::LanguageIdentifier>,
$(
$(#[$key_doc])*
pub $key: Option<$pref>,
)*
}

#[non_exhaustive]
#[derive(Debug, Clone)]
#[doc = concat!("The resolved version of `", stringify!($name), "`.")]
pub struct $resolved_name {
#[doc = concat!("The locale that these `", stringify!($name), "` use.")]
pub lid: $crate::LanguageIdentifier,
pub(crate) language: $crate::subtags::Language,
pub(crate) script: Option<$crate::subtags::Script>,
pub(crate) region: Option<$crate::subtags::Region>,
pub(crate) variant: Option<$crate::subtags::Variant>,
pub(crate) subdivision: Option<$crate::subtags::Subtag>,
pub(crate) ue_region: Option<$crate::subtags::Region>,

$(
$(#[$key_doc])*
pub $key: $pref,
pub $key: Option<$pref>,
)*
}

impl From<$crate::Locale> for $name {
fn from(loc: $crate::Locale) -> Self {
impl From<&$crate::Locale> for $name {
fn from(loc: &$crate::Locale) -> Self {
use $crate::preferences::PreferenceKey;

let lid = Some(loc.id);

$(
let mut $key = None;
)*
Expand All @@ -143,32 +94,91 @@ macro_rules! __preferences {
)*
}

let sd = loc
.extensions
.unicode
.keywords
.get(&$crate::extensions::unicode::key!("sd"))
.and_then(|v| v.as_single_subtag().copied());
let ue_region = loc
.extensions
.unicode
.keywords
.get(&$crate::extensions::unicode::key!("rg"))
.and_then(|v| v.as_single_subtag()
.and_then(|s| $crate::subtags::Region::try_from_str(s.as_str()).ok()));
Self {
lid,
language: loc.id.language,
script: loc.id.script,
region: loc.id.region,
variant: loc.id.variants.iter().copied().next(),
subdivision: sd,
ue_region,

$(
$key,
)*
}
}
}

impl From<&$crate::LanguageIdentifier> for $name {
fn from(lid: &$crate::LanguageIdentifier) -> Self {
Self {
language: lid.language,
script: lid.script,
region: lid.region,
variant: lid.variants.iter().copied().next(),
subdivision: None,
ue_region: None,

$(
$key: None,
)*
}
}
}

impl $name {
/// Constructs a `Locale` corresponding to these preferences.
pub fn into_locale(self) -> $crate::Locale {
use $crate::preferences::PreferenceKey;
let id = self.lid.unwrap_or_default();
let mut extensions = $crate::extensions::Extensions::new();
$(
if let Some(value) = &self.$key {
if let Some(ue) = <$pref>::unicode_extension_key() {
let val = value.unicode_extension_value().unwrap();
extensions.unicode.keywords.set(ue, val);
}
}
)*
$crate::Locale {
id,
extensions
id: $crate::LanguageIdentifier {
language: self.language,
script: self.script,
region: self.region,
variants: self
.variant
.map($crate::subtags::Variants::from_variant)
.unwrap_or_default(),
},
extensions: {
let mut extensions = $crate::extensions::Extensions::default();
if self.subdivision.is_some() || self.ue_region.is_some() {
if let Some(sd) = self.subdivision {
extensions.unicode.keywords.set(
$crate::extensions::unicode::key!("sd"),
$crate::extensions::unicode::Value::from_subtag(Some(sd))
);
}
if let Some(rg) = self.ue_region {
extensions.unicode.keywords.set(
$crate::extensions::unicode::key!("rg"),
$crate::extensions::unicode::Value::try_from_str(rg.as_str()).unwrap()
);
}
}
$(
if let Some(value) = &self.$key {
if let Some(ue) = <$pref>::unicode_extension_key() {
let val = value.unicode_extension_value().unwrap();
extensions.unicode.keywords.set(ue, val);
}
}
)*
extensions
},
}
}

Expand All @@ -193,17 +203,6 @@ macro_rules! __preferences {
)*
}
}

impl $resolved_name {
/// TODO
pub fn extend(&mut self, prefs: $name) {
$(
if let Some(v) = prefs.$key {
self.$key = v;
}
)*
}
}
)
}
#[doc(inline)]
Expand All @@ -226,7 +225,6 @@ mod tests {
preferences!(
/// Preferences for the dummy formatter
DummyPreferences,
DummyResolvedPreferences,
{
/// Controls how dummyly the formatter behaves
dummy_keyword: DummyKeyword
Expand All @@ -235,7 +233,7 @@ mod tests {

let loc: Locale = "und-u-ab-default-cd-foo".parse().unwrap();

let prefs = DummyPreferences::from(loc);
let prefs = DummyPreferences::from(&loc);
assert_eq!(prefs.dummy_keyword, Some(DummyKeyword::Default));
}
}
11 changes: 1 addition & 10 deletions components/locale_core/src/preferences/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,16 @@
#[doc(hidden)]
macro_rules! __options {
($name:ident,
$resolved_name:ident,
{$($key:ident => $pref:ty),*}
) => (
#[derive(Default, Debug, PartialEq)]
#[derive(Default, Debug, Clone)]
#[non_exhaustive]
pub struct $name {
$(
pub $key: Option<$pref>,
)*
}

#[non_exhaustive]
#[derive(Debug, PartialEq)]
pub struct $resolved_name {
$(
pub $key: $pref,
)*
}

impl $name {
pub fn extend(&mut self, other: $name) {
$(
Expand Down
52 changes: 0 additions & 52 deletions components/locale_core/tests/dtf/data_provider.rs

This file was deleted.

Loading

0 comments on commit 9bcc486

Please sign in to comment.