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

Data and provider for compact decimal formatting #2883

Merged
merged 63 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
0f2b754
maybe I should commit sometimes?
eggrobin Dec 7, 2022
1debe59
also the new files
eggrobin Dec 7, 2022
f1760a1
I forgot my laptop charger
eggrobin Dec 7, 2022
d5657bf
it appears to compile
eggrobin Dec 8, 2022
8b0f103
meow
eggrobin Dec 9, 2022
4d9f95f
plate the boiler
eggrobin Dec 9, 2022
43fe38e
a test that disturbingly passes
eggrobin Dec 9, 2022
aa6dc5c
I should probably have pushed that
eggrobin Dec 12, 2022
507e234
some refactoring and &&i8
eggrobin Dec 12, 2022
a5b63f6
logic is hard
eggrobin Dec 12, 2022
a0db0c9
long
eggrobin Dec 12, 2022
12bebe4
simpler end-to-end test
eggrobin Dec 13, 2022
7e60fe1
a test
eggrobin Dec 13, 2022
0c25367
more tests
eggrobin Dec 13, 2022
5f5db46
another test
eggrobin Dec 13, 2022
3dfd5bf
fix some warnings
eggrobin Dec 13, 2022
9537951
wrong warning is wrong
eggrobin Dec 13, 2022
b9f1405
use
eggrobin Dec 13, 2022
9ff37a2
the licensing will continue until morale improve
eggrobin Dec 13, 2022
22097a2
cargo dottore
eggrobin Dec 13, 2022
09cdad5
test all the errors
eggrobin Dec 13, 2022
addb602
readthee
eggrobin Dec 13, 2022
b9309e7
two
eggrobin Dec 13, 2022
21a35cf
appease clippy
eggrobin Dec 13, 2022
7373756
lints &c
eggrobin Dec 13, 2022
1c17a74
cargo make testdata
eggrobin Dec 13, 2022
e9c7bb1
logic remains hard
eggrobin Dec 14, 2022
89c14cb
cargo make testdata again
eggrobin Dec 14, 2022
a980ac0
feature propagation
eggrobin Dec 14, 2022
5265ca9
I am shocked, shocked, to find that escaping errors are going on in here
eggrobin Dec 14, 2022
0f3ce45
shuffle attributes between features
eggrobin Dec 14, 2022
dd8616c
Allow noncompact CompactDecimal
eggrobin Dec 14, 2022
0b693bc
remove stray assertions
eggrobin Dec 14, 2022
8bbc3b2
noun
eggrobin Dec 14, 2022
7127e5f
Update utils/fixed_decimal/src/compact.rs
eggrobin Dec 14, 2022
193e9b3
Update utils/fixed_decimal/src/compact.rs
eggrobin Dec 14, 2022
03afa3e
Update utils/fixed_decimal/src/compact.rs
eggrobin Dec 14, 2022
8285b0a
dottore
eggrobin Dec 14, 2022
b09a5e3
some more comments
eggrobin Dec 15, 2022
8d9f94d
refactor
eggrobin Dec 15, 2022
4a39f6b
use superstar
eggrobin Dec 15, 2022
5fbd7c1
zero
eggrobin Dec 15, 2022
4c92089
factor out something
eggrobin Dec 15, 2022
35d0180
comments
eggrobin Dec 15, 2022
2b55276
fmt
eggrobin Dec 15, 2022
64ca7a8
𒀀𒈾​𒎙𒎙​𒆠𒉈𒈠​𒌝𒈠​𒉭𒈠​𒆠𒀀𒄠​𒉈𒁉𒋳
eggrobin Dec 15, 2022
5b44901
cargo made testdata
eggrobin Dec 15, 2022
31d35b3
cargo make diplomat-coverage
eggrobin Dec 15, 2022
ad61326
mergez la vache
eggrobin Dec 15, 2022
c6e3d9d
cargo testdata gemacht
eggrobin Dec 15, 2022
3c6aa71
license
eggrobin Dec 15, 2022
3e6086b
fix test
eggrobin Dec 15, 2022
5b243bd
stray cfg
eggrobin Dec 15, 2022
4305c73
appease clippy
eggrobin Dec 16, 2022
ddb3bae
Merge branch 'noncompact-compact' into compact-decimal-formatting
eggrobin Dec 16, 2022
60b9843
Merge remote-tracking branch 'la-vache/main' into compact-decimal-for…
eggrobin Dec 16, 2022
e94c2ce
Feels like YTI18N
eggrobin Dec 16, 2022
e16100a
fmt
eggrobin Dec 16, 2022
9d95dae
clippy
eggrobin Dec 16, 2022
41d817f
maybe we needed that one after all
eggrobin Dec 16, 2022
b4336aa
Merge remote-tracking branch 'la-vache/main' into compact-decimal-for…
eggrobin Dec 16, 2022
0761f89
no into
eggrobin Dec 16, 2022
061953d
reinstate into_iter1
eggrobin Dec 16, 2022
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
18 changes: 18 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 @@ -21,6 +21,7 @@ members = [
"components/timezone",
"experimental/bies",
"experimental/casemapping",
"experimental/compactdecimal",
"experimental/displaynames",
"experimental/ixdtf",
"experimental/relativetime",
Expand Down
44 changes: 44 additions & 0 deletions experimental/compactdecimal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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 ).

