Skip to content

Commit

Permalink
Update Currency Data Field Names for Clarity and Consistency (#4697)
Browse files Browse the repository at this point in the history
  • Loading branch information
younies committed Mar 17, 2024
1 parent b36e0e4 commit 942ed63
Show file tree
Hide file tree
Showing 23 changed files with 8,960 additions and 8,955 deletions.
26 changes: 14 additions & 12 deletions components/experimental/src/dimension/provider/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ pub use crate::provider::Baked;
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[yoke(prove_covariance_manually)]
pub struct CurrencyEssentialsV1<'data> {
/// Maps from currency iso code to currency patterns
/// which points to which pattern to use and the place holder index.
/// A mapping from each currency's ISO code to its associated formatting patterns.
/// This includes information on which specific pattern to apply as well as the index
/// of placeholders within the `placeholders` vector.
#[cfg_attr(feature = "serde", serde(borrow))]
pub currency_patterns_map: ZeroMap<'data, UnvalidatedTinyAsciiStr<3>, CurrencyPatterns>,
pub pattern_config_map: ZeroMap<'data, UnvalidatedTinyAsciiStr<3>, CurrencyPatternConfig>,

// TODO(#4677): Implement the pattern to accept the signed negative and signed positive patterns.
/// Represents the standard pattern.
Expand All @@ -64,10 +65,11 @@ pub struct CurrencyEssentialsV1<'data> {

/// Contains all the place holders.
#[cfg_attr(feature = "serde", serde(borrow))]
pub place_holders: VarZeroVec<'data, str>,
pub placeholders: VarZeroVec<'data, str>,

/// Represents the currency patten in case the currency patterns map does not contain the currency.
pub default_pattern: CurrencyPatterns,
/// The fallback currency pattern configuration used
/// when a specific currency's pattern is not found in the currency patterns map.
pub default_pattern_config: CurrencyPatternConfig,
}

