| 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 |
//! 🚧 \[Unstable\] Data provider struct definitions for this ICU4X component. |
| 6 |
//! |
| 7 |
//! <div class="stab unstable"> |
| 8 |
//! 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 9 |
//! including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 10 |
//! to be stable, their Rust representation might not be. Use with caution. |
| 11 |
//! </div> |
| 12 |
//! |
| 13 |
//! Read more about data providers: [`icu_provider`] |
| 14 |
//! |
| 15 |
//! # Examples |
| 16 |
//! |
| 17 |
//! ## Get the resolved numbering system |
| 18 |
//! |
| 19 |
//! In a constructor call, the _last_ request for [`DecimalDigitsV1`] |
| 20 |
//! contains the resolved numbering system as its attribute: |
| 21 |
//! |
| 22 |
//! ``` |
| 23 |
//! use icu::decimal::DecimalFormatter; |
| 24 |
//! use icu::decimal::provider::DecimalDigitsV1; |
| 25 |
//! use icu::locale::locale; |
| 26 |
//! use icu_provider::prelude::*; |
| 27 |
//! use std::any::TypeId; |
| 28 |
//! use std::cell::RefCell; |
| 29 |
//! |
| 30 |
//! struct NumberingSystemInspectionProvider<P> { |
| 31 |
//! inner: P, |
| 32 |
//! numbering_system: RefCell<Option<Box<DataMarkerAttributes>>>, |
| 33 |
//! } |
| 34 |
//! |
| 35 |
//! impl<M, P> DataProvider<M> for NumberingSystemInspectionProvider<P> |
| 36 |
//! where |
| 37 |
//! M: DataMarker, |
| 38 |
//! P: DataProvider<M>, |
| 39 |
//! { |
| 40 |
//! fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> { |
| 41 |
//! if TypeId::of::<M>() == TypeId::of::<DecimalDigitsV1>() { |
| 42 |
//! *self.numbering_system.try_borrow_mut().unwrap() = |
| 43 |
//! Some(req.id.marker_attributes.to_owned()); |
| 44 |
//! } |
| 45 |
//! self.inner.load(req) |
| 46 |
//! } |
| 47 |
//! } |
| 48 |
//! |
| 49 |
//! let provider = NumberingSystemInspectionProvider { |
| 50 |
//! inner: icu::decimal::provider::Baked, |
| 51 |
//! numbering_system: RefCell::new(None), |
| 52 |
//! }; |
| 53 |
//! |
| 54 |
//! let formatter = DecimalFormatter::try_new_unstable( |
| 55 |
//! &provider, |
| 56 |
//! locale!("th").into(), |
| 57 |
//! Default::default(), |
| 58 |
//! ) |
| 59 |
//! .unwrap(); |
| 60 |
//! |
| 61 |
//! assert_eq!( |
| 62 |
//! provider |
| 63 |
//! .numbering_system |
| 64 |
//! .borrow() |
| 65 |
//! .as_ref() |
| 66 |
//! .map(|x| x.as_str()), |
| 67 |
//! Some("latn") |
| 68 |
//! ); |
| 69 |
//! |
| 70 |
//! let formatter = DecimalFormatter::try_new_unstable( |
| 71 |
//! &provider, |
| 72 |
//! locale!("th-u-nu-thai").into(), |
| 73 |
//! Default::default(), |
| 74 |
//! ) |
| 75 |
//! .unwrap(); |
| 76 |
//! |
| 77 |
//! assert_eq!( |
| 78 |
//! provider |
| 79 |
//! .numbering_system |
| 80 |
//! .borrow() |
| 81 |
//! .as_ref() |
| 82 |
//! .map(|x| x.as_str()), |
| 83 |
//! Some("thai") |
| 84 |
//! ); |
| 85 |
//! |
| 86 |
//! let formatter = DecimalFormatter::try_new_unstable( |
| 87 |
//! &provider, |
| 88 |
//! locale!("th-u-nu-adlm").into(), |
| 89 |
//! Default::default(), |
| 90 |
//! ) |
| 91 |
//! .unwrap(); |
| 92 |
//! |
| 93 |
//! assert_eq!( |
| 94 |
//! provider |
| 95 |
//! .numbering_system |
| 96 |
//! .borrow() |
| 97 |
//! .as_ref() |
| 98 |
//! .map(|x| x.as_str()), |
| 99 |
//! Some("adlm") |
| 100 |
//! ); |
| 101 |
//! ``` |
| 102 |
|
| 103 |
// Provider structs must be stable |
| 104 |
#![allow(clippy::exhaustive_structs)] |
| 105 |
#![allow(clippy::exhaustive_enums)] |
| 106 |
|
| 107 |
#[cfg(any(feature = "datagen", feature = "serde"))] |
| 108 |
#[cfg(feature = "unstable")] |
| 109 |
use alloc::boxed::Box; |
| 110 |
#[cfg(feature = "datagen")] |
| 111 |
#[cfg(feature = "unstable")] |
| 112 |
use alloc::vec::Vec; |
| 113 |
#[cfg(feature = "unstable")] |
| 114 |
use icu_pattern::{Pattern, PatternBackend, SinglePlaceholder}; |
| 115 |
#[cfg(feature = "unstable")] |
| 116 |
use icu_plurals::provider::PluralElementsPackedULE; |
| 117 |
use icu_provider::prelude::*; |
| 118 |
use zerovec::VarZeroCow; |
| 119 |
#[cfg(feature = "unstable")] |
| 120 |
use zerovec::VarZeroVec; |
| 121 |
#[cfg(feature = "unstable")] |
| 122 |
use zerovec::ule::vartuple::VarTupleULE; |
| 123 |
|
| 124 |
#[cfg(feature = "compiled_data")] |
| 125 |
#[derive(Debug)] |
| 126 |
/// Baked data |
| 127 |
/// |
| 128 |
/// <div class="stab unstable"> |
| 129 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 130 |
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only |
| 131 |
/// guaranteed to match with this version's `*_unstable` providers. Use with caution. |
| 132 |
/// </div> |
| 133 |
pub struct Baked; |
| 134 |
|
| 135 |
#[cfg(feature = "compiled_data")] |
| 136 |
#[allow(unused_imports)] |
| 137 |
const _: () = { |
| 138 |
use icu_decimal_data::*; |
| 139 |
pub mod icu { |
| 140 |
pub use crate as decimal; |
| 141 |
} |
| 142 |
make_provider!(Baked); |
| 143 |
#[cfg(feature = "unstable")] |
| 144 |
impl_decimal_compact_long_v1!(Baked); |
| 145 |
#[cfg(feature = "unstable")] |
| 146 |
impl_decimal_compact_short_v1!(Baked); |
| 147 |
impl_decimal_symbols_v1!(Baked); |
| 148 |
impl_decimal_digits_v1!(Baked); |
| 149 |
}; |
| 150 |
|
| 151 |
icu_provider::data_marker!( |
| 152 |
/// Data marker for decimal symbols |
| 153 |
DecimalSymbolsV1, |
| 154 |
"decimal/symbols/v1", |
| 155 |
DecimalSymbols<'static>, |
| 156 |
); |
| 157 |
|
| 158 |
icu_provider::data_marker!( |
| 159 |
/// The digits for a given numbering system. This data ought to be stored in the `und` locale with a marker attribute |
| 160 |
/// set to the numbering system code. |
| 161 |
DecimalDigitsV1, |
| 162 |
"decimal/digits/v1", |
| 163 |
[char; 10], |
| 164 |
#[cfg(feature = "datagen")] |
| 165 |
attributes_domain = "numbering_system" |
| 166 |
); |
| 167 |
|
| 168 |
#[cfg(feature = "datagen")] |
| 169 |
/// The latest minimum set of markers required by this component. |
| 170 |
#[cfg(feature = "unstable")] |
| 171 |
pub const MARKERS: &[DataMarkerInfo] = &[ |
| 172 |
DecimalSymbolsV1::INFO, |
| 173 |
DecimalDigitsV1::INFO, |
| 174 |
DecimalCompactLongV1::INFO, |
| 175 |
DecimalCompactShortV1::INFO, |
| 176 |
]; |
| 177 |
|
| 178 |
#[cfg(feature = "datagen")] |
| 179 |
/// The latest minimum set of markers required by this component. |
| 180 |
#[cfg(not(feature = "unstable"))] |
| 181 |
pub const MARKERS: &[DataMarkerInfo] = &[DecimalSymbolsV1::INFO, DecimalDigitsV1::INFO]; |
| 182 |
|
| 183 |
/// A collection of settings expressing where to put grouping separators in a decimal number. |
| 184 |
/// For example, `1,000,000` has two grouping separators, positioned along every 3 digits. |
| 185 |
/// |
| 186 |
/// <div class="stab unstable"> |
| 187 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 188 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 189 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 190 |
/// </div> |
| 191 |
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, Copy, zerofrom::ZeroFrom)] |
| 192 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 193 |
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] |
| 194 |
#[cfg_attr(feature = "datagen", databake(path = icu_decimal::provider))] |
| 195 |
pub struct GroupingSizes { |
| 196 |
/// The size of the first (lowest-magnitude) group. |
| 197 |
/// |
| 198 |
/// If 0, grouping separators will never be shown. |
| 199 |
pub primary: u8, |
| 200 |
|
| 201 |
/// The size of groups after the first group. |
| 202 |
/// |
| 203 |
/// If 0, defaults to be the same as `primary`. |
| 204 |
pub secondary: u8, |
| 205 |
|
| 206 |
/// The minimum number of digits required before the first group. For example, if `primary=3` |
| 207 |
/// and `min_grouping=2`, grouping separators will be present on 10,000 and above. |
| 208 |
pub min_grouping: u8, |
| 209 |
} |
| 210 |
|
| 211 |
/// A stack representation of the strings used in [`DecimalSymbols`], i.e. a builder type |
| 212 |
/// for [`DecimalSymbolsStrs`]. This type can be obtained from a [`DecimalSymbolsStrs`] |
| 213 |
/// the `From`/`Into` traits. |
| 214 |
/// |
| 215 |
/// <div class="stab unstable"> |
| 216 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 217 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 218 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 219 |
/// </div> |
| 220 |
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 221 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 222 |
#[cfg_attr(feature = "datagen", derive(serde::Serialize))] |
| 223 |
#[zerovec::make_varule(DecimalSymbolsStrs)] |
| 224 |
#[zerovec::derive(Debug)] |
| 225 |
#[zerovec::skip_derive(Ord)] |
| 226 |
#[cfg_attr(not(feature = "alloc"), zerovec::skip_derive(ZeroMapKV, ToOwned))] |
| 227 |
#[cfg_attr(feature = "serde", zerovec::derive(Deserialize))] |
| 228 |
#[cfg_attr(feature = "datagen", zerovec::derive(Serialize))] |
| 229 |
// Each affix/separator is at most three characters, which tends to be around 3-12 bytes each |
| 230 |
// and the numbering system is at most 8 ascii bytes, All put together the indexing is extremely |
| 231 |
// unlikely to have to go past 256. |
| 232 |
#[zerovec::format(zerovec::vecs::Index8)] |
| 233 |
pub struct DecimalSymbolStrsBuilder<'data> { |
| 234 |
/// Prefix to apply when a negative sign is needed. |
| 235 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 236 |
pub minus_sign_prefix: VarZeroCow<'data, str>, |
| 237 |
/// Suffix to apply when a negative sign is needed. |
| 238 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 239 |
pub minus_sign_suffix: VarZeroCow<'data, str>, |
| 240 |
|
| 241 |
/// Prefix to apply when a positive sign is needed. |
| 242 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 243 |
pub plus_sign_prefix: VarZeroCow<'data, str>, |
| 244 |
/// Suffix to apply when a positive sign is needed. |
| 245 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 246 |
pub plus_sign_suffix: VarZeroCow<'data, str>, |
| 247 |
|
| 248 |
/// Character used to separate the integer and fraction parts of the number. |
| 249 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 250 |
pub decimal_separator: VarZeroCow<'data, str>, |
| 251 |
|
| 252 |
/// Character used to separate groups in the integer part of the number. |
| 253 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 254 |
pub grouping_separator: VarZeroCow<'data, str>, |
| 255 |
|
| 256 |
/// The numbering system to use. |
| 257 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 258 |
pub numsys: VarZeroCow<'data, str>, |
| 259 |
} |
| 260 |
|
| 261 |
#[cfg(feature = "alloc")] |
| 262 |
impl DecimalSymbolStrsBuilder<'_> { |
| 263 |
/// Build a [`DecimalSymbolsStrs`] |
| 264 |
/// |
| 265 |
/// ✨ *Enabled with the `alloc` Cargo feature.* |
| 266 |
pub fn build(&self) -> VarZeroCow<'static, DecimalSymbolsStrs> { |
| 267 |
VarZeroCow::from_encodeable(self) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/// Symbols and metadata required for formatting a [`Decimal`](crate::input::Decimal). |
| 272 |
/// |
| 273 |
/// <div class="stab unstable"> |
| 274 |
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 275 |
/// including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 276 |
/// to be stable, their Rust representation might not be. Use with caution. |
| 277 |
/// </div> |
| 278 |
#[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 279 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 280 |
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] |
| 281 |
#[cfg_attr(feature = "datagen", databake(path = icu_decimal::provider))] |
| 282 |
pub struct DecimalSymbols<'data> { |
| 283 |
/// String data for the symbols: +/- affixes and separators |
| 284 |
#[cfg_attr(feature = "serde", serde(borrow))] |
| 285 |
// We use a VarZeroCow here to reduce the stack size of DecimalSymbols: instead of serializing multiple strs, |
| 286 |
// this type will now serialize as a single u8 buffer with optimized indexing that packs all the data together |
| 287 |
pub strings: VarZeroCow<'data, DecimalSymbolsStrs>, |
| 288 |
|
| 289 |
/// Settings used to determine where to place groups in the integer part of the number. |
| 290 |
pub grouping_sizes: GroupingSizes, |
| 291 |
} |
| 292 |
|
| 293 |
icu_provider::data_struct!( |
| 294 |
DecimalSymbols<'_>, |
| 295 |
#[cfg(feature = "datagen")] |
| 296 |
); |
| 297 |
|
| 298 |
impl<'a> core::ops::Deref for DecimalSymbols<'a> { |
| 299 |
type Target = VarZeroCow<'a, DecimalSymbolsStrs>; |
| 300 |
|
| 301 |
fn deref(&self) -> &Self::Target { |
| 302 |
&self.strings |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
impl DecimalSymbols<'static> { |
| 307 |
/// Create a new en-US format for use in testing |
| 308 |
#[cfg(feature = "datagen")] |
| 309 |
pub fn new_en_for_testing() -> Self { |
| 310 |
let strings = DecimalSymbolStrsBuilder { |
| 311 |
minus_sign_prefix: VarZeroCow::new_borrowed("-"), |
| 312 |
minus_sign_suffix: VarZeroCow::new_borrowed(""), |
| 313 |
plus_sign_prefix: VarZeroCow::new_borrowed("+"), |
| 314 |
plus_sign_suffix: VarZeroCow::new_borrowed(""), |
| 315 |
decimal_separator: VarZeroCow::new_borrowed("."), |
| 316 |
grouping_separator: VarZeroCow::new_borrowed(","), |
| 317 |
numsys: VarZeroCow::new_borrowed("latn"), |
| 318 |
}; |
| 319 |
Self { |
| 320 |
strings: VarZeroCow::from_encodeable(&strings), |
| 321 |
grouping_sizes: GroupingSizes { |
| 322 |
primary: 3, |
| 323 |
secondary: 3, |
| 324 |
min_grouping: 1, |
| 325 |
}, |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
#[cfg(feature = "unstable")] |
| 331 |
icu_provider::data_marker!( |
| 332 |
/// `DecimalCompactLongV1` |
| 333 |
DecimalCompactLongV1, |
| 334 |
CompactPatterns<'static, SinglePlaceholder>, |
| 335 |
); |
| 336 |
#[cfg(feature = "unstable")] |
| 337 |
icu_provider::data_marker!( |
| 338 |
/// `DecimalCompactShortV1` |
| 339 |
DecimalCompactShortV1, |
| 340 |
CompactPatterns<'static, SinglePlaceholder>, |
| 341 |
); |
| 342 |
|
| 343 |
/// Compact pattern data struct. |
| 344 |
/// |
| 345 |
/// As in CLDR, this is a mapping from type (a power of ten, corresponding to |
| 346 |
/// the magnitude of the number being formatted) to a plural pattern. |
| 347 |
/// |
| 348 |
/// If all plural patterns are compatible across consecutive types, the |
| 349 |
/// larger types are omitted, thus given |
| 350 |
/// > (3, other) ↦ 0K, (4, other) ↦ 00K, (5, other) ↦ 000K |
| 351 |
/// |
| 352 |
/// only |
| 353 |
/// > (3, other) ↦ 0K |
| 354 |
/// |
| 355 |
/// is stored. |
| 356 |
/// |
| 357 |
/// Finally, the pattern indicating noncompact notation for the first few powers |
| 358 |
/// of ten might be omitted; that is, there is an implicit (0, other) ↦ 0. |
| 359 |
/// |
| 360 |
/// The plural patterns are stored with the 4-bit metadata representing the exponent |
| 361 |
/// shift (number of zeros in the pattern minus 1). |
| 362 |
#[derive(Debug, Clone, PartialEq, yoke::Yokeable, zerofrom::ZeroFrom)] |
| 363 |
#[cfg_attr(feature = "datagen", derive(databake::Bake))] |
| 364 |
#[cfg_attr(feature = "datagen", databake(path = icu_decimal::provider))] |
| 365 |
#[cfg(feature = "unstable")] |
| 366 |
pub struct CompactPatterns<'a, P: PatternBackend>( |
| 367 |
pub VarZeroVec<'a, VarTupleULE<u8, PluralElementsPackedULE<Pattern<P>>>>, |
| 368 |
); |
| 369 |
|
| 370 |
#[cfg(feature = "datagen")] |
| 371 |
#[cfg(feature = "unstable")] |
| 372 |
impl<'data, P: PatternBackend> serde::Serialize for CompactPatterns<'data, P> |
| 373 |
where |
| 374 |
Pattern<P>: serde::Serialize, |
| 375 |
{ |
| 376 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 377 |
where |
| 378 |
S: serde::Serializer, |
| 379 |
{ |
| 380 |
self.0.serialize(serializer) |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
#[cfg(feature = "serde")] |
| 385 |
#[cfg(feature = "unstable")] |
| 386 |
impl<'de, 'data, P: PatternBackend> serde::Deserialize<'de> for CompactPatterns<'data, P> |
| 387 |
where |
| 388 |
'de: 'data, |
| 389 |
Box<Pattern<P>>: serde::Deserialize<'de>, |
| 390 |
{ |
| 391 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 392 |
where |
| 393 |
D: serde::Deserializer<'de>, |
| 394 |
{ |
| 395 |
VarZeroVec::<VarTupleULE<u8, PluralElementsPackedULE<Pattern<P>>>>::deserialize( |
| 396 |
deserializer, |
| 397 |
) |
| 398 |
.map(Self) |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
#[cfg(feature = "unstable")] |
| 403 |
icu_provider::data_struct!(<P: PatternBackend> CompactPatterns<'_, P>, #[cfg(feature = "datagen")]); |
| 404 |
|
| 405 |
#[cfg(feature = "datagen")] |
| 406 |
#[cfg(feature = "unstable")] |
| 407 |
impl<P: PatternBackend> CompactPatterns<'static, P> { |
| 408 |
/// Creates a new [`CompactPatterns`] from a map of patterns. |
| 409 |
/// The values contains an additional `u8` that contains the |
| 410 |
/// magnitude of the pattern, which can be different from the |
| 411 |
/// magnitude key (e.g. for the maginute 5 there might be a |
| 412 |
/// magnitude 3 pattern). |
| 413 |
#[allow(clippy::type_complexity)] |
| 414 |
pub fn new( |
| 415 |
patterns: alloc::collections::BTreeMap< |
| 416 |
u8, |
| 417 |
(u8, icu_plurals::PluralElements<Box<Pattern<P>>>), |
| 418 |
>, |
| 419 |
zero_magnitude: Option<&icu_plurals::PluralElements<&Pattern<P>>>, |
| 420 |
) -> Result<Self, DataError> { |
| 421 |
use icu_plurals::PluralElements; |
| 422 |
use icu_plurals::provider::FourBitMetadata; |
| 423 |
use zerovec::ule::encode_varule_to_box; |
| 424 |
use zerovec::ule::vartuple::VarTuple; |
| 425 |
use zerovec::vecs::VarZeroVecOwned; |
| 426 |
|
| 427 |
if !patterns |
| 428 |
.values() |
| 429 |
.zip(patterns.values().skip(1)) |
| 430 |
.all(|(low, high)| low.0 <= high.0) |
| 431 |
{ |
| 432 |
Err( |
| 433 |
DataError::custom("Compact exponents should be nondecreasing").with_debug_context( |
| 434 |
&patterns |
| 435 |
.values() |
| 436 |
.map(|(exponent, _)| exponent) |
| 437 |
.collect::<Vec<_>>(), |
| 438 |
), |
| 439 |
)?; |
| 440 |
} |
| 441 |
|
| 442 |
let mut deduplicated_patterns: Vec<( |
| 443 |
u8, |
| 444 |
PluralElements<(FourBitMetadata, Box<Pattern<P>>)>, |
| 445 |
)> = Vec::new(); |
| 446 |
|
| 447 |
// Deduplicate sequences of types that have the same plural map, keeping the lowest type. |
| 448 |
for (log10_type, (exponent, map)) in patterns |
| 449 |
.into_iter() |
| 450 |
// Skip leading 0 patterns |
| 451 |
.skip_while(|(_, (_, pattern))| Some(&pattern.as_ref().map(|p| &**p)) == zero_magnitude) |
| 452 |
{ |
| 453 |
if let Some(prev) = deduplicated_patterns.last() { |
| 454 |
// The higher pattern can never be exactly one of the low pattern, so we can ignore that value |
| 455 |
if prev |
| 456 |
.1 |
| 457 |
.as_ref() |
| 458 |
.with_explicit_one_value(None) |
| 459 |
.map(|(_, p)| p) |
| 460 |
== map.as_ref() |
| 461 |
{ |
| 462 |
continue; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
// Store the exponent as a difference from the log10_type, i.e. the number of zeros |
| 467 |
// in the pattern, minus 1. No pattern should have more than 16 zeros. |
| 468 |
let Some(metadata) = FourBitMetadata::try_from_byte(log10_type - exponent) else { |
| 469 |
return Err(DataError::custom("Pattern has too many zeros") |
| 470 |
.with_debug_context(&(log10_type - exponent))); |
| 471 |
}; |
| 472 |
|
| 473 |
deduplicated_patterns.push((log10_type, map.map(|p| (metadata, p)))) |
| 474 |
} |
| 475 |
|
| 476 |
#[allow(clippy::unwrap_used)] // keyed by u8, so it cannot exceed usize/2 |
| 477 |
Ok(Self( |
| 478 |
VarZeroVecOwned::try_from_elements( |
| 479 |
&deduplicated_patterns |
| 480 |
.into_iter() |
| 481 |
.map(|(log10_type, plural_map)| { |
| 482 |
encode_varule_to_box(&VarTuple { |
| 483 |
sized: log10_type, |
| 484 |
variable: plural_map, |
| 485 |
}) |
| 486 |
}) |
| 487 |
.collect::<Vec<Box<VarTupleULE<u8, PluralElementsPackedULE<Pattern<P>>>>>>(), |
| 488 |
) |
| 489 |
.unwrap() |
| 490 |
.into(), |
| 491 |
)) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
pub(crate) fn load_with_fallback<'a, M: DataMarker>( |
| 496 |
provider: &(impl DataProvider<M> + ?Sized), |
| 497 |
ids: impl Iterator<Item = DataIdentifierBorrowed<'a>>, |
| 498 |
) -> Result<DataResponse<M>, DataError> { |
| 499 |
let mut ids = ids.peekable(); |
| 500 |
|
| 501 |
while let Some(id) = ids.next() { |
| 502 |
if ids.peek().is_some() { |
| 503 |
if let Some(r) = provider |
| 504 |
.load(DataRequest { |
| 505 |
id, |
| 506 |
metadata: { |
| 507 |
let mut m = DataRequestMetadata::default(); |
| 508 |
m.silent = true; |
| 509 |
m |
| 510 |
}, |
| 511 |
}) |
| 512 |
.allow_identifier_not_found()? |
| 513 |
{ |
| 514 |
return Ok(r); |
| 515 |
} |
| 516 |
} else { |
| 517 |
return provider.load(DataRequest { |
| 518 |
id, |
| 519 |
metadata: DataRequestMetadata::default(), |
| 520 |
}); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
Err(DataErrorKind::InvalidRequest.into_error()) |
| 525 |
} |
| 526 |
|
| 527 |
impl crate::DecimalFormatterPreferences { |
| 528 |
/// Returns a data identifier with the numbering system resolved from the preferences, if present. |
| 529 |
#[doc(hidden)] |
| 530 |
pub fn nu_id<'a>(&'a self, locale: &'a DataLocale) -> Option<DataIdentifierBorrowed<'a>> { |
| 531 |
self.numbering_system |
| 532 |
.as_ref() |
| 533 |
.map(|s| s.as_str()) |
| 534 |
.map(|nu| { |
| 535 |
DataIdentifierBorrowed::for_marker_attributes_and_locale( |
| 536 |
DataMarkerAttributes::from_str_or_panic(nu), |
| 537 |
locale, |
| 538 |
) |
| 539 |
}) |
| 540 |
} |
| 541 |
} |