[package]
name = "icu_compactdecimal"
version = "0.1.0"
authors = ["The ICU4X Project Developers"]
edition = "2018"
repository = "https://github.com/unicode-org/icu4x"
license-file = "LICENSE"
categories = ["internationalization"]
# Keep this in sync with other crates unless there are exceptions
include = [
"data/*",
"src/**/*",
"examples/**/*",
"benches/**/*",
"tests/**/*",
"Cargo.toml",
"LICENSE",
"README.md"
]

[dependencies]
displaydoc = { version = "0.2.3", default-features = false }
icu_decimal = { version = "1.0.0", path = "../../components/decimal" }
icu_provider = { version = "1.0.0", path = "../../provider/core", features = ["macros"] }
icu_plurals = { version = "1.0.0", path = "../../components/plurals" }
zerovec = { version = "0.9", path = "../../utils/zerovec", features = ["yoke"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true }
writeable = { version = "0.5", path = "../../utils/writeable" }
fixed_decimal = { version = "0.5", path = "../../utils/fixed_decimal" }
databake = { version = "0.1.0", path = "../../utils/databake", optional = true, features = ["derive"]}

[dev-dependencies]
icu_testdata = { path = "../../provider/testdata", default-features = false, features = ["icu_plurals", "icu_decimal"] }
icu_locid = { version = "1.0.0", path = "../../components/locid" }

[features]
default = []
std = ["icu_provider/std"]
serde = ["dep:serde", "zerovec/serde", "icu_decimal/serde", "icu_plurals/serde"]
datagen = ["std", "serde", "dep:databake", "zerovec/databake"]
51 changes: 51 additions & 0 deletions experimental/compactdecimal/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE

See Terms of Use <https://www.unicode.org/copyright.html>
for definitions of Unicode Inc.’s Data Files and Software.

NOTICE TO USER: Carefully read the following legal agreement.
BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S
DATA FILES ("DATA FILES"), AND/OR SOFTWARE ("SOFTWARE"),
YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
TERMS AND CONDITIONS OF THIS AGREEMENT.
IF YOU DO NOT AGREE, DO NOT DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE
THE DATA FILES OR SOFTWARE.

COPYRIGHT AND PERMISSION NOTICE

Copyright © 1991-2022 Unicode, Inc. All rights reserved.
Distributed under the Terms of Use in https://www.unicode.org/copyright.html.

Permission is hereby granted, free of charge, to any person obtaining
a copy of the Unicode data files and any associated documentation
(the "Data Files") or Unicode software and any associated documentation
(the "Software") to deal in the Data Files or Software
without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, and/or sell copies of
the Data Files or Software, and to permit persons to whom the Data Files
or Software are furnished to do so, provided that either
(a) this copyright and permission notice appear with all copies
of the Data Files or Software, or
(b) this copyright and permission notice appear in associated
Documentation.

THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT OF THIRD PARTY RIGHTS.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THE DATA FILES OR SOFTWARE.

Except as contained in this notice, the name of a copyright holder
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in these Data Files or Software without prior
written authorization of the copyright holder.


Portions of ICU4X may have been adapted from ICU4C and/or ICU4J.
ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others.
17 changes: 17 additions & 0 deletions experimental/compactdecimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# icu_compactdecimal [![crates.io](https://img.shields.io/crates/v/icu_compactdecimal)](https://crates.io/crates/icu_compactdecimal)

🚧 \[Experimental\] Relative time formatting

This module is published as its own crate ([`icu_compactdecimal`](https://docs.rs/icu_compactdecimal/latest/icu_compactdecimal/))
and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.

<div class="stab unstable">
🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways,
including in SemVer minor releases. It can be enabled with the "experimental" feature
of the icu meta-crate. Use with caution.
</div>


## More Information

For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x).
41 changes: 41 additions & 0 deletions experimental/compactdecimal/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 ).

use displaydoc::Display;
use icu_decimal::DecimalError;
use icu_plurals::PluralsError;
use icu_provider::DataError;

/// A list of error outcomes for various operations in the `icu_compactdecimal` crate.
#[derive(Display, Debug, Copy, Clone, PartialEq)]
#[non_exhaustive]
pub enum CompactDecimalError {
/// An error originating from [`PluralRules`](icu_plurals::PluralRules).
#[displaydoc("Error loading plural rules: {0}")]
PluralRules(PluralsError),
/// An error originating from [`DataProvider`](icu_provider::DataProvider).
#[displaydoc("Error loading data: {0}")]
Data(DataError),
/// An error originating from [`FixedDecimalFormatter`](icu_decimal::FixedDecimalFormatter).
#[displaydoc("Error loading FixedDecimalFormatter: {0}")]
Decimal(DecimalError),
}

impl From<PluralsError> for CompactDecimalError {
fn from(e: PluralsError) -> Self {
CompactDecimalError::PluralRules(e)
}
}

impl From<DataError> for CompactDecimalError {
fn from(e: DataError) -> Self {
CompactDecimalError::Data(e)
}
}

impl From<DecimalError> for CompactDecimalError {
fn from(e: DecimalError) -> Self {
CompactDecimalError::Decimal(e)
}
}
38 changes: 38 additions & 0 deletions experimental/compactdecimal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 ).

