Debian Codegraph @

debian/rust-vendor/icu_decimal/src/options.rs

raw
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 [`DecimalFormatter`](crate::DecimalFormatter).
6
7 /// A bag of options defining how numbers will be formatted by
8 /// [`DecimalFormatter`](crate::DecimalFormatter).
9 #[derive(Debug, Eq, PartialEq, Clone, Copy, Default, Hash)]
10 #[non_exhaustive]
11 pub struct DecimalFormatterOptions {
12 /// When to render grouping separators.
13 ///
14 /// Default is [`GroupingStrategy::Auto`]
15 pub grouping_strategy: Option<GroupingStrategy>,
16 }
17
18 impl From<GroupingStrategy> for DecimalFormatterOptions {
19 fn from(grouping_strategy: GroupingStrategy) -> Self {
20 Self {
21 grouping_strategy: Some(grouping_strategy),
22 }
23 }
24 }
25
26 /// Configuration for how often to render grouping separators.
27 ///
28 /// # Examples
29 ///
30 /// ```
31 /// use icu::decimal::DecimalFormatter;
32 /// use icu::decimal::options;
33 /// use icu::locale::Locale;
34 /// use writeable::assert_writeable_eq;
35 ///
36 /// let locale = Default::default();
37 /// let mut options: options::DecimalFormatterOptions = Default::default();
38 /// options.grouping_strategy = Some(options::GroupingStrategy::Min2);
39 /// let formatter = DecimalFormatter::try_new(locale, options)
40 /// .expect("locale should be present");
41 ///
42 /// let one_thousand = 1000.into();
43 /// assert_writeable_eq!(formatter.format(&one_thousand), "1000");
44 ///
45 /// let ten_thousand = 10000.into();
46 /// assert_writeable_eq!(formatter.format(&ten_thousand), "10,000");
47 /// ```
48 #[non_exhaustive]
49 #[derive(Debug, Eq, PartialEq, Clone, Copy, Hash, Default)]
50 pub enum GroupingStrategy {
51 /// Render grouping separators according to locale preferences.
52 #[default]
53 Auto,
54
55 /// Never render grouping separators.
56 Never,
57
58 /// Always render grouping separators.
59 ///
60 /// For [`DecimalFormatter`](crate::DecimalFormatter), [`GroupingStrategy::Always`]
61 /// has the same behavior as [`GroupingStrategy::Auto`].
62 Always,
63
64 /// Render grouping separators only if there are at least 2 digits before the final grouping
65 /// separator. In most locales, this means that numbers between 1000 and 9999 do not get
66 /// grouping separators, but numbers 10,000 and above will.
67 Min2,
68 }
69
70 /// A bag of options defining how numbers will be formatted by
71 /// [`CompactDecimalFormatter`](super::CompactDecimalFormatter).
72 ///
73 /// <div class="stab unstable">
74 /// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
75 /// including in SemVer minor releases. Do not use this type unless you are prepared for things to occasionally break.
76 ///
77 /// Graduation tracking issue: [issue #7161](https://github.com/unicode-org/icu4x/issues/7161).
78 /// </div>
79 ///
80 /// ✨ *Enabled with the `unstable` Cargo feature.*
81 #[cfg(feature = "unstable")]
82 #[derive(Debug, Eq, PartialEq, Clone)]
83 #[non_exhaustive]
84 pub struct CompactDecimalFormatterOptions {
85 /// When to render grouping separators.
86 ///
87 /// Default is [`GroupingStrategy::Min2`]
88 pub grouping_strategy: Option<GroupingStrategy>,
89 }
90
91 #[cfg(feature = "unstable")]
92 impl Default for CompactDecimalFormatterOptions {
93 fn default() -> Self {
94 GroupingStrategy::Min2.into()
95 }
96 }
97
98 #[cfg(feature = "unstable")]
99 impl From<DecimalFormatterOptions> for CompactDecimalFormatterOptions {
100 fn from(decimal_formatter_options: DecimalFormatterOptions) -> Self {
101 Self {
102 grouping_strategy: decimal_formatter_options.grouping_strategy,
103 }
104 }
105 }
106
107 #[cfg(feature = "unstable")]
108 impl From<CompactDecimalFormatterOptions> for DecimalFormatterOptions {
109 fn from(decimal_formatter_options: CompactDecimalFormatterOptions) -> Self {
110 Self {
111 grouping_strategy: decimal_formatter_options.grouping_strategy,
112 }
113 }
114 }
115
116 #[cfg(feature = "unstable")]
117 impl From<GroupingStrategy> for CompactDecimalFormatterOptions {
118 fn from(grouping_strategy: GroupingStrategy) -> Self {
119 Self {
120 grouping_strategy: Some(grouping_strategy),
121 }
122 }
123 }