Struct icu_plurals::PluralRules[][src]

pub struct PluralRules { /* fields omitted */ }

A struct which provides an ability to retrieve an appropriate Plural Category for a given number.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType, PluralCategory};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("en");

let dp = InvariantDataProvider;

let pr = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal)
    .expect("Failed to construct a PluralRules struct.");

assert_eq!(pr.select(5_usize), PluralCategory::Other);

Implementations

impl PluralRules[src]

pub fn try_new<'d, D: DataProvider<'d, PluralRuleStringsV1<'d>> + ?Sized>(
    langid: LanguageIdentifier,
    data_provider: &D,
    type_: PluralRuleType
) -> Result<Self, PluralRulesError>
[src]

Constructs a new PluralRules for a given locale, type and data provider.

This constructor will fail if the Data Provider does not have the data.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("en");

let dp = InvariantDataProvider;

let _ = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal);

pub fn select<I: Into<PluralOperands>>(&self, input: I) -> PluralCategory[src]

Returns the Plural Category appropriate for the given number.

Examples

use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType, PluralCategory};
use icu_provider::inv::InvariantDataProvider;

let lid = langid!("en");

let dp = InvariantDataProvider;

let pr = PluralRules::try_new(lid, &dp, PluralRuleType::Cardinal)
    .expect("Failed to construct a PluralRules struct.");

match pr.select(1_usize) {
    PluralCategory::One => "One item",
    PluralCategory::Other => "Many items",
    _ => { unreachable!(); }
};

The method accepts any input that can be calculated into Plural Operands. All unsigned primitive number types can infallibly be converted so they can be used as an input.

For signed numbers and strings, Plural Operands implement TryFrom and FromStr, which should be used before passing the result to select().

Examples

use std::convert::TryFrom;
use icu::locid::macros::langid;
use icu::plurals::{PluralRules, PluralRuleType};
use icu::plurals::{PluralCategory, PluralOperands};
use icu_provider::inv::InvariantDataProvider;

let operands = PluralOperands::try_from(-5)
    .expect("Failed to parse to operands.");
let operands2: PluralOperands = "5.10".parse()
    .expect("Failed to parse to operands.");

assert_eq!(pr.select(operands), PluralCategory::Other);
assert_eq!(pr.select(operands2), PluralCategory::Other);

pub fn new_from_data(
    langid: LanguageIdentifier,
    data: &PluralRuleStringsV1<'_>
) -> Result<Self, PluralRulesError>
[src]

Lower-level constructor that allows constructing a PluralRules directly from data obtained from a provider.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.