//! 🚧 \[Experimental\] Relative time formatting
//!
//! This module is published as its own crate ([`icu_compactdecimal`](https://docs.rs/icu_compactdecimal/latest/icu_compactdecimal/))
//! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
//!
//! <div class="stab unstable">
//! 🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways,
//! including in SemVer minor releases. It can be enabled with the "experimental" feature
//! of the icu meta-crate. Use with caution.
//! </div>
//!

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![cfg_attr(not(test), no_std)]
#![cfg_attr(
not(test),
deny(
clippy::indexing_slicing,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::exhaustive_structs,
clippy::exhaustive_enums,
missing_debug_implementations,
)
)]
#![warn(missing_docs)]
eggrobin marked this conversation as resolved.
Show resolved Hide resolved

extern crate alloc;

mod error;
pub mod provider;

pub use error::CompactDecimalError;
127 changes: 127 additions & 0 deletions experimental/compactdecimal/src/provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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 ).

// Provider structs must be stable.
#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
// Suppress a warning on zerovec::makevarule.
#![allow(missing_docs)]

//! Data provider struct definitions for this ICU4X component.
//!
//! Read more about data providers: [`icu_provider`]

use alloc::borrow::Cow;
use icu_provider::{yoke, zerofrom};
use zerovec::ZeroMap2d;

/// Relative time format V1 data struct.
/// As in CLDR, this is a mapping from type (a power of ten, corresponding to
/// the magnitude of the number being formatted) and count (a plural case or an
/// explicit 1) to a pattern.
///
/// However, plural cases that are identical to the other case are omitted, thus
/// given
/// > (1000, one) ↦ 0K, (1000, other) ↦ 0K
///
/// only
/// > (1000, other) ↦ 0K
///
/// is stored.
///
/// Further, if all plural cases are compatible across consecutive types, the
/// larger types are omitted, thus given
/// > (1000, other) ↦ 0K, (10000, other) ↦ 00K, (100000, other) ↦ 000K
///
/// only
/// > (1000, other) ↦ 0K
///
/// is stored.
///
/// Finally, the pattern indicating noncompact notation for the first few powers
/// of ten is omitted; that is, there is an implict (1, other) ↦ 0.
#[icu_provider::data_struct(
LongCompactDecimalFormatDataV1Marker = "compactdecimal/long@1",
ShortCompactDecimalFormatDataV1Marker = "compactdecimal/short@1"
)]
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_compactdecimal::provider)
)]
#[yoke(prove_covariance_manually)]
pub struct CompactDecimalPatternDataV1<'data> {
eggrobin marked this conversation as resolved.
Show resolved Hide resolved
/// A map keyed on log10 of the CLDR `type` attribute and the CLDR `count` attribute.
#[cfg_attr(feature = "serde", serde(borrow))]
pub patterns: ZeroMap2d<'data, i8, Count, PatternULE>,
}

