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
use crate::strings::{
    DecString, DecimalStr, InfString, InfinityStr, MinString, MinusSignStr, NanStr, NanString,
    PlusSignStr, PlusString, SepString, SeparatorStr,
};
use crate::{CustomFormatBuilder, Format, Grouping, Locale};

/// Type for representing your own custom formats. Implements [`Format`].
///
/// # Example
/// ```rust
/// use num_format::{Buffer, Error, CustomFormat, Grouping};
///
/// fn main() -> Result<(), Error> {
///     let format = CustomFormat::builder()
///         .grouping(Grouping::Indian)
///         .minus_sign("🙌")
///         .separator("😀")
///         .build()?;
///
///     let mut buf = Buffer::new();
///     buf.write_formatted(&(-1000000), &format);
///     assert_eq!("🙌10😀00😀000", buf.as_str());
///
///     Ok(())
/// }
/// ```
///
/// [`Format`]: trait.Format.html
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct CustomFormat {
    pub(crate) dec: DecString,
    pub(crate) grp: Grouping,
    pub(crate) inf: InfString,
    pub(crate) min: MinString,
    pub(crate) nan: NanString,
    pub(crate) plus: PlusString,
    pub(crate) sep: SepString,
}

impl CustomFormat {
    /// Constructs a [`CustomFormatBuilder`].
    ///
    /// [`CustomFormatBuilder`]: struct.CustomFormatBuilder.html
    pub fn builder() -> CustomFormatBuilder {
        CustomFormatBuilder::new()
    }

    /// Turns `self` into a [`CustomFormatBuilder`].
    ///
    /// [`CustomFormatBuilder`]: struct.CustomFormatBuilder.html
    pub fn into_builder(self) -> CustomFormatBuilder {
        self.into()
    }

    /// Returns this format's representation of decimal points.
    pub fn decimal(&self) -> &str {
        &self.dec
    }

    /// Returns this format's [`Grouping`], which governs how digits are separated (see [`Grouping`]).
    ///
    /// [`Grouping`]: enum.Grouping.html
    pub fn grouping(&self) -> Grouping {
        self.grp
    }

    /// Returns this format's representation of infinity.
    pub fn infinity(&self) -> &str {
        &self.inf
    }

    /// Returns this format's representation of minus signs.
    pub fn minus_sign(&self) -> &str {
        &self.min
    }

    /// Returns this format's representation of NaN.
    pub fn nan(&self) -> &str {
        &self.nan
    }

    /// Returns this format's representation of plus signs.
    pub fn plus_sign(&self) -> &str {
        &self.plus
    }

    /// Returns this format's representation of separators.
    pub fn separator(&self) -> &str {
        &self.sep
    }
}

impl Default for CustomFormat {
    /// Returns a `CustomFormat` with settings equal to `Locale::en`.
    fn default() -> Self {
        Locale::en.into()
    }
}

impl Format for CustomFormat {
    #[inline(always)]
    fn decimal(&self) -> DecimalStr<'_> {
        DecimalStr::new(self.decimal()).unwrap()
    }

    #[inline(always)]
    fn grouping(&self) -> Grouping {
        self.grouping()
    }

    #[inline(always)]
    fn infinity(&self) -> InfinityStr<'_> {
        InfinityStr::new(self.infinity()).unwrap()
    }

    #[inline(always)]
    fn minus_sign(&self) -> MinusSignStr<'_> {
        MinusSignStr::new(self.minus_sign()).unwrap()
    }

    #[inline(always)]
    fn nan(&self) -> NanStr<'_> {
        NanStr::new(self.nan()).unwrap()
    }

    #[inline(always)]
    fn plus_sign(&self) -> PlusSignStr<'_> {
        PlusSignStr::new(self.plus_sign()).unwrap()
    }

    #[inline(always)]
    fn separator(&self) -> SeparatorStr<'_> {
        SeparatorStr::new(self.separator()).unwrap()
    }
}

impl From<Locale> for CustomFormat {
    fn from(locale: Locale) -> Self {
        Self {
            dec: DecString::new(locale.decimal()).unwrap(),
            grp: locale.grouping(),
            inf: InfString::new(locale.infinity()).unwrap(),
            min: MinString::new(locale.minus_sign()).unwrap(),
            nan: NanString::new(locale.nan()).unwrap(),
            plus: PlusString::new(locale.plus_sign()).unwrap(),
            sep: SepString::new(locale.separator()).unwrap(),
        }
    }
}

#[cfg(all(feature = "with-system-locale", any(unix, windows)))]
mod system {
    use super::*;
    use crate::SystemLocale;

    impl From<SystemLocale> for CustomFormat {
        fn from(locale: SystemLocale) -> Self {
            Self {
                dec: DecString::new(locale.decimal()).unwrap(),
                grp: locale.grouping(),
                inf: InfString::new(locale.infinity()).unwrap(),
                min: MinString::new(locale.minus_sign()).unwrap(),
                nan: NanString::new(locale.nan()).unwrap(),
                plus: PlusString::new(locale.plus_sign()).unwrap(),
                sep: SepString::new(locale.separator()).unwrap(),
            }
        }
    }
}

#[cfg(all(test, feature = "with-serde"))]
mod tests {
    use super::*;

    #[test]
    fn test_serialization() {
        let locale = CustomFormat::builder().build().unwrap();
        let s = serde_json::to_string(&locale).unwrap();
        let expected =
            r#"{"dec":".","grp":"Standard","inf":"∞","min":"-","nan":"NaN","plus":"+","sep":","}"#;
        assert_eq!(expected, &s);
    }
}