Skip to content

Commit e43b29c

Browse files
committed
plurals wip
1 parent 64c8173 commit e43b29c

File tree

8 files changed

+428
-127
lines changed

8 files changed

+428
-127
lines changed

components/plurals/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ categories = ["internationalization"]
1717
# Keep this in sync with other crates unless there are exceptions
1818
include = [
1919
"src/**/*",
20+
"data/macros.rs",
2021
"examples/**/*",
2122
"benches/**/*",
2223
"tests/**/*",
@@ -53,6 +54,7 @@ serde = ["dep:serde", "zerovec/serde", "icu_locid/serde", "icu_provider/serde"]
5354
datagen = ["serde", "zerovec/databake", "dep:databake"]
5455
experimental = []
5556
bench = ["serde"]
57+
data = []
5658

5759
[lib]
5860
path = "src/lib.rs"

components/plurals/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
fn main() {
6+
if std::env::var("CARGO_FEATURE_DATA").is_ok() {
7+
match std::env::var("ICU4X_DATA_DIR") {
8+
Ok(path) => println!("cargo:rustc-env=MACROS_RS={}/macros.rs", path),
9+
Err(_) => println!("cargo:rustc-env=MACROS_RS=../data/macros.rs"),
10+
}
11+
println!("cargo:rerun-if-env-changed=ICU4X_DATA_DIR");
12+
}
13+
}

components/plurals/data/any.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @generated
2+
impl_any_provider ! (BakedDataProvider) ;

components/plurals/data/macros.rs

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

components/plurals/data/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @generated
2+
include ! ("macros.rs") ; # [clippy :: msrv = "1.61"] pub struct BakedDataProvider ; impl_data_provider ! (BakedDataProvider) ;

components/plurals/src/lib.rs

Lines changed: 104 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ mod operands;
8484
pub mod provider;
8585
pub mod rules;
8686

87+
#[doc(hidden)]
88+
#[cfg(feature = "data")]
89+
pub mod data {
90+
include!(core::env!("MACROS_RS"));
91+
92+
use crate as icu_plurals;
93+
pub(crate) struct Provider;
94+
impl_plurals_ordinal_v1!(Provider);
95+
impl_plurals_cardinal_v1!(Provider);
96+
}
97+
8798
use core::cmp::{Ord, PartialOrd};
8899
pub use error::PluralsError;
89100
use icu_provider::prelude::*;
@@ -289,30 +300,30 @@ impl PluralCategory {
289300
pub struct PluralRules(DataPayload<ErasedPluralRulesV1Marker>);
290301

291302
impl PluralRules {
292-
/// Constructs a new `PluralRules` for a given locale, [`type`] and [`data provider`].
293-
///
294-
/// This constructor will fail if the [`Data Provider`] does not have the data.
295-
///
296-
/// [📚 Help choosing a constructor](icu_provider::constructors)
297-
/// <div class="stab unstable">
298-
/// ⚠️ The bounds on this function may change over time, including in SemVer minor releases.
299-
/// </div>
300-
///
301-
/// # Examples
302-
///
303-
/// ```
304-
/// use icu::locid::locale;
305-
/// use icu::plurals::{PluralRuleType, PluralRules};
306-
///
307-
/// let _ = PluralRules::try_new_unstable(
308-
/// &icu_testdata::unstable(),
309-
/// &locale!("en").into(),
310-
/// PluralRuleType::Cardinal,
311-
/// );
312-
/// ```
313-
///
314-
/// [`type`]: PluralRuleType
315-
/// [`data provider`]: icu_provider
303+
304+
icu_provider::gen_any_buffer_data_constructors!(
305+
locale: include,
306+
rule_type: PluralRuleType,
307+
error: PluralsError,
308+
/// Constructs a new `PluralRules` for a given locale, [`type`] and [`data provider`].
309+
///
310+
/// # Examples
311+
///
312+
/// ```
313+
/// use icu::locid::locale;
314+
/// use icu::plurals::{PluralRuleType, PluralRules};
315+
///
316+
/// let _ = PluralRules::try_new(
317+
/// &locale!("en").into(),
318+
/// PluralRuleType::Cardinal,
319+
/// );
320+
/// ```
321+
///
322+
/// [`type`]: PluralRuleType
323+
/// [`data provider`]: icu_provider
324+
);
325+
326+
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
316327
pub fn try_new_unstable<D>(
317328
data_provider: &D,
318329
locale: &DataLocale,
@@ -327,44 +338,42 @@ impl PluralRules {
327338
}
328339
}
329340

330-
icu_provider::gen_any_buffer_constructors!(
341+
icu_provider::gen_any_buffer_data_constructors!(
331342
locale: include,
332-
rule_type: PluralRuleType,
333-
error: PluralsError
343+
options: skip,
344+
error: PluralsError,
345+
/// Constructs a new `PluralRules` for a given locale for cardinal numbers.
346+
///
347+
/// Cardinal plural forms express quantities of units such as time, currency or distance,
348+
/// used in conjunction with a number expressed in decimal digits (i.e. "2", not "two").
349+
///
350+
/// For example, English has two forms for cardinals:
351+
///
352+
/// * [`One`]: `1 day`
353+
/// * [`Other`]: `0 days`, `2 days`, `10 days`, `0.3 days`
354+
///
355+
/// # Examples
356+
///
357+
/// ```
358+
/// use icu::locid::locale;
359+
/// use icu::plurals::{PluralCategory, PluralRules};
360+
///
361+
/// let rules = PluralRules::try_new_cardinal(&locale!("ru").into()).expect("Data should be present");
362+
///
363+
/// assert_eq!(rules.category_for(2_usize), PluralCategory::Few);
364+
/// ```
365+
///
366+
/// [`One`]: PluralCategory::One
367+
/// [`Other`]: PluralCategory::Other
368+
functions: [
369+
Self::try_new_cardinal_unstable,
370+
try_new_cardinal_with_any_provider,
371+
try_new_cardinal_with_buffer_provider,
372+
try_new_cardinal,
373+
]
334374
);
335375

336-
/// Constructs a new `PluralRules` for a given locale for cardinal numbers.
337-
///
338-
/// Cardinal plural forms express quantities of units such as time, currency or distance,
339-
/// used in conjunction with a number expressed in decimal digits (i.e. "2", not "two").
340-
///
341-
/// For example, English has two forms for cardinals:
342-
///
343-
/// * [`One`]: `1 day`
344-
/// * [`Other`]: `0 days`, `2 days`, `10 days`, `0.3 days`
345-
///
346-
/// [📚 Help choosing a constructor](icu_provider::constructors)
347-
/// <div class="stab unstable">
348-
/// ⚠️ The bounds on this function may change over time, including in SemVer minor releases.
349-
/// </div>
350-
///
351-
/// # Examples
352-
///
353-
/// ```
354-
/// use icu::locid::locale;
355-
/// use icu::plurals::{PluralCategory, PluralRules};
356-
///
357-
/// let rules = PluralRules::try_new_cardinal_unstable(
358-
/// &icu_testdata::unstable(),
359-
/// &locale!("ru").into(),
360-
/// )
361-
/// .expect("Data should be present");
362-
///
363-
/// assert_eq!(rules.category_for(2_usize), PluralCategory::Few);
364-
/// ```
365-
///
366-
/// [`One`]: PluralCategory::One
367-
/// [`Other`]: PluralCategory::Other
376+
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new_cardinal)]
368377
pub fn try_new_cardinal_unstable<D>(
369378
data_provider: &D,
370379
locale: &DataLocale,
@@ -383,52 +392,49 @@ impl PluralRules {
383392
))
384393
}
385394

386-
icu_provider::gen_any_buffer_constructors!(
395+
icu_provider::gen_any_buffer_data_constructors!(
387396
locale: include,
388397
options: skip,
389398
error: PluralsError,
399+
/// Constructs a new `PluralRules` for a given locale for ordinal numbers.
400+
///
401+
/// Ordinal plural forms denote the order of items in a set and are always integers.
402+
///
403+
/// For example, English has four forms for ordinals:
404+
///
405+
/// * [`One`]: `1st floor`, `21st floor`, `101st floor`
406+
/// * [`Two`]: `2nd floor`, `22nd floor`, `102nd floor`
407+
/// * [`Few`]: `3rd floor`, `23rd floor`, `103rd floor`
408+
/// * [`Other`]: `4th floor`, `11th floor`, `96th floor`
409+
///
410+
/// # Examples
411+
///
412+
/// ```
413+
/// use icu::locid::locale;
414+
/// use icu::plurals::{PluralCategory, PluralRules};
415+
///
416+
/// let rules = PluralRules::try_new_ordinal_unstable(
417+
/// &icu_testdata::unstable(),
418+
/// &locale!("ru").into(),
419+
/// )
420+
/// .expect("Data should be present");
421+
///
422+
/// assert_eq!(rules.category_for(2_usize), PluralCategory::Other);
423+
/// ```
424+
///
425+
/// [`One`]: PluralCategory::One
426+
/// [`Two`]: PluralCategory::Two
427+
/// [`Few`]: PluralCategory::Few
428+
/// [`Other`]: PluralCategory::Other
390429
functions: [
391-
Self::try_new_cardinal_unstable,
392-
try_new_cardinal_with_any_provider,
393-
try_new_cardinal_with_buffer_provider
430+
Self::try_new_ordinal_unstable,
431+
try_new_ordinal_with_any_provider,
432+
try_new_ordinal_with_buffer_provider,
433+
try_new_ordinal,
394434
]
395435
);
396436

397-
/// Constructs a new `PluralRules` for a given locale for ordinal numbers.
398-
///
399-
/// Ordinal plural forms denote the order of items in a set and are always integers.
400-
///
401-
/// For example, English has four forms for ordinals:
402-
///
403-
/// * [`One`]: `1st floor`, `21st floor`, `101st floor`
404-
/// * [`Two`]: `2nd floor`, `22nd floor`, `102nd floor`
405-
/// * [`Few`]: `3rd floor`, `23rd floor`, `103rd floor`
406-
/// * [`Other`]: `4th floor`, `11th floor`, `96th floor`
407-
///
408-
/// [📚 Help choosing a constructor](icu_provider::constructors)
409-
/// <div class="stab unstable">
410-
/// ⚠️ The bounds on this function may change over time, including in SemVer minor releases.
411-
/// </div>
412-
///
413-
/// # Examples
414-
///
415-
/// ```
416-
/// use icu::locid::locale;
417-
/// use icu::plurals::{PluralCategory, PluralRules};
418-
///
419-
/// let rules = PluralRules::try_new_ordinal_unstable(
420-
/// &icu_testdata::unstable(),
421-
/// &locale!("ru").into(),
422-
/// )
423-
/// .expect("Data should be present");
424-
///
425-
/// assert_eq!(rules.category_for(2_usize), PluralCategory::Other);
426-
/// ```
427-
///
428-
/// [`One`]: PluralCategory::One
429-
/// [`Two`]: PluralCategory::Two
430-
/// [`Few`]: PluralCategory::Few
431-
/// [`Other`]: PluralCategory::Other
437+
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new_ordinal)]
432438
pub fn try_new_ordinal_unstable<D>(
433439
data_provider: &D,
434440
locale: &DataLocale,
@@ -447,17 +453,6 @@ impl PluralRules {
447453
))
448454
}
449455

450-
icu_provider::gen_any_buffer_constructors!(
451-
locale: include,
452-
options: skip,
453-
error: PluralsError,
454-
functions: [
455-
Self::try_new_ordinal_unstable,
456-
try_new_ordinal_with_any_provider,
457-
try_new_ordinal_with_buffer_provider
458-
]
459-
);
460-
461456
/// Returns the [`Plural Category`] appropriate for the given number.
462457
///
463458
/// # Examples

0 commit comments

Comments
 (0)