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

Add FixedDecimalFormat data provider plumbing #541

Merged
merged 22 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"experimental/bies",
"experimental/segmenter_lstm",
"components/datetime",
"components/decimal",
"components/ecma402",
"components/capi",
"components/icu",
Expand Down
1 change: 1 addition & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ args = [
"--",
"--cldr-core", "../../resources/testdata/data/cldr/cldr-core",
"--cldr-dates", "../../resources/testdata/data/cldr/cldr-dates-full",
"--cldr-numbers", "../../resources/testdata/data/cldr/cldr-numbers-full",
"--out", "../../resources/testdata/data/bincode",
"--all-keys",
"-s", "bincode",
Expand Down
33 changes: 33 additions & 0 deletions components/decimal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is part of ICU4X. For terms of use, please see the file
gregtatum marked this conversation as resolved.
Show resolved Hide resolved
# called LICENSE at the top level of the ICU4X source tree
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

[package]
name = "icu_decimal"
version = "0.1.0"
authors = ["The ICU4X Project Developers"]
edition = "2018"
repository = "https://github.com/unicode-org/icu4x"
license-file = "../../LICENSE"
categories = ["internationalization"]
include = [
"src/**/*",
"benches/**/*",
sffc marked this conversation as resolved.
Show resolved Hide resolved
"Cargo.toml",
"README.md"
]

[package.metadata.cargo-all-features]
skip_optional_dependencies = true

[dependencies]
smallstr = { version = "0.2", features = ["serde"] }
zbraniecki marked this conversation as resolved.
Show resolved Hide resolved
icu_locid = { version = "0.1", path = "../locid" }
icu_provider = { version = "0.1", path = "../provider" }
serde = { version = "1.0", features = ["derive"], optional = true }

[features]
default = ["provider_serde"]
bench = []
provider_serde = ["serde"]
serialize_none = []
12 changes: 12 additions & 0 deletions components/decimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
`icu_decimal` offers localized decimal number formatting.

It will eventually support:

- Plain decimal numbers
- Currencies
- Measurement units
- Compact notation

Currently, the crate is a work in progress with extremely limited functionality. To track progress, follow this issue:

https://github.com/unicode-org/icu4x/issues/275
sffc marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions components/decimal/src/lib.rs
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 ).

//! `icu_decimal` offers localized decimal number formatting.
//!
//! It will eventually support:
//!
//! - Plain decimal numbers
//! - Currencies
//! - Measurement units
//! - Compact notation
//!
//! Currently, the crate is a work in progress with extremely limited functionality. To track
//! progress, follow this issue:
//!
//! https://github.com/unicode-org/icu4x/issues/275

pub mod provider;
76 changes: 76 additions & 0 deletions components/decimal/src/provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 ).

gregtatum marked this conversation as resolved.
Show resolved Hide resolved
//! Data provider struct definitions for `icu_decimal`.
//!
//! Read more about data providers: [icu_provider]

pub type SmallString8 = smallstr::SmallString<[u8; 8]>;
sffc marked this conversation as resolved.
Show resolved Hide resolved

pub mod key {
//! Resource keys for `icu_decimal`.
use icu_provider::{resource_key, ResourceKey};

/// Resource key: symbols used for basic decimal formatting.
pub const SYMBOLS_V1: ResourceKey = resource_key!(decimal, "symbols", 1);
}

/// A collection of strings to affix to a decimal number.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct AffixesV1 {
/// String to prepend before the decimal number.
pub prefix: SmallString8,

/// String to append after the decimal number.
pub suffix: SmallString8,
}

/// A collection of settings expressing where to put grouping separators in a decimal number.
/// For example, `1,000,000` has two grouping separators, positioned along every 3 digits.
sffc marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct GroupingSizesV1 {
/// The size of the first (lowest-magnitude) group.
pub primary: u8,

/// The size of groups after the first group.
pub secondary: u8,

/// The minimum number of digits required before the first group. For example, if `primary=3`
/// and `min_grouping=2`, grouping separators will be present on 10,000 and above.
pub min_grouping: u8,
}

/// Symbols and metadata required for formatting a FixedDecimal.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct DecimalSymbolsV1 {
/// Prefix and suffix to apply when a negative sign is needed.
pub minus_sign_affixes: AffixesV1,
sffc marked this conversation as resolved.
Show resolved Hide resolved

/// Prefix and suffix to apply when a plus sign is needed.
pub plus_sign_affixes: AffixesV1,

/// Character used to separate the integer and fraction parts of the number.
pub decimal_separator: SmallString8,
gregtatum marked this conversation as resolved.
Show resolved Hide resolved

/// Character used to separate groups in the integer part of the number.
pub grouping_separator: SmallString8,

/// Settings used to determine where to place groups in the integer part of the number.
pub grouping_sizes: GroupingSizesV1,

