Skip to content

Commit

Permalink
Move datetime auxiliary subtag metadata from icu_datagen to component (
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Nov 18, 2023
1 parent abaff69 commit 4d54f71
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 94 deletions.
99 changes: 99 additions & 0 deletions components/datetime/src/provider/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,105 @@ use icu_provider::prelude::*;
use zerovec::ule::UnvalidatedStr;
use zerovec::{VarZeroVec, ZeroMap};

/// Helpers involving the auxiliary subtags used for date symbols.
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
#[allow(missing_docs)]
pub mod aux {
use icu_locid::extensions::private::{subtag, Subtag};

pub const NUMERIC: Subtag = subtag!("1");
pub const ABBR: Subtag = subtag!("3");
pub const NARROW: Subtag = subtag!("4");
pub const WIDE: Subtag = subtag!("5");
pub const SHORT: Subtag = subtag!("6");
pub const ABBR_STANDALONE: Subtag = subtag!("3s");
pub const NARROW_STANDALONE: Subtag = subtag!("4s");
pub const WIDE_STANDALONE: Subtag = subtag!("5s");
pub const SHORT_STANDALONE: Subtag = subtag!("6s");

pub const PATTERN_FULL: Subtag = subtag!("f");
pub const PATTERN_LONG: Subtag = subtag!("l");
pub const PATTERN_MEDIUM: Subtag = subtag!("m");
pub const PATTERN_SHORT: Subtag = subtag!("s");

pub const PATTERN_FULL12: Subtag = subtag!("f12");
pub const PATTERN_LONG12: Subtag = subtag!("l12");
pub const PATTERN_MEDIUM12: Subtag = subtag!("m12");
pub const PATTERN_SHORT12: Subtag = subtag!("s12");

pub const PATTERN_FULL24: Subtag = subtag!("f24");
pub const PATTERN_LONG24: Subtag = subtag!("l24");
pub const PATTERN_MEDIUM24: Subtag = subtag!("m24");
pub const PATTERN_SHORT24: Subtag = subtag!("s24");

/// Field lengths supported in auxiliary subtags.
///
/// For a stable version of this enum, use [`FieldLength`].
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
///
/// [`FieldLength`]: crate::fields::FieldLength
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)] // documented as unstable
pub enum Length {
Abbr,
Narrow,
Wide,
Short,
Numeric,
}

/// Field contexts supported in auxiliary subtags.
///
/// For a stable version of this enum, use one of the specific field symbol enums in [`fields`].
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
///
/// [`fields`]: crate::fields
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[allow(clippy::exhaustive_enums)] // documented as unstable
pub enum Context {
Format,
Standalone,
}

/// Parses an aux key subtag to enum values.
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed
/// to be stable, their Rust representation might not be. Use with caution.
/// </div>
pub fn subtag_info(subtag: Subtag) -> Option<(Context, Length)> {
use {Context::*, Length::*};
match subtag {
NUMERIC => Some((Format, Numeric)),
ABBR => Some((Format, Abbr)),
NARROW => Some((Format, Narrow)),
WIDE => Some((Format, Wide)),
SHORT => Some((Format, Short)),
ABBR_STANDALONE => Some((Standalone, Abbr)),
NARROW_STANDALONE => Some((Standalone, Narrow)),
WIDE_STANDALONE => Some((Standalone, Wide)),
SHORT_STANDALONE => Some((Standalone, Short)),
_ => None,
}
}
}

/// Symbols used for representing the year name
///
/// This uses an auxiliary subtag for length. The subtag is simply the number of
Expand Down
18 changes: 1 addition & 17 deletions provider/datagen/src/transform/cldr/cldr_serde/ca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! Sample file:
//! <https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-dates-full/main/en/ca-gregorian.json>
use icu_datetime::provider::neo::aux::{Context, Length};
use serde::Deserialize;
use std::borrow::Cow;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -44,23 +45,6 @@ pub struct Contexts<Symbols> {
pub numeric: Option<Numeric<Symbols>>,
}

/// A length, for querying Contexts
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Length {
Abbr,
Narrow,
Wide,
Short,
Numeric,
}

/// A context, for querying Contexts
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Context {
Format,
Standalone,
}

impl<Symbols> Contexts<Symbols> {
fn get_symbols_exact(&self, context: Context, length: Length) -> Option<&Symbols> {
use {Context::*, Length::*};
Expand Down
120 changes: 43 additions & 77 deletions provider/datagen/src/transform/cldr/datetime/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use super::supported_cals;
use crate::transform::cldr::cldr_serde::ca::{self, Context, Length, PatternLength};
use crate::transform::cldr::cldr_serde::ca::{self, PatternLength};
use crate::DatagenProvider;
use icu_datetime::pattern::{self, CoarseHourCycle};

use icu_datetime::provider::calendar::{patterns::GenericLengthPatternsV1, DateSkeletonPatternsV1};
use icu_datetime::provider::neo::aux::{self, Context, Length};
use icu_datetime::provider::neo::*;
use icu_locid::{
extensions::private::{subtag, Subtag},
extensions::private::Subtag,
extensions::unicode::{value, Value},
LanguageIdentifier, Locale,
};
Expand All @@ -21,49 +22,8 @@ use std::collections::BTreeMap;
use tinystr::TinyAsciiStr;
use zerovec::ule::UnvalidatedStr;

const NUMERIC: Subtag = subtag!("1");
const ABBR: Subtag = subtag!("3");
const NARROW: Subtag = subtag!("4");
const WIDE: Subtag = subtag!("5");
const SHORT: Subtag = subtag!("6");
const ABBR_STANDALONE: Subtag = subtag!("3s");
const NARROW_STANDALONE: Subtag = subtag!("4s");
const WIDE_STANDALONE: Subtag = subtag!("5s");
const SHORT_STANDALONE: Subtag = subtag!("6s");

const PATTERN_FULL: Subtag = subtag!("f");
const PATTERN_LONG: Subtag = subtag!("l");
const PATTERN_MEDIUM: Subtag = subtag!("m");
const PATTERN_SHORT: Subtag = subtag!("s");

const PATTERN_FULL12: Subtag = subtag!("f12");
const PATTERN_LONG12: Subtag = subtag!("l12");
const PATTERN_MEDIUM12: Subtag = subtag!("m12");
const PATTERN_SHORT12: Subtag = subtag!("s12");

const PATTERN_FULL24: Subtag = subtag!("f24");
const PATTERN_LONG24: Subtag = subtag!("l24");
const PATTERN_MEDIUM24: Subtag = subtag!("m24");
const PATTERN_SHORT24: Subtag = subtag!("s24");

fn aux_subtag_info(subtag: Subtag) -> (Context, Length) {
use {Context::*, Length::*};
match subtag {
NUMERIC => (Format, Numeric),
ABBR => (Format, Abbr),
NARROW => (Format, Narrow),
WIDE => (Format, Wide),
SHORT => (Format, Short),
ABBR_STANDALONE => (Standalone, Abbr),
NARROW_STANDALONE => (Standalone, Narrow),
WIDE_STANDALONE => (Standalone, Wide),
SHORT_STANDALONE => (Standalone, Short),
_ => panic!("Found unexpected auxiliary subtag {}", subtag),
}
}

fn aux_pattern_subtag_info(subtag: Subtag) -> (PatternLength, Option<CoarseHourCycle>) {
use {CoarseHourCycle::*, PatternLength::*};
use {aux::*, CoarseHourCycle::*, PatternLength::*};
match subtag {
PATTERN_FULL => (Full, None),
PATTERN_LONG => (Long, None),
Expand All @@ -87,43 +47,47 @@ fn aux_pattern_subtag_info(subtag: Subtag) -> (PatternLength, Option<CoarseHourC
///
/// We may further investigate and kick out standalone for some keys
const NORMAL_KEY_LENGTHS: &[Subtag] = &[
ABBR,
NARROW,
WIDE,
ABBR_STANDALONE,
NARROW_STANDALONE,
WIDE_STANDALONE,
aux::ABBR,
aux::NARROW,
aux::WIDE,
aux::ABBR_STANDALONE,
aux::NARROW_STANDALONE,
aux::WIDE_STANDALONE,
];

/// Lengths for month data (NORMAL_KEY_LENGTHS + numeric)
const NUMERIC_MONTHS_KEY_LENGTHS: &[Subtag] = &[
ABBR,
NARROW,
WIDE,
ABBR_STANDALONE,
NARROW_STANDALONE,
WIDE_STANDALONE,
NUMERIC,
aux::ABBR,
aux::NARROW,
aux::WIDE,
aux::ABBR_STANDALONE,
aux::NARROW_STANDALONE,
aux::WIDE_STANDALONE,
aux::NUMERIC,
];

/// Lengths for year data (does not do standalone formatting)
const YEARS_KEY_LENGTHS: &[Subtag] = &[ABBR, NARROW, WIDE];
const YEARS_KEY_LENGTHS: &[Subtag] = &[aux::ABBR, aux::NARROW, aux::WIDE];

/// All possible non-numeric lengths
const FULL_KEY_LENGTHS: &[Subtag] = &[
ABBR,
NARROW,
WIDE,
SHORT,
ABBR_STANDALONE,
NARROW_STANDALONE,
WIDE_STANDALONE,
SHORT_STANDALONE,
aux::ABBR,
aux::NARROW,
aux::WIDE,
aux::SHORT,
aux::ABBR_STANDALONE,
aux::NARROW_STANDALONE,
aux::WIDE_STANDALONE,
aux::SHORT_STANDALONE,
];

/// Lengths for normal patterns (not counting hour cycle stuff)
const NORMAL_PATTERN_KEY_LENGTHS: &[Subtag] =
&[PATTERN_FULL, PATTERN_LONG, PATTERN_MEDIUM, PATTERN_SHORT];
const NORMAL_PATTERN_KEY_LENGTHS: &[Subtag] = &[
aux::PATTERN_FULL,
aux::PATTERN_LONG,
aux::PATTERN_MEDIUM,
aux::PATTERN_SHORT,
];

impl DatagenProvider {
fn load_calendar_dates(
Expand Down Expand Up @@ -206,7 +170,9 @@ impl DatagenProvider {
Self: IterableDataProvider<M>,
{
self.load_neo_key(req, &calendar, |langid, data, aux| {
let (context, length) = aux_subtag_info(aux);
let Some((context, length)) = aux::subtag_info(aux) else {
panic!("Found unexpected auxiliary subtag {}", aux)
};
conversion(self, langid, data, &calendar, context, length)
})
}
Expand Down Expand Up @@ -749,14 +715,14 @@ impl IterableDataProvider<TimePatternV1Marker> for DatagenProvider {
let tp = &data.time_formats;

let keylengths = [
PATTERN_FULL,
PATTERN_LONG,
PATTERN_MEDIUM,
PATTERN_SHORT,
nondefault_subtag(&tp.full, PATTERN_FULL12, PATTERN_FULL24),
nondefault_subtag(&tp.long, PATTERN_LONG12, PATTERN_LONG24),
nondefault_subtag(&tp.medium, PATTERN_MEDIUM12, PATTERN_MEDIUM24),
nondefault_subtag(&tp.short, PATTERN_SHORT12, PATTERN_SHORT24),
aux::PATTERN_FULL,
aux::PATTERN_LONG,
aux::PATTERN_MEDIUM,
aux::PATTERN_SHORT,
nondefault_subtag(&tp.full, aux::PATTERN_FULL12, aux::PATTERN_FULL24),
nondefault_subtag(&tp.long, aux::PATTERN_LONG12, aux::PATTERN_LONG24),
nondefault_subtag(&tp.medium, aux::PATTERN_MEDIUM12, aux::PATTERN_MEDIUM24),
nondefault_subtag(&tp.short, aux::PATTERN_SHORT12, aux::PATTERN_SHORT24),
];
keylengths.into_iter().map(move |length| {
let locale: Locale = lid.clone().into();
Expand Down

0 comments on commit 4d54f71

Please sign in to comment.