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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt;
/// Contains the API configuration as prescribed by [ECMA 402][ecma].
///
/// [ecma]: https://www.ecma-international.org/publications/standards/Ecma-402.htm
///
/// The meaning of the options is the same as in the similarly named
/// options in the JS version.
///
/// See [Options] for the contents of the options. See the [NumberFormat::try_new]
/// for the use of the options.
pub mod options {
/// Controls whether short or long form display is used. Short is slightly
/// more economical with spacing. Only used when notation is Compact.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum CompactDisplay {
/// Default.
Short,
/// Long display layout.
Long,
}
/// The ISO currency code for a currency, if the currency formatting is being used.
/// For example "EUR" or "USD".
///
/// The value entered as currency is not validated. This responsibility is
/// delegated to the implementor.
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct Currency(pub String);
impl From<&str> for Currency {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
/// Controls the display of currency information.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum CurrencyDisplay {
/// Use a localized currency symbol such as €, this is the default value.
Symbol,
/// Narrow format symbol ("$100" rather than "US$100").
NarrowSymbol,
/// Use the ISO currency code.
Code,
/// Use a localized currency name, e.g. "dollar".
Name,
}
/// Controls the display of currency sign.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum CurrencySign {
/// Uses accounting notation such as `($100)` to mean "minus one hundred dollars".
Accounting,
/// Uses standard notation such as `$-100`.
Standard,
}
/// Controls the number formatting.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum Notation {
/// Example: 10,000.00. This is the default.
Standard,
/// Example: 1e+4
Scientific,
/// Example: 10k
Engineering,
/// Example: 10000
Compact,
}
/// Controls the number formatting.
///
/// Possible values include: "arab", "arabext", " bali", "beng", "deva", "fullwide", "gujr",
/// "guru", "hanidec", "khmr", " knda", "laoo", "latn", "limb", "mlym", " mong", "mymr",
/// "orya", "tamldec", " telu", "thai", "tibt".
///
/// The value entered as currency is not validated. This responsibility is
/// delegated to the implementor.
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct NumberingSystem(pub String);
impl From<&str> for NumberingSystem {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl Default for NumberingSystem {
fn default() -> Self {
NumberingSystem("latn".to_string())
}
}
/// When to display the sign of the number.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum SignDisplay {
/// Display for negative numbers only.
Auto,
/// Never display the sign.
Never,
/// Always display the sign (even plus).
Always,
/// Display for positive and negative numbers, but not zero.
ExceptZero,
}
/// When to display the sign of the number.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum Style {
/// For plain number formatting.
Decimal,
/// For currency formatting ("10 EUR")
Currency,
/// For percentage formatting ("10%")
Percent,
/// For unit formatting ("10 meters")
Unit,
}
/// The unit formatting style, when [Style::Unit] is used.
///
/// Possible values are core unit identifiers, defined in UTS #35, Part 2, Section 6. A subset
/// of units from the full list was selected for use in ECMAScript. Pairs of simple units can
/// be concatenated with "-per-" to make a compound unit. There is no default value; if the
/// style is "unit", the unit property must be provided.
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum UnitDisplay {
/// "16 litres"
Long,
/// "16 l"
Short,
/// "16l"
Narrow,
}
/// The unit to use when [Style::Unit] is selected as [Style].
///
/// Possible values are core unit identifiers, defined in UTS #35, Part 2, Section 6. A subset
/// of units from the full list was selected for use in ECMAScript. Pairs of simple units can
/// be concatenated with "-per-" to make a compound unit. There is no default value; if the
/// style is "unit", the unit property must be provided.
#[derive(Debug, Clone)]
pub struct Unit(pub String);
}
/// The options set by the user at construction time. See discussion at the top level
/// about the name choice. Provides as a "bag of options" since we don't expect any
/// implementations to be attached to this struct.
///
/// The default values of all the options are prescribed in by the [TC39 report][tc39lf].
///
/// [tc39lf]: https://tc39.es/proposal-intl-number-format/#sec-Intl.NumberFormat
#[derive(Debug, Clone)]
pub struct Options {
pub compact_display: Option<options::CompactDisplay>,
pub currency: Option<options::Currency>,
pub currency_display: options::CurrencyDisplay,
pub currency_sign: options::CurrencySign,
pub notation: options::Notation,
pub numbering_system: Option<options::NumberingSystem>,
pub sign_display: options::SignDisplay,
pub style: options::Style,
pub unit: Option<options::Unit>,
pub minimum_integer_digits: Option<u8>,
pub minimum_fraction_digits: Option<u8>,
pub maximum_fraction_digits: Option<u8>,
pub minimum_significant_digits: Option<u8>,
pub maximum_significant_digits: Option<u8>,
}
impl Default for Options {
/// Gets the default values of [Options] if omitted at setup. The
/// default values are prescribed in by the [TC39 report][tc39lf].
///
/// [tc39lf]: https://tc39.es/proposal-intl-list-format/#sec-Intl.ListFormat
fn default() -> Self {
Options {
compact_display: None,
currency: None,
currency_display: options::CurrencyDisplay::Symbol,
currency_sign: options::CurrencySign::Standard,
notation: options::Notation::Standard,
sign_display: options::SignDisplay::Auto,
style: options::Style::Decimal,
unit: None,
numbering_system: None,
minimum_integer_digits: None,
minimum_fraction_digits: None,
maximum_fraction_digits: None,
minimum_significant_digits: None,
maximum_significant_digits: None,
}
}
}
/// Formats number based on the rules configured on initialization.
pub trait NumberFormat {
/// The type of error reported, if any.
type Error: std::error::Error;
/// Creates a new [NumberFormat].
///
/// Creation may fail, for example, if the locale-specific data is not loaded, or if
/// the supplied options are inconsistent.
fn try_new<L>(l: L, opts: Options) -> Result<Self, Self::Error>
where
L: crate::Locale,
Self: Sized;
/// Formats `number` into the supplied `writer`.
///
/// The function implements [`Intl.NumberFormat`][nfmt] from [ECMA 402][ecma].
///
/// [nfmt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
/// [ecma]: https://www.ecma-international.org/publications/standards/Ecma-402.htm
fn format<W>(&self, number: f64, writer: &mut W) -> fmt::Result
where
W: fmt::Write;
}