/// Zero digit for the current numbering system.
pub zero_digit: char,
}
5 changes: 5 additions & 0 deletions components/provider/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum ResourceCategory {
Plurals,
Dates,
Uniset,
Decimal,
PrivateUse(TinyStr4),
}

Expand All @@ -34,6 +35,7 @@ impl ResourceCategory {
Self::Plurals => Cow::Borrowed("plurals"),
Self::Dates => Cow::Borrowed("dates"),
Self::Uniset => Cow::Borrowed("uniset"),
Self::Decimal => Cow::Borrowed("decimal"),
sffc marked this conversation as resolved.
Show resolved Hide resolved
Self::PrivateUse(id) => {
let mut result = String::from("x-");
result.push_str(id.as_str());
Expand Down Expand Up @@ -106,6 +108,9 @@ macro_rules! resource_key {
(uniset, $sub_category:literal, $version:tt) => {
$crate::resource_key!($crate::ResourceCategory::Uniset, $sub_category, $version)
};
(decimal, $sub_category:literal, $version:tt) => {
$crate::resource_key!($crate::ResourceCategory::Decimal, $sub_category, $version)
};
(x, $pu:literal, $sub_category:literal, $version:tt) => {
$crate::resource_key!(
$crate::ResourceCategory::PrivateUse($crate::internal::tinystr4!($pu)),
Expand Down
8 changes: 6 additions & 2 deletions components/provider_cldr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ icu_locid = { version = "0.1", path = "../locid" }
icu_plurals = { version = "0.1", path = "../plurals" }
icu_datetime = { version = "0.1", path = "../datetime" }
icu_locale_canonicalizer = { version = "0.1", path = "../locale_canonicalizer" }
icu_decimal = { version = "0.1", path = "../decimal" }
itertools = "0.10"
json = "0.12"
litemap = { version = "0.1.1", path = "../../utils/litemap" }
serde = { version = "1.0", features = ["derive"] }
serde-aux = "2.1.1"
sffc marked this conversation as resolved.
Show resolved Hide resolved
serde_json = "1.0"
serde-tuple-vec-map = "1.0"
tinystr = "0.4"
smallstr = { version = "0.2", features = ["serde"] }
smallvec = "1.4"
litemap = { version = "0.1.1", path = "../../utils/litemap" }
tinystr = { version = "0.4.3", features = ["serde"] }

# Dependencies for the download feature
urlencoding = { version = "1.1", optional = true }
Expand Down
26 changes: 22 additions & 4 deletions components/provider_cldr/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_locid::LanguageIdentifier;
use std::error;
use std::fmt;
use std::path::{Path, PathBuf};
Expand All @@ -14,6 +15,7 @@ use crate::download;
pub enum Error {
Io(std::io::Error, Option<PathBuf>),
Json(serde_json::error::Error, Option<PathBuf>),
Custom(String, Option<LanguageIdentifier>),
MissingSource(MissingSourceError),
#[cfg(feature = "download")]
Download(download::Error),
Expand Down Expand Up @@ -47,6 +49,22 @@ impl<P: AsRef<Path>> From<(serde_json::error::Error, P)> for Error {
}
}

/// To help with debugging, string errors should be paired with a locale.
/// If a locale is unavailable, create the error directly: `Error::Custom(err, None)`
impl<L: AsRef<LanguageIdentifier>> From<(String, L)> for Error {
fn from(pieces: (String, L)) -> Self {
Self::Custom(pieces.0, Some(pieces.1.as_ref().clone()))
}
}

/// To help with debugging, string errors should be paired with a locale.
/// If a locale is unavailable, create the error directly: `Error::Custom(err, None)`
impl<L: AsRef<LanguageIdentifier>> From<(&'static str, L)> for Error {
fn from(pieces: (&'static str, L)) -> Self {
Self::Custom(pieces.0.to_string(), Some(pieces.1.as_ref().clone()))
}
}

impl From<MissingSourceError> for Error {
fn from(err: MissingSourceError) -> Self {
Self::MissingSource(err)
Expand All @@ -68,10 +86,10 @@ impl fmt::Display for Error {
match self {
Self::Io(err, Some(path)) => write!(f, "{}: {:?}", err, path),
Self::Io(err, None) => err.fmt(f),
Self::Json(err, Some(filename)) => {
write!(f, "JSON parse error: {}: {:?}", err, filename)
}
Self::Json(err, None) => write!(f, "JSON parse error: {}", err),
Self::Json(err, Some(path)) => write!(f, "JSON error: {}: {:?}", err, path),
Self::Json(err, None) => write!(f, "JSON error: {}", err),
Self::Custom(s, Some(langid)) => write!(f, "{}: {:?}", s, langid),
Self::Custom(s, None) => write!(f, "{}", s),
Self::MissingSource(err) => err.fmt(f),
#[cfg(feature = "download")]
Self::Download(err) => err.fmt(f),
Expand Down
Loading