1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
// 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\] APIs and Data Structures for Plural Rules
//!
//! A single Plural Rule is an expression which tests the value of [`PluralOperands`]
//! against a condition. If the condition is truthful, then the [`PluralCategory`]
//! to which the Rule is assigned should be used.
//!
//! <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. Use with caution.
//! <a href="https://github.com/unicode-org/icu4x/issues/1091">#1091</a>
//! </div>
//!
//! # Examples
//!
//! In this example we're going to examine the AST, parsing and resolving of a
//! set of English Cardinal Plural Rules.
//!
//! A CLDR JSON source contains the following entry:
//!
//! ```json
//! {
//! "one": "i = 1 and v = 0 @integer 1",
//! "other": " @integer 0, 2~16, 100, 1000, 10000, 100000, 1000000, … @decimal 0.0~1.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …"
//! }
//! ```
//!
//! When the user provides a number for which the [`PluralCategory`] is to be selected,
//! the system will examine a rule for each category in order, and stop on the first
//! category which matches.
//!
//! In our example, the user provided an input value `1`.
//! That value expanded into [`PluralOperands`] might look something like this, in its
//! internal representation of plural operand values, or something logically equivalent:
//!
//! ```text
//! PluralOperands {
//! i: 1,
//! v: 0,
//! w: 0,
//! f: 0,
//! t: 0,
//! c: 0,
//! };
//! ```
//!
//! Now, the system will parse the first rule, assigned to [`PluralCategory::One`], and
//! test if it matches.
//!
//! The value of the rule is:
//!
//! ```text
//! i = 1 and v = 0 @integer 1
//! ```
//!
//! The [`Rule`] contains a [`Condition`] `i = 1 and v = 0` and a [`Sample`] `@integer 1`.
//!
//! When parsed, the resulting [`AST`] will look like this:
//!
//! ```
//! use icu::plurals::rules::reference::ast::*;
//! use icu::plurals::rules::reference::parse_condition;
//!
//! let input = "i = 1 and v = 0 @integer 1";
//!
//! let ast = parse_condition(input.as_bytes()).expect("Parsing failed.");
//! assert_eq!(
//! ast,
//! Condition(vec![AndCondition(vec![
//! Relation {
//! expression: Expression {
//! operand: Operand::I,
//! modulus: None,
//! },
//! operator: Operator::Eq,
//! range_list: RangeList(vec![RangeListItem::Value(Value(1))])
//! },
//! Relation {
//! expression: Expression {
//! operand: Operand::V,
//! modulus: None,
//! },
//! operator: Operator::Eq,
//! range_list: RangeList(vec![RangeListItem::Value(Value(0))])
//! },
//! ]),])
//! );
//! ```
//!
//! Finally, we can pass this [`AST`] (in fact, just the [`Condition`] node),
//! to a resolver alongside the [`PluralOperands`] to test if the Rule
//! matches:
//!
//! ```
//! use icu::plurals::rules::reference::parse_condition;
//! use icu::plurals::rules::reference::test_condition;
//! use icu::plurals::PluralOperands;
//!
//! let input = "i = 1 and v = 0 @integer 1";
//!
//! let operands = PluralOperands::from(1_u32);
//!
//! let ast = parse_condition(input.as_bytes()).expect("Parsing failed.");
//!
//! assert!(test_condition(&ast, &operands));
//! ```
//!
//! Since the rule for [`PluralCategory::One`] matches, we will return this category.
//! Otherwise, we'd test the next rule, in this case [`PluralCategory::Other`], which has an
//! empty [`Condition`], meaning that it'll match all operands.
//!
//! # Summary
//!
//! For [`PluralRuleType::Cardinal`] in English, we can restate the rule's logic as:
//!
//! When the `PluralOperands::i` is `1` and `PluralOperands::v` is `0` (or equivalent thereof), [`PluralCategory::One`]
//! should be used, otherwise [`PluralCategory::Other`] should be used.
//!
//! For other locales, there are different/more [`PluralCategories`] defined in the `PluralRules` (see [`PluralRules::categories`]),
//! and possibly more complicated [`Rules`] therein.
//!
//! # Difference between Category and Number
//!
//! While in English [`PluralCategory::One`] overlaps with an integer value `1`,
//! in other languages, the category may be used for other numbers as well.
//!
//! For example, in Russian [`PluralCategory::One`] matches numbers such as `11`, `21`, `121` etc.
//!
//! # Runtime vs. Resolver Rules
//!
//! `ICU4X` provides two sets of rules AST and APIs to manage it:
//! * `reference` is the canonical implementation of the specification intended for
//! tooling and testing to use.
//! This module provides APIs for parsing, editing and serialization
//! of the rules.
//! * `runtime` is a non-public, non-mutable runtime version optimized for
//! performance and low memory overhead. This version provides
//! runtime resolver used by the `PluralRules` itself.
//!
//! [`PluralCategory`]: super::PluralCategory
//! [`PluralCategories`]: super::PluralCategory
//! [`PluralCategory::One`]: super::PluralCategory::One
//! [`PluralCategory::Other`]: super::PluralCategory::Other
//! [`PluralOperands`]: super::PluralOperands
//! [`PluralRules::categories`]: super::PluralRules::categories
//! [`PluralRuleType::Cardinal`]: super::PluralRuleType::Cardinal
//! [`Rule`]: super::rules::reference::ast::Rule
//! [`Rules`]: super::rules::reference::ast::Rule
//! [`Condition`]: super::rules::reference::ast::Condition
//! [`Sample`]: super::rules::reference::ast::Samples
//! [`AST`]: super::rules::reference::ast
#[doc(hidden)]
pub mod reference;
// Need to expose it for `icu_datagen` use, but we don't
// have a reason to make it fully public, so hiding docs for now.
#[cfg(feature = "experimental")]
mod raw_operands;
#[doc(hidden)]
pub mod runtime;
#[cfg(feature = "experimental")]
pub use raw_operands::RawPluralOperands;