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 #[diplomat::bridge] 6 #[diplomat::abi_rename = "icu4x_{0}_mv1"] 7 #[diplomat::attr(auto, namespace = "icu4x")] 8 pub mod ffi { 9 use alloc::boxed::Box; 10 11 #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))] 12 use crate::errors::ffi::DataError; 13 use crate::errors::ffi::FixedDecimalParseError; 14 #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))] 15 use crate::locale_core::ffi::Locale; 16 #[cfg(feature = "buffer_provider")] 17 use crate::provider::ffi::DataProvider; 18 19 #[diplomat::rust_link(icu::plurals::PluralCategory, Enum)] 20 #[diplomat::enum_convert(icu_plurals::PluralCategory)] 21 pub enum PluralCategory { 22 Zero, 23 One, 24 Two, 25 Few, 26 Many, 27 Other, 28 } 29 30 impl PluralCategory { 31 /// Construct from a string in the format 32 /// [specified in TR35](https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules) 33 #[diplomat::rust_link(icu::plurals::PluralCategory::get_for_cldr_string, FnInEnum)] 34 #[diplomat::rust_link(icu::plurals::PluralCategory::get_for_cldr_bytes, FnInEnum)] get_for_cldr_string(s: &DiplomatStr) -> Option<PluralCategory>35 pub fn get_for_cldr_string(s: &DiplomatStr) -> Option<PluralCategory> { 36 icu_plurals::PluralCategory::get_for_cldr_bytes(s).map(Into::into) 37 } 38 } 39 40 #[diplomat::rust_link(icu::plurals::PluralRules, Struct)] 41 #[diplomat::opaque] 42 pub struct PluralRules(icu_plurals::PluralRules); 43 44 impl PluralRules { 45 /// Construct an [`PluralRules`] for the given locale, for cardinal numbers, using compiled data. 46 #[diplomat::rust_link(icu::plurals::PluralRules::try_new_cardinal, FnInStruct)] 47 #[diplomat::rust_link(icu::plurals::PluralRules::try_new, FnInStruct, hidden)] 48 #[diplomat::rust_link(icu::plurals::PluralRuleType, Enum, hidden)] 49 #[diplomat::rust_link(icu::plurals::PluralRulesOptions, Struct, hidden)] 50 #[diplomat::rust_link(icu::plurals::PluralRulesOptions::default, FnInStruct, hidden)] 51 #[diplomat::rust_link(icu::plurals::PluralRulesOptions::with_type, FnInStruct, hidden)] 52 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "cardinal")] 53 #[cfg(feature = "compiled_data")] create_cardinal(locale: &Locale) -> Result<Box<PluralRules>, DataError>54 pub fn create_cardinal(locale: &Locale) -> Result<Box<PluralRules>, DataError> { 55 let prefs = icu_plurals::PluralRulesPreferences::from(&locale.0); 56 Ok(Box::new(PluralRules( 57 icu_plurals::PluralRules::try_new_cardinal(prefs)?, 58 ))) 59 } 60 /// Construct an [`PluralRules`] for the given locale, for cardinal numbers, using a particular data source. 61 #[diplomat::rust_link(icu::plurals::PluralRules::try_new_cardinal, FnInStruct)] 62 #[diplomat::rust_link(icu::plurals::PluralRules::try_new, FnInStruct, hidden)] 63 #[diplomat::rust_link(icu::plurals::PluralRuleType, Enum, hidden)] 64 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "cardinal_with_provider")] 65 #[cfg(feature = "buffer_provider")] create_cardinal_with_provider( provider: &DataProvider, locale: &Locale, ) -> Result<Box<PluralRules>, DataError>66 pub fn create_cardinal_with_provider( 67 provider: &DataProvider, 68 locale: &Locale, 69 ) -> Result<Box<PluralRules>, DataError> { 70 let prefs = icu_plurals::PluralRulesPreferences::from(&locale.0); 71 Ok(Box::new(PluralRules( 72 icu_plurals::PluralRules::try_new_cardinal_with_buffer_provider( 73 provider.get()?, 74 prefs, 75 )?, 76 ))) 77 } 78 /// Construct an [`PluralRules`] for the given locale, for ordinal numbers, using compiled data. 79 #[diplomat::rust_link(icu::plurals::PluralRules::try_new_ordinal, FnInStruct)] 80 #[diplomat::rust_link(icu::plurals::PluralRules::try_new, FnInStruct, hidden)] 81 #[diplomat::rust_link(icu::plurals::PluralRuleType, Enum, hidden)] 82 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "ordinal")] 83 #[cfg(feature = "compiled_data")] create_ordinal(locale: &Locale) -> Result<Box<PluralRules>, DataError>84 pub fn create_ordinal(locale: &Locale) -> Result<Box<PluralRules>, DataError> { 85 let prefs = icu_plurals::PluralRulesPreferences::from(&locale.0); 86 Ok(Box::new(PluralRules( 87 icu_plurals::PluralRules::try_new_ordinal(prefs)?, 88 ))) 89 } 90 /// Construct an [`PluralRules`] for the given locale, for ordinal numbers, using a particular data source. 91 #[diplomat::rust_link(icu::plurals::PluralRules::try_new_ordinal, FnInStruct)] 92 #[diplomat::rust_link(icu::plurals::PluralRules::try_new, FnInStruct, hidden)] 93 #[diplomat::rust_link(icu::plurals::PluralRuleType, Enum, hidden)] 94 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor = "ordinal_with_provider")] 95 #[cfg(feature = "buffer_provider")] create_ordinal_with_provider( provider: &DataProvider, locale: &Locale, ) -> Result<Box<PluralRules>, DataError>96 pub fn create_ordinal_with_provider( 97 provider: &DataProvider, 98 locale: &Locale, 99 ) -> Result<Box<PluralRules>, DataError> { 100 let prefs = icu_plurals::PluralRulesPreferences::from(&locale.0); 101 Ok(Box::new(PluralRules( 102 icu_plurals::PluralRules::try_new_ordinal_with_buffer_provider( 103 provider.get()?, 104 prefs, 105 )?, 106 ))) 107 } 108 /// Get the category for a given number represented as operands 109 #[diplomat::rust_link(icu::plurals::PluralRules::category_for, FnInStruct)] category_for(&self, op: &PluralOperands) -> PluralCategory110 pub fn category_for(&self, op: &PluralOperands) -> PluralCategory { 111 self.0.category_for(op.0).into() 112 } 113 114 /// Get all of the categories needed in the current locale 115 #[diplomat::rust_link(icu::plurals::PluralRules::categories, FnInStruct)] 116 #[diplomat::attr(auto, getter)] categories(&self) -> PluralCategories117 pub fn categories(&self) -> PluralCategories { 118 PluralCategories::from_iter(self.0.categories()) 119 } 120 } 121 122 #[diplomat::opaque] 123 #[diplomat::rust_link(icu::plurals::PluralOperands, Struct)] 124 pub struct PluralOperands(pub icu_plurals::PluralOperands); 125 126 impl PluralOperands { 127 /// Construct for a given string representing a number 128 #[diplomat::rust_link(icu::plurals::PluralOperands::from_str, FnInStruct)] 129 #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)] from_string(s: &DiplomatStr) -> Result<Box<PluralOperands>, FixedDecimalParseError>130 pub fn from_string(s: &DiplomatStr) -> Result<Box<PluralOperands>, FixedDecimalParseError> { 131 Ok(Box::new(PluralOperands(icu_plurals::PluralOperands::from( 132 &fixed_decimal::Decimal::try_from_utf8(s)?, 133 )))) 134 } 135 136 /// Construct for a given integer 137 #[diplomat::attr(auto, named_constructor)] 138 #[diplomat::attr(dart, rename = "from_int")] 139 #[diplomat::attr(js, rename = "from_big_int")] 140 #[diplomat::attr(supports = method_overloading, rename = "from")] from_int64(i: i64) -> Box<PluralOperands>141 pub fn from_int64(i: i64) -> Box<PluralOperands> { 142 Box::new(PluralOperands(icu_plurals::PluralOperands::from(i))) 143 } 144 145 /// Construct from a FixedDecimal 146 /// 147 /// Retains at most 18 digits each from the integer and fraction parts. 148 #[cfg(feature = "decimal")] 149 #[diplomat::attr(auto, named_constructor)] from_fixed_decimal(x: &crate::fixed_decimal::ffi::Decimal) -> Box<Self>150 pub fn from_fixed_decimal(x: &crate::fixed_decimal::ffi::Decimal) -> Box<Self> { 151 Box::new(Self((&x.0).into())) 152 } 153 } 154 155 #[diplomat::out] 156 pub struct PluralCategories { 157 pub zero: bool, 158 pub one: bool, 159 pub two: bool, 160 pub few: bool, 161 pub many: bool, 162 pub other: bool, 163 } 164 165 impl PluralCategories { from_iter(i: impl Iterator<Item = icu_plurals::PluralCategory>) -> Self166 fn from_iter(i: impl Iterator<Item = icu_plurals::PluralCategory>) -> Self { 167 i.fold( 168 PluralCategories { 169 zero: false, 170 one: false, 171 two: false, 172 few: false, 173 many: false, 174 other: false, 175 }, 176 |mut categories, category| { 177 match category { 178 icu_plurals::PluralCategory::Zero => categories.zero = true, 179 icu_plurals::PluralCategory::One => categories.one = true, 180 icu_plurals::PluralCategory::Two => categories.two = true, 181 icu_plurals::PluralCategory::Few => categories.few = true, 182 icu_plurals::PluralCategory::Many => categories.many = true, 183 icu_plurals::PluralCategory::Other => categories.other = true, 184 }; 185 categories 186 }, 187 ) 188 } 189 } 190 } 191