Skip to content

Commit

Permalink
Create DigitalDurationDataV1 (#5186)
Browse files Browse the repository at this point in the history
  • Loading branch information
kartva authored Jul 8, 2024
1 parent b98c337 commit a6529b8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions components/experimental/src/duration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

mod duration;
mod formatter;
pub mod provider;

pub mod options;

pub use duration::{Duration, DurationSign};
Expand Down
64 changes: 64 additions & 0 deletions components/experimental/src/duration/provider/digital.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 ).

// Provider structs must be stable
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]

//! Provider structs for digital data.
use alloc::borrow::Cow;
use alloc::string::{String, ToString};
use core::str::FromStr;

use displaydoc::Display;
use icu_provider::prelude::*;

#[icu_provider::data_struct(DigitalDurationDataV1Marker = "duration/digital@1")]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_experimental::duration::provider)
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]

/// A struct containing digital duration data (durationUnit-type-* patterns).
pub struct DigitalDurationDataV1<'data> {
/// The separator between the hour, minute, and second fields.
#[cfg_attr(feature = "serde", serde(borrow))]
separator: Cow<str, 'data>,

/// The number of digits to pad the hour field with.
hour_padding: u8,
}

/// Unknown time pattern: {0}
#[derive(Debug, Display)]
pub struct UnknownPatternError(String);

impl<'data> FromStr for DigitalDurationDataV1<'data> {
type Err = UnknownPatternError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"hh:mm:ss" => Ok(DigitalDurationDataV1 {
separator: ":".into(),
hour_padding: 2,
}),
"h:mm:ss" => Ok(DigitalDurationDataV1 {
separator: ":".into(),
hour_padding: 1,
}),
"h.mm.ss" => Ok(DigitalDurationDataV1 {
separator: ".".into(),
hour_padding: 1,
}),
"h,mm,ss" => Ok(DigitalDurationDataV1 {
separator: ",".into(),
hour_padding: 1,
}),
_ => Err(UnknownPatternError(s.to_string())),
}
}
}
10 changes: 10 additions & 0 deletions components/experimental/src/duration/provider/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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 ).

//! Data provider struct definitions for this ICU4X component.
//!
//! Read more about data providers: [`icu_provider`]
pub mod digital;
pub use digital::{DigitalDurationDataV1, DigitalDurationDataV1Marker};
21 changes: 21 additions & 0 deletions provider/source/src/cldr_serde/units/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ pub(crate) struct Patterns {
pub(crate) other_compound_unit_pattern1: Option<String>,
}

#[derive(PartialEq, Debug, Deserialize)]
pub struct DurationUnit {
#[serde(rename = "durationUnitPattern")]
pub(crate) pat: String,
#[serde(rename = "durationUnitPattern-alt-variant")]
pub(crate) alt_pat: Option<String>,
}

#[derive(PartialEq, Debug, Deserialize)]
pub struct DurationUnits {
#[serde(rename = "durationUnit-type-hm")]
pub hm: DurationUnit,
#[serde(rename = "durationUnit-type-hms")]
pub hms: DurationUnit,
#[serde(rename = "durationUnit-type-ms")]
pub ms: DurationUnit,
}

// TODO: replace Value with specific structs
#[derive(PartialEq, Debug, Deserialize)]
pub(crate) struct UnitsData {
Expand All @@ -70,6 +88,9 @@ pub(crate) struct UnitsData {
pub(crate) short: BTreeMap<String, Patterns>,

pub(crate) narrow: BTreeMap<String, Patterns>,

#[serde(flatten)]
pub(crate) duration: DurationUnits,
}

#[derive(PartialEq, Debug, Deserialize)]
Expand Down

0 comments on commit a6529b8

Please sign in to comment.