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
167
168
169
// 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.

use fixed_decimal::FixedDecimal;
use icu_decimal::options::FixedDecimalFormatterOptions;
use icu_decimal::FixedDecimalFormatter;
use icu_provider::{
    DataError, DataIdentifierBorrowed, DataLocale, DataPayload, DataProvider, DataRequest,
};

use super::super::provider::percent::PercentEssentialsV1Marker;
use super::format::FormattedPercent;
use super::options::PercentFormatterOptions;

extern crate alloc;

/// A formatter for percent values.
///
/// [`PercentFormatter`] supports:
///   1. Rendering in the locale's percent system.
pub struct PercentFormatter<R> {
    /// Essential data for the percent formatter.
    essential: DataPayload<PercentEssentialsV1Marker>,

    /// Options bag for the percent formatter to determine the behavior of the formatter.
    options: PercentFormatterOptions,

    /// A fixed decimal formatter used to format the percent value.
    fixed_decimal_formatter: R,
}

impl PercentFormatter<FixedDecimalFormatter> {
    icu_provider::gen_any_buffer_data_constructors!(
        (locale, options: PercentFormatterOptions) -> error: DataError,
        functions: [
            try_new: skip,
            try_new_with_any_provider,
            try_new_with_buffer_provider,
            try_new_unstable,
            Self
        ]
    );

    /// Creates a new [`PercentFormatter`] from compiled locale data and an options bag.
    ///
    /// ✨ *Enabled with the `compiled_data` Cargo feature.*
    ///
    /// [📚 Help choosing a constructor](icu_provider::constructors)
    #[cfg(feature = "compiled_data")]
    pub fn try_new(
        locale: &DataLocale,
        options: PercentFormatterOptions,
    ) -> Result<Self, DataError> {
        let fixed_decimal_formatter =
            FixedDecimalFormatter::try_new(locale, FixedDecimalFormatterOptions::default())?;

        PercentFormatter::try_new_with_fixed_decimal_formatter(
            locale,
            fixed_decimal_formatter,
            options,
        )
    }

    #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
    pub fn try_new_unstable<D>(
        provider: &D,
        locale: &DataLocale,
        options: PercentFormatterOptions,
    ) -> Result<Self, DataError>
    where
        D: ?Sized
            + DataProvider<super::super::provider::percent::PercentEssentialsV1Marker>
            + DataProvider<icu_decimal::provider::DecimalSymbolsV2Marker>,
    {
        let fixed_decimal_formatter = FixedDecimalFormatter::try_new_unstable(
            provider,
            locale,
            FixedDecimalFormatterOptions::default(),
        )?;

        PercentFormatter::try_new_with_fixed_decimal_formatter_unstable(
            provider,
            locale,
            fixed_decimal_formatter,
            options,
        )
    }

    /// Formats a [`FixedDecimal`] value for the given percent code.
    ///
    /// # Examples
    /// ```
    /// use icu::experimental::dimension::percent::formatter::{
    ///     PercentFormatter,
    /// };
    /// use icu::locale::locale;
    /// use writeable::Writeable;
    ///
    /// let locale = locale!("en-US").into();
    /// let fmt = PercentFormatter::try_new(&locale, Default::default()).unwrap();
    /// let value = "12345.67".parse().unwrap();
    /// let formatted_percent = fmt.format(&value);
    /// let mut sink = String::new();
    /// formatted_percent.write_to(&mut sink).unwrap();
    /// assert_eq!(sink.as_str(), "12,345.67%");
    /// ```
    pub fn format<'l>(&'l self, value: &'l FixedDecimal) -> FormattedPercent<'l> {
        FormattedPercent {
            value,
            essential: self.essential.get(),
            fixed_decimal_formatter: &self.fixed_decimal_formatter,
            options: &self.options,
        }
    }
}

impl<R> PercentFormatter<R>
where
    R: AsRef<FixedDecimalFormatter>,
{
    /// Creates a new [`PercentFormatter`] from compiled locale data, an options bag and fixed decimal formatter.
    ///
    /// ✨ *Enabled with the `compiled_data` Cargo feature.*
    ///
    /// [📚 Help choosing a constructor](icu_provider::constructors)
    #[cfg(feature = "compiled_data")]
    pub fn try_new_with_fixed_decimal_formatter(
        locale: &DataLocale,
        fixed_decimal_formatter: R,
        options: PercentFormatterOptions,
    ) -> Result<Self, DataError> {
        let essential = crate::provider::Baked
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(locale),
                ..Default::default()
            })?
            .payload;

        Ok(Self {
            essential,
            options,
            fixed_decimal_formatter,
        })
    }

    #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
    pub fn try_new_with_fixed_decimal_formatter_unstable(
        provider: &(impl DataProvider<PercentEssentialsV1Marker> + ?Sized),
        locale: &DataLocale,
        fixed_decimal_formatter: R,
        options: PercentFormatterOptions,
    ) -> Result<Self, DataError> {
        let essential = provider
            .load(DataRequest {
                id: DataIdentifierBorrowed::for_locale(locale),
                ..Default::default()
            })?
            .payload;

        Ok(Self {
            essential,
            options,
            fixed_decimal_formatter,
        })
    }
}