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.
Move
MeasureUnit
to separate module and Implement UnitsTrieV1
Dat…
…a Provider (unicode-org#5348)
- Loading branch information
Showing
31 changed files
with
604 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 smallvec::SmallVec; | ||
|
||
use super::provider::single_unit::SingleUnit; | ||
|
||
// TODO NOTE: the MeasureUnitParser takes the trie and the ConverterFactory takes the full payload and an instance of MeasureUnitParser. | ||
/// MeasureUnit is a struct that contains a processed CLDR unit. | ||
/// For example, "meter-per-second". | ||
/// NOTE: | ||
/// - To construct a MeasureUnit from a cldr unit identifier, use the `MeasureUnitParser`. | ||
#[derive(Debug)] | ||
pub struct MeasureUnit { | ||
// TODO: make this field private and add functions to use it. | ||
/// Contains the processed units. | ||
pub contained_units: SmallVec<[SingleUnit; 8]>, | ||
} |
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 |
---|---|---|
|
@@ -4,3 +4,7 @@ | |
|
||
pub(crate) mod power; | ||
pub(crate) mod si_prefix; | ||
|
||
pub mod measureunit; | ||
pub mod parser; | ||
pub mod provider; |
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,7 @@ | ||
// 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 si_prefix; | ||
pub mod single_unit; | ||
pub mod trie; |
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,47 @@ | ||
// 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`] | ||
/// Represents the base of an si prefix. | ||
#[zerovec::make_ule(BaseULE)] | ||
#[cfg_attr( | ||
feature = "datagen", | ||
derive(serde::Serialize, databake::Bake), | ||
databake(path = icu_experimental::units::provider), | ||
)] | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] | ||
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Default)] | ||
#[repr(u8)] | ||
pub enum Base { | ||
/// The base of the si prefix is 10. | ||
#[default] | ||
Decimal = 0, | ||
|
||
/// The base of the si prefix is 2. | ||
Binary = 1, | ||
} | ||
|
||
// TODO: Consider reducing the size of this struct while implementing the ULE. | ||
/// Represents the SI prefix. | ||
#[zerovec::make_ule(SiPrefixULE)] | ||
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Default)] | ||
#[cfg_attr( | ||
feature = "datagen", | ||
derive(serde::Serialize, databake::Bake), | ||
databake(path = icu_experimental::units::provider), | ||
)] | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] | ||
pub struct SiPrefix { | ||
/// The absolute value of the power of the si prefix. | ||
pub power: i8, | ||
|
||
/// The base of the si prefix. | ||
pub base: Base, | ||
} |
28 changes: 28 additions & 0 deletions
28
components/experimental/src/measure/provider/single_unit.rs
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,28 @@ | ||
// 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 super::si_prefix::SiPrefix; | ||
|
||
/// Represents a single unit in a measure unit. | ||
/// For example, the MeasureUnit `kilometer-per-square-second` contains two single units: | ||
/// 1. `kilometer` with power 1 and prefix 3 with base 10. | ||
/// 2. `second` with power -2 and prefix power equal to 0. | ||
#[zerovec::make_ule(SingleUnitULE)] | ||
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Default)] | ||
#[cfg_attr( | ||
feature = "datagen", | ||
derive(serde::Serialize, databake::Bake), | ||
databake(path = icu_experimental::units::provider), | ||
)] | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] | ||
pub struct SingleUnit { | ||
/// The power of the unit. | ||
pub power: i8, | ||
|
||
/// The si base of the unit. | ||
pub si_prefix: SiPrefix, | ||
|
||
/// The id of the unit. | ||
pub unit_id: u16, | ||
} |
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,46 @@ | ||
// 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 icu_provider::prelude::*; | ||
use zerotrie::ZeroTrieSimpleAscii; | ||
use zerovec::ZeroVec; | ||
|
||
#[cfg(feature = "compiled_data")] | ||
/// Baked data | ||
/// | ||
/// <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. In particular, the `DataProvider` implementations are only | ||
/// guaranteed to match with this version's `*_unstable` providers. Use with caution. | ||
/// </div> | ||
pub use crate::provider::Baked; | ||
|
||
/// This type encapsulates all the constant data required for unit conversions. | ||
/// | ||
/// <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> | ||
#[icu_provider::data_struct(marker(UnitsTrieV1Marker, "units/trie@1", singleton))] | ||
#[derive(Clone, PartialEq, Debug)] | ||
#[cfg_attr( | ||
feature = "datagen", | ||
derive(serde::Serialize, databake::Bake), | ||
databake(path = icu_experimental::measure::provider::trie), | ||
)] | ||
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] | ||
pub struct UnitsTrieV1<'data> { | ||
// TODO: remove this field from units/provider::UnitsInfoV1 once the `MeasureUnit` is fully used in the measurement units. | ||
/// Maps from unit name (e.g. foot or meter) to its unit id. this id can be used to retrieve the conversion information from the `UnitsInfoV1`. | ||
#[cfg_attr(feature = "serde", serde(borrow))] | ||
pub trie: ZeroTrieSimpleAscii<ZeroVec<'data, u8>>, | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.