Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subset of dates data to DataProvider #256

Merged
merged 7 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions components/data-provider/src/data_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tinystr::TinyStr16;
pub enum DataCategory {
Decimal,
Plurals,
Dates,
PrivateUse(TinyStr16),
}

Expand All @@ -19,6 +20,7 @@ impl DataCategory {
match self {
DataCategory::Decimal => Cow::Borrowed("decimal"),
DataCategory::Plurals => Cow::Borrowed("plurals"),
DataCategory::Dates => Cow::Borrowed("dates"),
DataCategory::PrivateUse(id) => {
let mut result = String::from("x-");
result.push_str(id.as_str());
Expand Down Expand Up @@ -64,6 +66,9 @@ macro_rules! icu_data_key {
(plurals: $sub_category:tt @ $version:tt) => {
icu_data_key!($crate::DataCategory::Plurals, $sub_category, $version)
};
(dates: $sub_category:tt @ $version:tt) => {
icu_data_key!($crate::DataCategory::Dates, $sub_category, $version)
};
(x-$private_use:tt: $sub_category:tt @ $version:tt) => {
icu_data_key!(
$crate::DataCategory::PrivateUse(stringify!($private_use).parse().unwrap()),
Expand All @@ -86,6 +91,7 @@ fn test_data_key_macro(category: DataCategory) {
let data_key_1 = match category {
DataCategory::Decimal => icu_data_key!(decimal: foo@1),
DataCategory::Plurals => icu_data_key!(plurals: foo@1),
DataCategory::Dates => icu_data_key!(dates: foo@1),
DataCategory::PrivateUse(_) => icu_data_key!(x-private: foo@1),
};
let data_key_2 = DataKey {
Expand All @@ -100,6 +106,7 @@ fn test_data_key_macro(category: DataCategory) {
fn test_all_data_key_macros() {
test_data_key_macro(DataCategory::Decimal);
test_data_key_macro(DataCategory::Plurals);
test_data_key_macro(DataCategory::Dates);
test_data_key_macro(DataCategory::PrivateUse("private".parse().unwrap()));
}

Expand Down
110 changes: 110 additions & 0 deletions components/data-provider/src/structs/dates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Date
#[cfg(feature = "invariant")]
use crate::prelude::*;

/// Gets a locale-invariant default struct given a data key in this module's category.
#[cfg(feature = "invariant")]
pub(crate) fn get_invariant(data_key: &DataKey) -> Option<DataResponse<'static>> {
use crate::invariant::make_inv_response;
if data_key.category != DataCategory::Dates {
return None;
}
match (data_key.sub_category.as_str(), data_key.version) {
("gregory", 1) => make_inv_response::<gregory::DatesV1>(),
_ => None,
}
}

pub mod gregory {
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
pub struct DatesV1 {
pub symbols: DateSymbolsV1,

pub patterns: PatternsV1,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
pub struct DateSymbolsV1 {
pub months: months::ContextsV1,

pub weekdays: weekdays::ContextsV1,

pub day_periods: day_periods::ContextsV1,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
pub struct PatternsV1 {
pub date: patterns::StylePatternsV1,

pub time: patterns::StylePatternsV1,

pub date_time: patterns::StylePatternsV1,
}

macro_rules! symbols {
($name: ident, $expr: ty) => {
pub mod $name {
use super::*;

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
pub struct SymbolsV1(pub $expr);

symbols!();
}
};
($name: ident, $($element: ident: $ty: ty),*) => {
pub mod $name {
use super::*;

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
pub struct SymbolsV1 {
$(pub $element: $ty),*
}
symbols!();
}
};
() => {
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
pub struct WidthsV1 {
#[serde(skip_serializing_if = "Option::is_none")]
pub abbreviated: Option<SymbolsV1>,

#[serde(skip_serializing_if = "Option::is_none")]
pub narrow: Option<SymbolsV1>,

#[serde(skip_serializing_if = "Option::is_none")]
pub short: Option<SymbolsV1>,

#[serde(skip_serializing_if = "Option::is_none")]
pub wide: Option<SymbolsV1>,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
pub struct ContextsV1 {
pub format: WidthsV1,

#[serde(skip_serializing_if = "Option::is_none")]
pub stand_alone: Option<WidthsV1>,
}
};
}

symbols!(months, [Cow<'static, str>; 12]);

symbols!(weekdays, [Cow<'static, str>; 7]);

symbols!(day_periods, am: Cow<'static, str>, pm: Cow<'static, str>);

pub mod patterns {
use super::*;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
pub struct StylePatternsV1 {
pub full: Cow<'static, str>,
pub long: Cow<'static, str>,
pub medium: Cow<'static, str>,
pub short: Cow<'static, str>,
}
}
}
2 changes: 2 additions & 0 deletions components/data-provider/src/structs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod dates;
pub mod decimal;
pub mod plurals;

Expand All @@ -12,4 +13,5 @@ pub(crate) fn get_invariant(data_key: &DataKey) -> Option<DataResponse<'static>>
None //
.or_else(|| decimal::get_invariant(data_key)) //
.or_else(|| plurals::get_invariant(data_key)) //
.or_else(|| dates::get_invariant(data_key)) //
}
14 changes: 1 addition & 13 deletions components/data-provider/src/structs/plurals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) fn get_invariant(data_key: &DataKey) -> Option<DataResponse<'static>>
///
/// More information: https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "invariant", derive(Default))]
pub struct PluralRuleStringsV1 {
#[serde(skip_serializing_if = "Option::is_none")]
pub zero: Option<Cow<'static, str>>,
Expand All @@ -37,16 +38,3 @@ pub struct PluralRuleStringsV1 {
#[serde(skip_serializing_if = "Option::is_none")]
pub many: Option<Cow<'static, str>>,
}

#[cfg(feature = "invariant")]
impl Default for PluralRuleStringsV1 {
fn default() -> Self {
Self {
zero: None,
one: None,
two: None,
few: None,
many: None,
}
}
}