#[zerovec::make_ule(PatternSelectionULE)]
Expand Down Expand Up @@ -98,7 +100,7 @@ pub enum PatternSelection {
#[repr(u16)]
pub enum PlaceholderValue {
/// The index of the place holder in the place holders list.
/// NOTE: the maximum value is MAX_PLACE_HOLDER_INDEX which is 2045 (0b0111_1111_1101).
/// NOTE: the maximum value is MAX_PLACEHOLDER_INDEX which is 2045 (0b0111_1111_1101).
Index(u16),

/// The place holder is the iso code.
Expand All @@ -111,20 +113,20 @@ pub enum PlaceholderValue {
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[derive(Copy, Debug, Clone, Default, PartialEq, PartialOrd, Eq, Ord)]
pub struct CurrencyPatterns {
pub struct CurrencyPatternConfig {
/// If it is true, then use the standard pattern.
/// Otherwise, use the standard_alpha_next_to_number pattern.
pub short_pattern_standard: PatternSelection,
pub short_pattern_selection: PatternSelection,

/// If it is true, then use the standard pattern.
/// Otherwise, use the standard_alpha_next_to_number pattern.
pub narrow_pattern_standard: PatternSelection,
pub narrow_pattern_selection: PatternSelection,

/// The index of the short pattern place holder in the place holders list.
/// If the value is `None`, this means that the short pattern does not have a place holder.
pub short_place_holder_index: Option<PlaceholderValue>,
pub short_placeholder_index: Option<PlaceholderValue>,

/// The index of the narrow pattern place holder in the place holders list.
/// If the value is `None`, this means that the narrow pattern does not have a place holder.
pub narrow_place_holder_index: Option<PlaceholderValue>,
pub narrow_placeholder_index: Option<PlaceholderValue>,
}
78 changes: 40 additions & 38 deletions components/experimental/src/dimension/ule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ use zerovec::{
ule::{AsULE, ZeroVecError, ULE},
};

use crate::dimension::provider::currency::{CurrencyPatterns, PatternSelection, PlaceholderValue};
use crate::dimension::provider::currency::{
CurrencyPatternConfig, PatternSelection, PlaceholderValue,
};

const NO_PLACE_HOLDER: u16 = 0b0111_1111_1111; // decimal: 2047
const NO_PLACEHOLDER: u16 = 0b0111_1111_1111; // decimal: 2047
const USE_ISO_CODE: u16 = 0b0111_1111_1110; // decimal: 2046

// TODO(#4013): Remove this constant once we have an invariant that the injecting text index is always less than 2046.
pub const MAX_PLACE_HOLDER_INDEX: u16 = 0b0111_1111_1101; // decimal: 2045
pub const MAX_PLACEHOLDER_INDEX: u16 = 0b0111_1111_1101; // decimal: 2045

/// `CurrencyPatternsULE` is a type optimized for efficient storing and
/// deserialization of `CurrencyPatterns` using the `ZeroVec` model.
///
/// The serialization model packages the pattern item in three bytes.
///
/// The first bit (b7) is used to determine the short_pattern_standard. If the bit is `0`, then, the value will be `Standard`.
/// The first bit (b7) is used to determine the short_pattern_selection. If the bit is `0`, then, the value will be `Standard`.
/// If the bit is `1`, then, the value will be `StandardAlphaNextToNumber`.
///
/// The second bit (b6) is used to determine the narrow_pattern_standard. If the bit is `0`, then, the value will be `Standard`.
/// The second bit (b6) is used to determine the narrow_pattern_selection. If the bit is `0`, then, the value will be `Standard`.
/// If the bit is `1`, then, the value will be `StandardAlphaNextToNumber`.
///
/// The next three bits (b5, b4 & b3) with the second byte is used to determine the short_place_holder_index.
/// The next three bits (b2, b1 & b0) with the third byte is used to determine the narrow_place_holder_index.
/// The next three bits (b5, b4 & b3) with the second byte is used to determine the short_placeholder_index.
/// The next three bits (b2, b1 & b0) with the third byte is used to determine the narrow_placeholder_index.
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(transparent)]
pub struct CurrencyPatternsULE([u8; 3]);
Expand Down Expand Up @@ -56,45 +58,45 @@ const PATTERN_NARROW_SHIFT: u8 = 6;
const INDEX_SHORT_SHIFT: u8 = 3;
const INDEX_NARROW_SHIFT: u8 = 0;

impl AsULE for CurrencyPatterns {
impl AsULE for CurrencyPatternConfig {
type ULE = CurrencyPatternsULE;

#[inline]
fn to_unaligned(self) -> Self::ULE {
let mut first_byte_ule: u8 = 0;

if self.short_pattern_standard == PatternSelection::StandardAlphaNextToNumber {
if self.short_pattern_selection == PatternSelection::StandardAlphaNextToNumber {
first_byte_ule |= 0b1 << PATTERN_SHORT_SHIFT;
}
if self.narrow_pattern_standard == PatternSelection::StandardAlphaNextToNumber {
if self.narrow_pattern_selection == PatternSelection::StandardAlphaNextToNumber {
first_byte_ule |= 0b1 << PATTERN_NARROW_SHIFT;
}

// For short_place_holder_index
// For short_placeholder_index
let [short_most_significant_byte, short_least_significant_byte_ule] =
match self.short_place_holder_index {
match self.short_placeholder_index {
Some(PlaceholderValue::Index(index)) => index.to_be_bytes(),
Some(PlaceholderValue::ISO) => USE_ISO_CODE.to_be_bytes(),
None => NO_PLACE_HOLDER.to_be_bytes(),
None => NO_PLACEHOLDER.to_be_bytes(),
};
if short_most_significant_byte & 0b1111_1000 != 0 {
panic!(
"short_place_holder_index is too large {}, {}",
"short_placeholder_index is too large {}, {}",
short_most_significant_byte, short_least_significant_byte_ule
)
}
first_byte_ule |= short_most_significant_byte << INDEX_SHORT_SHIFT;

// For narrow_place_holder_index
// For narrow_placeholder_index
let [narrow_most_significant_byte, narrow_least_significant_byte_ule] =
match self.narrow_place_holder_index {
match self.narrow_placeholder_index {
Some(PlaceholderValue::Index(index)) => index.to_be_bytes(),
Some(PlaceholderValue::ISO) => USE_ISO_CODE.to_be_bytes(),
None => NO_PLACE_HOLDER.to_be_bytes(),
None => NO_PLACEHOLDER.to_be_bytes(),
};
if narrow_most_significant_byte & 0b1111_1000 != 0 {
panic!(
"narrow_place_holder_index is too large {}, {}",
"narrow_placeholder_index is too large {}, {}",
narrow_most_significant_byte, narrow_least_significant_byte_ule
)
}
Expand All @@ -111,14 +113,14 @@ impl AsULE for CurrencyPatterns {
fn from_unaligned(unaligned: Self::ULE) -> Self {
let [first_byte, second_byte, third_byte] = unaligned.0;

let short_pattern_standard =
let short_pattern_selection =
if first_byte & (0b1 << PATTERN_SHORT_SHIFT) == 0b1 << PATTERN_SHORT_SHIFT {
PatternSelection::StandardAlphaNextToNumber
} else {
PatternSelection::Standard
};

let narrow_pattern_standard =
let narrow_pattern_selection =
if first_byte & (0b1 << PATTERN_NARROW_SHIFT) == 0b1 << PATTERN_NARROW_SHIFT {
PatternSelection::StandardAlphaNextToNumber
} else {
Expand All @@ -127,39 +129,39 @@ impl AsULE for CurrencyPatterns {
let short_prefix = (first_byte & 0b111 << INDEX_SHORT_SHIFT) >> INDEX_SHORT_SHIFT;
let narrow_prefix = (first_byte & 0b111 << INDEX_NARROW_SHIFT) >> INDEX_NARROW_SHIFT;

let short_place_holder_index = ((short_prefix as u16) << 8) | second_byte as u16;
let narrow_place_holder_index = ((narrow_prefix as u16) << 8) | third_byte as u16;
let short_placeholder_index = ((short_prefix as u16) << 8) | second_byte as u16;
let narrow_placeholder_index = ((narrow_prefix as u16) << 8) | third_byte as u16;

let short_place_holder_index = match short_place_holder_index {
NO_PLACE_HOLDER => None,
let short_placeholder_index = match short_placeholder_index {
NO_PLACEHOLDER => None,
USE_ISO_CODE => Some(PlaceholderValue::ISO),
index => {
debug_assert!(index <= MAX_PLACE_HOLDER_INDEX);
debug_assert!(index <= MAX_PLACEHOLDER_INDEX);
Some(PlaceholderValue::Index(index))
}
};

let narrow_place_holder_index = match narrow_place_holder_index {
NO_PLACE_HOLDER => None,
let narrow_placeholder_index = match narrow_placeholder_index {
NO_PLACEHOLDER => None,
USE_ISO_CODE => Some(PlaceholderValue::ISO),
index => {
debug_assert!(index <= MAX_PLACE_HOLDER_INDEX);
debug_assert!(index <= MAX_PLACEHOLDER_INDEX);
Some(PlaceholderValue::Index(index))
}
};

CurrencyPatterns {
short_pattern_standard,
narrow_pattern_standard,
short_place_holder_index,
narrow_place_holder_index,
CurrencyPatternConfig {
short_pattern_selection,
narrow_pattern_selection,
short_placeholder_index,
narrow_placeholder_index,
}
}
}

impl<'a> ZeroMapKV<'a> for CurrencyPatterns {
type Container = zerovec::ZeroVec<'a, CurrencyPatterns>;
type Slice = zerovec::ZeroSlice<CurrencyPatterns>;
type GetType = <CurrencyPatterns as AsULE>::ULE;
type OwnedType = CurrencyPatterns;
impl<'a> ZeroMapKV<'a> for CurrencyPatternConfig {
type Container = zerovec::ZeroVec<'a, CurrencyPatternConfig>;
type Slice = zerovec::ZeroSlice<CurrencyPatternConfig>;
type GetType = <CurrencyPatternConfig as AsULE>::ULE;
type OwnedType = CurrencyPatternConfig;
}
Loading

0 comments on commit 942ed63

Please sign in to comment.