Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create PercentFormatter #4483

Open
sffc opened this issue Dec 21, 2023 · 1 comment
Open

Create PercentFormatter #4483

sffc opened this issue Dec 21, 2023 · 1 comment
Labels
C-numbers Component: Numbers, units, currencies S-medium Size: Less than a week (larger bug fix or enhancement) U-ecma402 User: ECMA-402 compatibility

Comments

@sffc
Copy link
Member

sffc commented Dec 21, 2023

We do not currently have percentage formatting. We will need it for ECMA-402 compatibility.

We can do this in the scope of measurement unit formatting, but I think it would be useful to have a standalone percentage formatter since this is how it worked in previous versions of ECMA-402.

As a first step, we can make a data struct, probably in the icu_dimension crate.

@sffc sffc added C-numbers Component: Numbers, units, currencies U-ecma402 User: ECMA-402 compatibility labels Dec 21, 2023
@sffc
Copy link
Member Author

sffc commented Dec 21, 2023

A few tips to get started.

First, review the tutorial here: https://github.com/unicode-org/icu4x/blob/main/docs/process/writing_a_new_data_struct.md

Specifically for the percent formatter: you can create a data struct in experimental/dimension/src/provider.rs, something like this:

#[icu_provider::data_struct(PercentEssentialsV1Marker = "percent/essentials@1")]
#[derive(Default, Clone, PartialEq, Debug)]
#[cfg_attr(
    feature = "datagen",
    derive(serde::Serialize, databake::Bake),
    databake(path = icu_dimension::provider),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct PercentEssentialsV1<'data> {
    /// Percent format with plus sign
    pub plus_pattern: Cow<'data, str>,
    /// Percent format with no sign
    pub zero_pattern: Cow<'data, str>,
    /// Percent format with minus sign
    pub minus_pattern: Cow<'data, str>,
    /// Index of the number placeholder within the plus pattern
    pub plus_index: u8,
    /// Index of the number placeholder within the no-sign pattern
    pub zero_index: u8,
    /// Index of the number placeholder within the minus pattern
    pub minus_index: u8,
}

We can make that a little bit more efficiently packed but I like to start with the clear and simple model.

Next, create provider/datagen/src/transform/cldr/decimal/percent.rs: this is where you will put your transform code. You need to have at least these impls:

impl DataProvider<PercentEssentialsV1Marker> for crate::DatagenProvider
impl IterableDataProviderInternal<PercentEssentialsV1Marker> for crate::DatagenProvider

You can see the other impls for inspiration.

Then, to plug it together, add your new data key in at least two places:

  1. provider/datagen/src/registry.rs
  2. pub const KEYS in experimental/dimension/src/provider.rs

With this, you should be able to run cargo make testdata at the project root and see the JSON data being generated. Once you get to this point, it is ready for a code review.

@sffc sffc added this to the 1.x Priority ⟨P2⟩ milestone Feb 29, 2024
@sffc sffc added the S-medium Size: Less than a week (larger bug fix or enhancement) label Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-numbers Component: Numbers, units, currencies S-medium Size: Less than a week (larger bug fix or enhancement) U-ecma402 User: ECMA-402 compatibility
Projects
None yet
Development

No branches or pull requests

1 participant