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

Make datagen datetime symbols code generic, add monthPatterns parsing #3767

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Remove macro
  • Loading branch information
Manishearth committed Aug 3, 2023
commit 34f57309096fcb8302c9f6ac2e055795b0aafaa5
79 changes: 27 additions & 52 deletions provider/datagen/src/transform/cldr/cldr_serde/ca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,33 @@ pub struct Contexts<Symbols> {
pub stand_alone: Option<StandAloneWidths<Symbols>>,
}

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

#[derive(Debug, PartialEq, Clone, Deserialize)]
$symbols

symbols!();
}
};
() => {

pub type FormatWidths = super::FormatWidths<Symbols>;
pub type StandAloneWidths = super::StandAloneWidths<Symbols>;
pub type Contexts = super::Contexts<Symbols>;

}
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct MonthSymbols(pub HashMap<String, String>);
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct MonthPatternSymbols {
pub leap: String,
}

symbols!(months, pub struct Symbols(pub HashMap<String, String>););

symbols!(
month_patterns,
pub struct Symbols {
pub leap: String,
}
);

symbols!(
days,
pub struct Symbols {
pub sun: String,
pub mon: String,
pub tue: String,
pub wed: String,
pub thu: String,
pub fri: String,
pub sat: String,
}
);
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct DaySymbols {
pub sun: String,
pub mon: String,
pub tue: String,
pub wed: String,
pub thu: String,
pub fri: String,
pub sat: String,
}

// The day period symbols are Cow<'static, str> instead of String because the Option
// needs to be retained when converting them into Cow for the data provider.
symbols!(
day_periods,
pub struct Symbols {
pub am: Cow<'static, str>,
pub pm: Cow<'static, str>,
pub noon: Option<Cow<'static, str>>,
pub midnight: Option<Cow<'static, str>>,
}
);
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub struct DayPeriodSymbols {
pub am: Cow<'static, str>,
pub pm: Cow<'static, str>,
pub noon: Option<Cow<'static, str>>,
pub midnight: Option<Cow<'static, str>>,
}

