From 6e99b6d5e8a593dd8e35d69031cbfd6a842f61b6 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 14 Nov 2024 16:49:38 -0800 Subject: [PATCH] fix tutorial --- components/decimal/src/provider.rs | 17 ++++++++++++++++- provider/source/src/decimal/mod.rs | 2 +- provider/source/src/decimal/symbols.rs | 3 +-- tutorials/data_provider.md | 10 ++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/components/decimal/src/provider.rs b/components/decimal/src/provider.rs index d855fee925f..641a1cae21d 100644 --- a/components/decimal/src/provider.rs +++ b/components/decimal/src/provider.rs @@ -77,7 +77,9 @@ pub struct GroupingSizesV1 { pub min_grouping: u8, } -/// The strings used in DecimalSymbolsV2 +/// A stack representation of the strings used in [`DecimalSymbolsV2`], i.e. a builder type +/// for [`DecimalSymbolsStrs`]. This type can be obtained from a [`DecimalSymbolsStrs`] +/// the `From`/`Into` traits. /// ///
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, @@ -116,6 +118,19 @@ pub struct DecimalSymbolStrsBuilder<'data> { pub grouping_separator: Cow<'data, str>, } +impl<'data> DecimalSymbolStrsBuilder<'data> { + /// Build a [`DecimalSymbolsStrs`] + pub fn build(&self) -> VarZeroCow<'static, DecimalSymbolsStrs> { + VarZeroCow::from_encodeable(self) + } +} + +impl<'data> From<&'data DecimalSymbolsStrs> for DecimalSymbolStrsBuilder<'data> { + fn from(other: &'data DecimalSymbolsStrs) -> Self { + zerofrom::ZeroFrom::zero_from(other) + } +} + /// Symbols and metadata required for formatting a [`FixedDecimal`](crate::FixedDecimal). /// ///
diff --git a/provider/source/src/decimal/mod.rs b/provider/source/src/decimal/mod.rs index d76f5859698..2dcb0a3aaaa 100644 --- a/provider/source/src/decimal/mod.rs +++ b/provider/source/src/decimal/mod.rs @@ -122,7 +122,7 @@ impl SourceDataProvider { } /// Produce DataIdentifier's for all digit-based numbering systems in the form und/ - #[allow(unused)] // TODO configurable + #[allow(unused)] // TODO we should support using this, https://github.com/unicode-org/icu4x/issues/5824 fn iter_all_number_ids(&self) -> Result>, DataError> { use cldr_serde::numbering_systems::NumberingSystemType; let resource: &cldr_serde::numbering_systems::Resource = self diff --git a/provider/source/src/decimal/symbols.rs b/provider/source/src/decimal/symbols.rs index 7f1280b44ec..68beb13a606 100644 --- a/provider/source/src/decimal/symbols.rs +++ b/provider/source/src/decimal/symbols.rs @@ -10,7 +10,6 @@ use icu_provider::prelude::*; use std::borrow::Cow; use std::collections::HashSet; use std::convert::TryFrom; -use zerovec::VarZeroCow; impl DataProvider for SourceDataProvider { fn load(&self, req: DataRequest) -> Result, DataError> { @@ -90,7 +89,7 @@ impl TryFrom> for DecimalSymbolsV2<'static> { .map_err(|_| format!("Numbering system {nsname} should not be more than 8 bytes!"))?; Ok(Self { - strings: VarZeroCow::from_encodeable(&strings), + strings: strings.build(), grouping_sizes: GroupingSizesV1 { primary: parsed_pattern.positive.primary_grouping, secondary: parsed_pattern.positive.secondary_grouping, diff --git a/tutorials/data_provider.md b/tutorials/data_provider.md index 98e287393bf..94f3afb580e 100644 --- a/tutorials/data_provider.md +++ b/tutorials/data_provider.md @@ -196,7 +196,7 @@ The following example illustrates how to overwrite the decimal separators for a ```rust use core::any::Any; use icu::decimal::FixedDecimalFormatter; -use icu::decimal::provider::DecimalSymbolsV2Marker; +use icu::decimal::provider::{DecimalSymbolsV2Marker, DecimalSymbolStrsBuilder}; use icu_provider::prelude::*; use icu_provider_adapters::fixed::FixedProvider; use icu::locale::locale; @@ -217,8 +217,10 @@ where if req.id.locale.region == Some(region!("CH")) { if let Ok(mut decimal_payload) = res.payload.dynamic_cast_mut::() { decimal_payload.with_mut(|data| { - // Change the digit 0 for all Swiss locales to '🐮' - data.digits[0] = '🐮'; + let mut builder = DecimalSymbolStrsBuilder::from(&*data.strings); + // Change grouping separator for all Swiss locales to '🐮' + builder.grouping_separator = "🐮".into(); + data.strings = builder.build(); }); } } @@ -249,7 +251,7 @@ let formatter = FixedDecimalFormatter::try_new_unstable( ) .unwrap(); -assert_eq!(formatter.format_to_string(&100007i64.into()), "1🐮🐮,🐮🐮7"); +assert_eq!(formatter.format_to_string(&100007i64.into()), "100🐮007"); ``` ## Forking Data Providers