pub struct PluralRulesWithRanges<R> { /* private fields */ }
Expand description

A PluralRules that also has the ability to retrieve an appropriate Plural Category for a range.

Enabled with the experimental Cargo feature.

🚧 This code is experimental; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. Use with caution. #4140

Examples

use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralOperands};
use icu::plurals::{PluralRuleType, PluralRulesWithRanges};
use std::convert::TryFrom;

let ranges = PluralRulesWithRanges::try_new(
    &locale!("ar").into(),
    PluralRuleType::Cardinal,
)
.expect("locale should be present");

let operands = PluralOperands::from(1_usize);
let operands2: PluralOperands =
    "2.0".parse().expect("parsing to operands should succeed");

assert_eq!(
    ranges.category_for_range(operands, operands2),
    PluralCategory::Other
);

Implementations§

source§

impl PluralRulesWithRanges<PluralRules>

source

pub fn try_new( locale: &DataLocale, rule_type: PluralRuleType ) -> Result<Self, PluralsError>

Constructs a new PluralRulesWithRanges for a given locale using compiled data.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

Examples
use icu::locid::locale;
use icu::plurals::{PluralRuleType, PluralRulesWithRanges};

let _ = PluralRulesWithRanges::try_new(
    &locale!("en").into(),
    PluralRuleType::Cardinal,
).expect("locale should be present");

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale, rule_type: PluralRuleType ) -> Result<Self, PluralsError>

A version of Self::try_new that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), locale: &DataLocale, rule_type: PluralRuleType ) -> Result<Self, PluralsError>

A version of Self::try_new that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_unstable( provider: &(impl DataProvider<PluralRangesV1Marker> + DataProvider<CardinalV1Marker> + DataProvider<OrdinalV1Marker> + ?Sized), locale: &DataLocale, rule_type: PluralRuleType ) -> Result<Self, PluralsError>

A version of Self::try_new that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn try_new_cardinal(locale: &DataLocale) -> Result<Self, PluralsError>

Constructs a new PluralRulesWithRanges for a given locale for cardinal numbers using compiled data.

See PluralRules::try_new_cardinal for more information.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRulesWithRanges};

let rules = PluralRulesWithRanges::try_new_cardinal(&locale!("ru").into())
    .expect("locale should be present");

assert_eq!(rules.category_for_range(0_usize, 2_usize), PluralCategory::Few);

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_cardinal_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale ) -> Result<Self, PluralsError>

A version of Self::try_new_cardinal that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_cardinal_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), locale: &DataLocale ) -> Result<Self, PluralsError>

A version of Self::try_new_cardinal that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_cardinal_unstable( provider: &(impl DataProvider<CardinalV1Marker> + DataProvider<PluralRangesV1Marker> + ?Sized), locale: &DataLocale ) -> Result<Self, PluralsError>

A version of Self::try_new_cardinal that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn try_new_ordinal(locale: &DataLocale) -> Result<Self, PluralsError>

Constructs a new PluralRulesWithRanges for a given locale for ordinal numbers using compiled data.

See PluralRules::try_new_ordinal for more information.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRulesWithRanges};

let rules = PluralRulesWithRanges::try_new_ordinal(
    &locale!("ru").into(),
)
.expect("locale should be present");

assert_eq!(rules.category_for_range(0_usize, 2_usize), PluralCategory::Other);

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_ordinal_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale ) -> Result<Self, PluralsError>

A version of Self::try_new_ordinal that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_ordinal_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), locale: &DataLocale ) -> Result<Self, PluralsError>

A version of Self::try_new_ordinal that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_ordinal_unstable( provider: &(impl DataProvider<OrdinalV1Marker> + DataProvider<PluralRangesV1Marker> + ?Sized), locale: &DataLocale ) -> Result<Self, PluralsError>

A version of Self::try_new_ordinal that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source§

impl<R> PluralRulesWithRanges<R>where R: AsRef<PluralRules>,

source

pub fn try_new_with_rules( locale: &DataLocale, rules: R ) -> Result<Self, PluralsError>

Constructs a new PluralRulesWithRanges for a given locale from an existing PluralRules (either owned or as a reference) and compiled data.

⚠️ Warning

The provided locale MUST be the same as the locale provided to the constructor of rules. Otherwise, Self::category_for_range will return incorrect results.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

Examples
use icu::locid::locale;
use icu::plurals::{PluralRuleType, PluralRulesWithRanges, PluralRules};

let rules = PluralRules::try_new(&locale!("en").into(), PluralRuleType::Cardinal)
    .expect("locale should be present");

let _ =
    PluralRulesWithRanges::try_new_with_rules(&locale!("en").into(), rules)
        .expect("locale should be present");

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_with_rules_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale, rules: R ) -> Result<Self, PluralsError>

A version of Self::try_new_with_rules that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_with_rules_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), locale: &DataLocale, rules: R ) -> Result<Self, PluralsError>

A version of Self::try_new_with_rules that uses custom data provided by a BufferProvider.

Enabled with the serde feature.

📚 Help choosing a constructor

source

pub fn try_new_with_rules_unstable( provider: &(impl DataProvider<PluralRangesV1Marker> + ?Sized), locale: &DataLocale, rules: R ) -> Result<Self, PluralsError>

A version of Self::try_new_with_rules that uses custom data provided by a DataProvider.

📚 Help choosing a constructor

⚠️ The bounds on provider may change over time, including in SemVer minor releases.
source

pub fn rules(&self) -> &PluralRules

Gets a reference to the inner PluralRules.

Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRulesWithRanges};

let ranges = PluralRulesWithRanges::try_new_cardinal(&locale!("en").into())
    .expect("locale should be present");

let rules = ranges.rules();

assert_eq!(rules.category_for(1u8), PluralCategory::One);
source

pub fn category_for_range<S: Into<PluralOperands>, E: Into<PluralOperands>>( &self, start: S, end: E ) -> PluralCategory

Returns the Plural Category appropriate for a range.

Note that the returned category is correct only if the range fulfills the following requirements:

  • The start value is strictly less than the end value.
  • Both values are positive.
Examples
use icu::locid::locale;
use icu::plurals::{
    PluralCategory, PluralOperands, PluralRuleType, PluralRulesWithRanges,
};

let ranges = PluralRulesWithRanges::try_new(
    &locale!("ro").into(),
    PluralRuleType::Cardinal,
)
.expect("locale should be present");
let operands: PluralOperands =
    "0.5".parse().expect("parsing to operands should succeed");
let operands2 = PluralOperands::from(1_usize);

assert_eq!(
    ranges.category_for_range(operands, operands2),
    PluralCategory::Few
);
source

pub fn resolve_range( &self, start: PluralCategory, end: PluralCategory ) -> PluralCategory

Returns the Plural Category appropriate for a range from the categories of its endpoints.

Note that the returned category is correct only if the original numeric range fulfills the following requirements:

  • The start value is strictly less than the end value.
  • Both values are positive.
Examples
use icu::locid::locale;
use icu::plurals::{PluralCategory, PluralRuleType, PluralRulesWithRanges};

let ranges = PluralRulesWithRanges::try_new(
    &locale!("sl").into(),
    PluralRuleType::Ordinal,
)
.expect("locale should be present");

assert_eq!(
    ranges.resolve_range(PluralCategory::Other, PluralCategory::One),
    PluralCategory::Few
);

Trait Implementations§

source§

impl<R: Debug> Debug for PluralRulesWithRanges<R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for Twhere T: 'static,

source§

impl<T> MaybeSendSync for T