/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_decimal-1.5.0/src/options.rs
Line | Count | Source (jump to first uncovered line) |
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 | | //! Options for [`FixedDecimalFormatter`](crate::FixedDecimalFormatter). |
6 | | |
7 | | /// A bag of options defining how numbers will be formatted by |
8 | | /// [`FixedDecimalFormatter`](crate::FixedDecimalFormatter). |
9 | | #[derive(Debug, Eq, PartialEq, Clone, Copy, Default, Hash)] |
10 | | #[non_exhaustive] |
11 | | pub struct FixedDecimalFormatterOptions { |
12 | | /// When to render grouping separators. |
13 | | pub grouping_strategy: GroupingStrategy, |
14 | | } |
15 | | |
16 | | impl From<GroupingStrategy> for FixedDecimalFormatterOptions { |
17 | 0 | fn from(grouping_strategy: GroupingStrategy) -> Self { |
18 | 0 | Self { grouping_strategy } |
19 | 0 | } |
20 | | } |
21 | | |
22 | | /// Configuration for how often to render grouping separators. |
23 | | /// |
24 | | /// # Examples |
25 | | /// |
26 | | /// ``` |
27 | | /// use icu::decimal::options; |
28 | | /// use icu::decimal::FixedDecimalFormatter; |
29 | | /// use icu::locid::Locale; |
30 | | /// use writeable::assert_writeable_eq; |
31 | | /// |
32 | | /// let locale = Locale::UND; |
33 | | /// let mut options: options::FixedDecimalFormatterOptions = Default::default(); |
34 | | /// options.grouping_strategy = options::GroupingStrategy::Min2; |
35 | | /// let fdf = FixedDecimalFormatter::try_new(&locale.into(), options) |
36 | | /// .expect("locale should be present"); |
37 | | /// |
38 | | /// let one_thousand = 1000.into(); |
39 | | /// assert_writeable_eq!(fdf.format(&one_thousand), "1000"); |
40 | | /// |
41 | | /// let ten_thousand = 10000.into(); |
42 | | /// assert_writeable_eq!(fdf.format(&ten_thousand), "10,000"); |
43 | | /// ``` |
44 | | #[non_exhaustive] |
45 | | #[derive(Debug, Eq, PartialEq, Clone, Copy, Hash)] |
46 | | pub enum GroupingStrategy { |
47 | | /// Render grouping separators according to locale preferences. |
48 | | Auto, |
49 | | |
50 | | /// Never render grouping separators. |
51 | | Never, |
52 | | |
53 | | /// Always render grouping separators. |
54 | | /// |
55 | | /// For [`FixedDecimalFormatter`](crate::FixedDecimalFormatter), [`GroupingStrategy::Always`] |
56 | | /// has the same behavior as [`GroupingStrategy::Auto`]. |
57 | | Always, |
58 | | |
59 | | /// Render grouping separators only if there are at least 2 digits before the final grouping |
60 | | /// separator. In most locales, this means that numbers between 1000 and 9999 do not get |
61 | | /// grouping separators, but numbers 10,000 and above will. |
62 | | Min2, |
63 | | } |
64 | | |
65 | | impl Default for GroupingStrategy { |
66 | 0 | fn default() -> Self { |
67 | 0 | Self::Auto |
68 | 0 | } |
69 | | } |