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
use arrayvec::ArrayString;

use crate::constants::{MAX_INF_LEN, MAX_MIN_LEN, MAX_NAN_LEN};
use crate::errors::Error;
use crate::format::{CustomFormat, Format, Grouping, Locale};

/// Type for building [`CustomFormat`]s.
///
/// [`CustomFormat`]: struct.CustomFormat.html
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
pub struct CustomFormatBuilder {
    dec: char,
    grp: Grouping,
    inf: Result<ArrayString<[u8; MAX_INF_LEN]>, Error>,
    min: Result<ArrayString<[u8; MAX_MIN_LEN]>, Error>,
    nan: Result<ArrayString<[u8; MAX_NAN_LEN]>, Error>,
    sep: Option<char>,
}

impl CustomFormatBuilder {
    pub(crate) fn new() -> Self {
        Self {
            dec: Locale::en.decimal(),
            grp: Locale::en.grouping(),
            inf: ArrayString::from(Locale::en.infinity()).map_err(|_| Error::capacity(MAX_INF_LEN)),
            min: ArrayString::from(Locale::en.minus_sign())
                .map_err(|_| Error::capacity(MAX_MIN_LEN)),
            nan: ArrayString::from(Locale::en.nan()).map_err(|_| Error::capacity(MAX_NAN_LEN)),
            sep: Locale::en.separator(),
        }
    }

    /// Construct a [`CustomFormat`].
    ///
    /// # Errors
    ///
    /// Return an error if:
    /// - The "infinity sign" is longer than 64 bytes
    /// - The "minus sign" is longer than 7 bytes
    /// - The "nan symbol" is longer than 64 bytes
    ///
    /// [`CustomFormat`]: struct.CustomFormat.html
    pub fn build(self) -> Result<CustomFormat, Error> {
        Ok(CustomFormat {
            dec: self.dec,
            grp: self.grp,
            inf: self.inf?,
            min: self.min?,
            nan: self.nan?,
            sep: self.sep,
        })
    }

    /// Sets the character used to represent decimal points.
    pub fn decimal(mut self, value: char) -> Self {
        self.dec = value;
        self
    }

    /// Sets the decimal, grouping, infinity, minus sign, nan, and separator representations
    /// according to the provided format.
    pub fn format<F>(mut self, value: &F) -> Self
    where
        F: Format,
    {
        self.dec = value.decimal();
        self.grp = value.grouping();
        self.inf = ArrayString::from(value.infinity().into_str())
            .map_err(|_| Error::capacity(MAX_INF_LEN));
        self.min = ArrayString::from(value.minus_sign().into_str())
            .map_err(|_| Error::capacity(MAX_MIN_LEN));
        self.nan =
            ArrayString::from(value.nan().into_str()).map_err(|_| Error::capacity(MAX_NAN_LEN));
        self.sep = value.separator();
        self
    }

    /// Sets the [`Grouping`] used to separate digits.
    ///
    /// [`Grouping`]: enum.Grouping.html
    pub fn grouping(mut self, value: Grouping) -> Self {
        self.grp = value;
        self
    }

    /// Sets the string used for infinity. Note: If the length is greater than 64 bytes
    /// [`build`] will return an error (see [`build`]).
    ///
    /// [`build`]: struct.CustomFormatBuilder.html#method.build
    pub fn infinity<S>(mut self, value: S) -> Self
    where
        S: AsRef<str>,
    {
        let s = value.as_ref();
        self.inf = ArrayString::from(s).map_err(|_| Error::capacity(MAX_INF_LEN));
        self
    }

    /// Sets the string used for minus signs. Note: If the length is greater than 7 bytes
    /// [`build`] will return an error (see [`build`]).
    ///
    /// [`build`]: struct.CustomFormatBuilder.html#method.build
    pub fn minus_sign<S>(mut self, value: S) -> Self
    where
        S: AsRef<str>,
    {
        let s = value.as_ref();
        self.min = ArrayString::from(s).map_err(|_| Error::capacity(MAX_MIN_LEN));
        self
    }

    /// Sets the string used for NaN. Note: If the length is greater than 64 bytes
    /// [`build`] will return an error (see [`build`]).
    ///
    /// [`build`]: struct.CustomFormatBuilder.html#method.build
    pub fn nan<S>(mut self, value: S) -> Self
    where
        S: AsRef<str>,
    {
        let s = value.as_ref();
        self.nan = ArrayString::from(s).map_err(|_| Error::capacity(MAX_NAN_LEN));
        self
    }

    /// Sets the character, if any, used to represent separtors.
    pub fn separator(mut self, value: Option<char>) -> Self {
        self.sep = value;
        self
    }
}

impl From<CustomFormat> for CustomFormatBuilder {
    fn from(format: CustomFormat) -> CustomFormatBuilder {
        CustomFormatBuilder {
            dec: format.dec,
            grp: format.grp,
            inf: Ok(format.inf),
            min: Ok(format.min),
            nan: Ok(format.nan),
            sep: format.sep,
        }
    }
}