/// A CLDR plural keyword, or the explicit value 1.
/// See <https://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules>.
#[zerovec::make_ule(CountULE)]
#[zerovec::derive(Debug)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_compactdecimal::provider)
)]
#[repr(u8)]
pub enum Count {
Copy link
Member

@younies younies Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enum should is representing the general Count for any plural rules, for example in currency or units, we will need Count too.

Therefore, I think Count implementation should be in some central place that we can use it, instead of writing this implementation for each component.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah; for now let's duplicate it but we should figure out a common solution. I don't want to over-engineer this given that we're waiting on icu_pattern which is intended to be the common solution (@zbraniecki)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is pattern related to this enum?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enum is input into the pattern selection, which I imagine may influence the design of the enum

/// The CLDR keyword `zero`.
Zero = 0,
/// The CLDR keyword `one`.
One = 1,
/// The CLDR keyword `two`.
Two = 2,
/// The CLDR keyword `few`.
Few = 3,
/// The CLDR keyword `many`.
Many = 4,
/// The CLDR keyword `other`.
Other = 5,
/// The explicit 1 case, see <https://www.unicode.org/reports/tr35/tr35-numbers.html#Explicit_0_1_rules>.
Explicit1 = 6,
eggrobin marked this conversation as resolved.
Show resolved Hide resolved
// NOTE(egg): No explicit 0, because the compact decimal pattern selection
younies marked this conversation as resolved.
Show resolved Hide resolved
// algorithm does not allow such a thing to arise.
}

/// A compact decimal pattern, representing some literal text with an optional
/// placeholder, and the power of 10 expressed by the text.
#[derive(
Debug, Clone, Default, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom, Ord, PartialOrd, Eq,
)]
#[zerovec::make_varule(PatternULE)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_compactdecimal::provider),
zerovec::derive(Serialize),
)]
#[zerovec::derive(Debug)]
#[cfg_attr(feature = "serde", zerovec::derive(Deserialize))]
pub struct Pattern<'data> {
/// The compact decimal exponent, e.g., 6 for "million".
/// The value 0 indicates that compact notation is not used; in that case,
/// literal text must be empty; this corresponds to the CLDR pattern "0".
/// This is derived from the numbers of 0s in the pattern and the associated
eggrobin marked this conversation as resolved.
Show resolved Hide resolved
/// `type` attribute; it is a more convenient representation than the number
/// of 0s, because it is often common to multiple types; for instance, the
/// following correspond to the same [`Pattern`]:
/// <pattern type="1000000" count="other">0 M</pattern>
/// <pattern type="10000000" count="other">00 M</pattern>
pub exponent: i8,
/// The index in literal_text before which the placeholder is inserted;
/// this is 0 for insertion at the beginning, which is most common.
/// The value 255 indicates that the pattern does not have a placeholder,
/// as in French "mille" for 1000.
pub index: u8,
#[cfg_attr(feature = "serde", serde(borrow))]
/// The underlying CLDR pattern with the placeholder removed, e.g.,
/// " M" for the pattern "000 M"
pub literal_text: Cow<'data, str>,
}
3 changes: 2 additions & 1 deletion provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ icu_casemapping = { version = "0.7.0", path = "../../experimental/casemapping",
icu_segmenter = { version = "0.7.0", path = "../../experimental/segmenter", features = ["datagen", "lstm"], optional = true }
icu_displaynames = { version = "0.7.0", path = "../../experimental/displaynames", features = ["datagen"], optional = true }
icu_relativetime = { version = "0.1.0", path = "../../experimental/relativetime", features = ["datagen"], optional = true }
icu_compactdecimal = { version = "0.1.0", path = "../../experimental/compactdecimal", features = ["datagen"], optional = true }

# ICU provider infrastructure
icu_provider = { version = "1.0.0", path = "../core", features = ["std", "log_error_context", "datagen"]}
Expand Down Expand Up @@ -96,7 +97,7 @@ icu = { path = "../../components/icu" }

[features]
default = []
experimental = ["icu_casemapping", "icu_segmenter", "icu_datetime/experimental", "icu_displaynames", "icu_relativetime"]
experimental = ["icu_casemapping", "icu_segmenter", "icu_datetime/experimental", "icu_displaynames", "icu_relativetime", "icu_compactdecimal"]
bin = ["clap", "eyre", "simple_logger"]

[[bin]]
Expand Down
4 changes: 4 additions & 0 deletions provider/datagen/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use icu_timezone::provider::*;
#[cfg(feature = "experimental")]
use icu_casemapping::provider::*;
#[cfg(feature = "experimental")]
use icu_compactdecimal::provider::*;
#[cfg(feature = "experimental")]
use icu_displaynames::provider::*;
#[cfg(feature = "experimental")]
use icu_relativetime::provider::*;
Expand Down Expand Up @@ -252,6 +254,8 @@ registry!(
LongYearRelativeTimeFormatDataV1Marker,
ShortYearRelativeTimeFormatDataV1Marker,
NarrowYearRelativeTimeFormatDataV1Marker,
LongCompactDecimalFormatDataV1Marker,
eggrobin marked this conversation as resolved.
Show resolved Hide resolved
ShortCompactDecimalFormatDataV1Marker,
}
);

Expand Down
Loading