#[derive(PartialEq, Debug, Deserialize, Clone)]
#[serde(untagged)]
Expand Down Expand Up @@ -151,13 +125,14 @@ pub struct AvailableFormats(pub HashMap<String, String>);
/// https://github.com/unicode-org/cldr-json/blob/master/cldr-json/cldr-dates-full/main/en/ca-gregorian.json
#[derive(PartialEq, Debug, Deserialize, Clone)]
pub struct Dates {
pub months: months::Contexts,
pub months: Contexts<MonthSymbols>,
#[serde(rename = "monthPatterns")]
pub month_patterns: Option<month_patterns::Contexts>,
pub days: days::Contexts,
// Not used yet, will be in the future
pub month_patterns: Option<Contexts<MonthPatternSymbols>>,
pub days: Contexts<DaySymbols>,
pub eras: Eras,
#[serde(rename = "dayPeriods")]
pub day_periods: day_periods::Contexts,
pub day_periods: Contexts<DayPeriodSymbols>,
#[serde(rename = "dateFormats")]
pub date_formats: LengthPatterns,
#[serde(rename = "timeFormats")]
Expand Down
38 changes: 21 additions & 17 deletions provider/datagen/src/transform/cldr/datetime/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ fn get_era_code_map(calendar: &str) -> BTreeMap<String, TinyStr16> {
}

macro_rules! symbols_from {
([$name: ident, $name2: ident $(,)?], $ctx:ty, [ $($element: ident),+ $(,)? ] $(,)?) => {
impl cldr_serde::ca::$name::Symbols {
([$symbols: path, $name2: ident $(,)?], $ctx:ty, [ $($element: ident),+ $(,)? ] $(,)?) => {
impl $symbols {
fn get(&self, _ctx: &$ctx) -> $name2::SymbolsV1<'static> {
$name2::SymbolsV1([
$(
Expand All @@ -123,10 +123,10 @@ macro_rules! symbols_from {
])
}
}
symbols_from!([$name, $name2], $ctx);
symbols_from!([$symbols, $name2], $ctx);
};
([$name: ident, $name2: ident $(,)?], $ctx:ty, { $($element: ident),+ $(,)? } $(,)?) => {
impl cldr_serde::ca::$name::Symbols {
([$symbols: path, $name2: ident $(,)?], $ctx:ty, { $($element: ident),+ $(,)? } $(,)?) => {
impl $symbols {
fn get(&self, _ctx: &$ctx) -> $name2::SymbolsV1<'static> {
$name2::SymbolsV1 {
$(
Expand All @@ -135,10 +135,10 @@ macro_rules! symbols_from {
}
}
}
symbols_from!([$name, $name], $ctx);
symbols_from!([$symbols, $name2], $ctx);
};
([$name: ident, $name2: ident], $ctx:ty) => {
impl cldr_serde::ca::$name::Symbols {
([$symbols: path, $name2: ident], $ctx:ty) => {
impl $symbols {
// Helper function which returns None if the two groups of symbols overlap.
pub fn get_unaliased(&self, other: &Self) -> Option<Self> {
if self == other {
Expand All @@ -149,7 +149,7 @@ macro_rules! symbols_from {
}
}

impl ca::Contexts<cldr_serde::ca::$name::Symbols> {
impl ca::Contexts<$symbols> {
fn get(&self, ctx: &$ctx) -> $name2::ContextsV1<'static> {
$name2::ContextsV1 {
format: self.format.get(ctx),
Expand All @@ -160,9 +160,9 @@ macro_rules! symbols_from {
}
}

impl ca::StandAloneWidths<cldr_serde::ca::$name::Symbols> {
impl ca::StandAloneWidths<$symbols> {
// Helper function which returns None if the two groups of symbols overlap.
pub fn get_unaliased(&self, other: &cldr_serde::ca::$name::FormatWidths) -> Option<Self> {
pub fn get_unaliased(&self, other: &ca::FormatWidths<$symbols>) -> Option<Self> {
let abbreviated = self.abbreviated.as_ref().and_then(|v| v.get_unaliased(&other.abbreviated));
let narrow = self.narrow.as_ref().and_then(|v| v.get_unaliased(&other.narrow));
let short = if self.short == other.short {
Expand All @@ -185,7 +185,7 @@ macro_rules! symbols_from {
}
}

impl ca::FormatWidths<cldr_serde::ca::$name::Symbols> {
impl ca::FormatWidths<$symbols> {
fn get(&self, ctx: &$ctx) -> $name2::FormatWidthsV1<'static> {
$name2::FormatWidthsV1 {
abbreviated: self.abbreviated.get(ctx),
Expand All @@ -196,7 +196,7 @@ macro_rules! symbols_from {
}
}

impl ca::StandAloneWidths<cldr_serde::ca::$name::Symbols> {
impl ca::StandAloneWidths<$symbols> {
fn get(&self, ctx: &$ctx) -> $name2::StandAloneWidthsV1<'static> {
$name2::StandAloneWidthsV1 {
abbreviated: self.abbreviated.as_ref().map(|width| width.get(ctx)),
Expand All @@ -208,9 +208,9 @@ macro_rules! symbols_from {
}
};
}
symbols_from!([months, months], &'static [TinyStr4]);
symbols_from!([cldr_serde::ca::MonthSymbols, months], &'static [TinyStr4]);

impl cldr_serde::ca::months::Symbols {
impl cldr_serde::ca::MonthSymbols {
fn get(&self, ctx: &&'static [TinyStr4]) -> months::SymbolsV1<'static> {
if ctx.len() == 12 && self.0.len() == 12 {
let mut arr: [Cow<'static, str>; 12] = Default::default();
Expand Down Expand Up @@ -251,11 +251,15 @@ impl cldr_serde::ca::months::Symbols {
}
}

symbols_from!([days, weekdays], (), [sun, mon, tue, wed, thu, fri, sat]);
symbols_from!(
[cldr_serde::ca::DaySymbols, weekdays],
(),
[sun, mon, tue, wed, thu, fri, sat]
);

symbols_from!(
[
day_periods,
cldr_serde::ca::DayPeriodSymbols,
day_periods,
],
(),
Expand Down