| 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 |
use fixed_decimal::UnsignedDecimal; |
| 6 |
use icu_plurals::PluralOperands; |
| 7 |
use writeable::Writeable; |
| 8 |
|
| 9 |
use crate::{ |
| 10 |
CompactDecimalFormatter, DecimalFormatter, FormattedSign, FormattedUnsignedCompactDecimal, |
| 11 |
FormattedUnsignedDecimal, |
| 12 |
}; |
| 13 |
|
| 14 |
pub trait Sealed {} |
| 15 |
|
| 16 |
/// A trait representing an abstract number formatter. |
| 17 |
/// |
| 18 |
/// This is a building block for more complicated formatters, like currency or units. |
| 19 |
/// |
| 20 |
/// <div class="stab unstable"> |
| 21 |
/// 🚫 This trait is sealed; it cannot be implemented by user code. If an API requests an item that implements this |
| 22 |
/// trait, please consider using a type from the implementors listed below. |
| 23 |
/// </div> |
| 24 |
pub trait AbstractFormatter: core::fmt::Debug + Sealed { |
| 25 |
#[doc(hidden)] |
| 26 |
type FormattedUnsigned<'a>: Writeable |
| 27 |
where |
| 28 |
Self: 'a; |
| 29 |
|
| 30 |
#[doc(hidden)] |
| 31 |
fn format_unsigned<'a>(&'a self, value: UnsignedDecimal) -> Self::FormattedUnsigned<'a>; |
| 32 |
|
| 33 |
#[doc(hidden)] |
| 34 |
fn format_sign<'a, W: Writeable>( |
| 35 |
&'a self, |
| 36 |
value: W, |
| 37 |
sign: fixed_decimal::Sign, |
| 38 |
) -> FormattedSign<'a, W>; |
| 39 |
|
| 40 |
#[doc(hidden)] |
| 41 |
fn plural_operands(value: &Self::FormattedUnsigned<'_>) -> PluralOperands; |
| 42 |
} |
| 43 |
|
| 44 |
impl Sealed for DecimalFormatter {} |
| 45 |
impl AbstractFormatter for DecimalFormatter { |
| 46 |
type FormattedUnsigned<'a> = FormattedUnsignedDecimal<'a>; |
| 47 |
|
| 48 |
fn format_unsigned<'a>(&'a self, value: UnsignedDecimal) -> Self::FormattedUnsigned<'a> { |
| 49 |
self.format_unsigned(crate::Cow::Owned(value)) |
| 50 |
} |
| 51 |
|
| 52 |
fn format_sign<'a, W: Writeable>( |
| 53 |
&'a self, |
| 54 |
value: W, |
| 55 |
sign: fixed_decimal::Sign, |
| 56 |
) -> FormattedSign<'a, W> { |
| 57 |
self.format_sign(sign, value) |
| 58 |
} |
| 59 |
|
| 60 |
fn plural_operands(value: &Self::FormattedUnsigned<'_>) -> PluralOperands { |
| 61 |
value.plural_operands() |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl Sealed for CompactDecimalFormatter {} |
| 66 |
impl AbstractFormatter for CompactDecimalFormatter { |
| 67 |
type FormattedUnsigned<'a> = FormattedUnsignedCompactDecimal<'a>; |
| 68 |
|
| 69 |
fn format_unsigned<'a>(&'a self, value: UnsignedDecimal) -> Self::FormattedUnsigned<'a> { |
| 70 |
self.format_unsigned(&value) |
| 71 |
} |
| 72 |
|
| 73 |
fn format_sign<'a, W: Writeable>( |
| 74 |
&'a self, |
| 75 |
value: W, |
| 76 |
sign: fixed_decimal::Sign, |
| 77 |
) -> FormattedSign<'a, W> { |
| 78 |
self.decimal_formatter.format_sign(sign, value) |
| 79 |
} |
| 80 |
|
| 81 |
fn plural_operands(value: &Self::FormattedUnsigned<'_>) -> PluralOperands { |
| 82 |
value.plural_operands() |
| 83 |
} |
| 84 |
} |