Skip to content

Commit

Permalink
fix tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Nov 15, 2024
1 parent f6ffefe commit 6e99b6d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
17 changes: 16 additions & 1 deletion components/decimal/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
Expand Down Expand Up @@ -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).
///
/// <div class="stab unstable">
Expand Down
2 changes: 1 addition & 1 deletion provider/source/src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl SourceDataProvider {
}

/// Produce DataIdentifier's for all digit-based numbering systems in the form und/<numsys>
#[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<HashSet<DataIdentifierCow<'static>>, DataError> {
use cldr_serde::numbering_systems::NumberingSystemType;
let resource: &cldr_serde::numbering_systems::Resource = self
Expand Down
3 changes: 1 addition & 2 deletions provider/source/src/decimal/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DecimalSymbolsV2Marker> for SourceDataProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<DecimalSymbolsV2Marker>, DataError> {
Expand Down Expand Up @@ -90,7 +89,7 @@ impl TryFrom<NumbersWithNumsys<'_>> 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,
Expand Down
10 changes: 6 additions & 4 deletions tutorials/data_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -217,8 +217,10 @@ where
if req.id.locale.region == Some(region!("CH")) {
if let Ok(mut decimal_payload) = res.payload.dynamic_cast_mut::<DecimalSymbolsV2Marker>() {
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();
});
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6e99b6d

Please sign in to comment.