forked from unicode-org/icu4x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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)] | ||
|
||
//! Data provider struct definitions for this ICU4X component. | ||
//! | ||
//! Read more about data providers: [`icu_provider`] | ||
use alloc::borrow::Cow; | ||
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> { | ||
separator: Cow<str, 'data>, | ||
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())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// 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 ). | ||
|
||
pub mod digital; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 ). | ||
|
||
use crate::cldr_serde; | ||
use crate::cldr_serde::units::data::DurationUnits; | ||
use crate::DatagenProvider; | ||
use icu_provider::prelude::*; | ||
use std::collections::HashSet; | ||
use std::str::FromStr; | ||
|
||
use icu::experimental::duration::provider::digital::{ | ||
DigitalDurationDataV1, DigitalDurationDataV1Marker, | ||
}; | ||
|
||
impl DataProvider<DigitalDurationDataV1Marker> for DatagenProvider { | ||
fn load( | ||
&self, | ||
req: DataRequest, | ||
) -> Result<DataResponse<DigitalDurationDataV1Marker>, DataError> { | ||
self.check_req::<DigitalDurationDataV1Marker>(req)?; | ||
let langid = req.id.locale.get_langid(); | ||
|
||
// Get units | ||
let units_format_data: &cldr_serde::units::data::Resource = | ||
self.cldr()?.units().read_and_parse(&langid, "units.json")?; | ||
let DurationUnits { hms, .. } = &units_format_data.main.value.units.duration; | ||
|
||
let result = DigitalDurationDataV1::from_str(&hms.pat).map_err(|err| { | ||
DataError::custom("Failed to convert hms pattern: ").with_debug_context(&err) | ||
})?; | ||
|
||
Ok(DataResponse { | ||
metadata: Default::default(), | ||
payload: DataPayload::from_owned(result), | ||
}) | ||
} | ||
} | ||
|
||
impl crate::IterableDataProviderCached<DigitalDurationDataV1Marker> for DatagenProvider { | ||
fn iter_ids_cached(&self) -> Result<HashSet<DataIdentifierCow<'static>>, DataError> { | ||
Ok(self | ||
.cldr()? | ||
.numbers() | ||
.list_langs()? | ||
.filter(|langid| { | ||
self.cldr() | ||
.unwrap() | ||
.units() | ||
.read_and_parse::<cldr_serde::units::data::Resource>(langid, "units.json") | ||
.is_ok() | ||
}) | ||
.map(|langid| DataIdentifierCow::from_locale(DataLocale::from(&langid))) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters