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
#[derive(Debug, Clone)]
pub struct CurrencyOpts<'a> {
    symbol: &'a str,
    separator: &'a str,
    decimal: &'a str,
    precision: f64,
    pattern: &'a str,
    negative_pattern: &'a str,
    from_cents: bool,
    increment: Option<f64>,
    use_vedic: bool,
    error_on_invalid: bool,
}

impl Default for CurrencyOpts<'_> {
    fn default() -> Self {
        Self {
            symbol: "$".into(),
            separator: ",".into(),
            decimal: ".".into(),
            precision: 2.,
            pattern: "!#".into(),
            negative_pattern: "-!#".into(),
            from_cents: false,
            increment: None,
            use_vedic: false,
            error_on_invalid: false,
        }
    }
}

impl CurrencyOpts<'_> {
    pub fn new() -> Self {
        Self::default()
    }

    /// `pow` takes a `f64` and returns a `f64`
    ///
    /// Arguments:
    ///
    /// * `p`: The power to raise 10 to.
    ///
    /// Returns:
    ///
    /// The value of 10 to the power of p.
    fn pow(p: f64) -> f64 {
        10_f64.powf(p)
    }
}

// SETTERS
impl<'a> CurrencyOpts<'a> {
    pub fn set_symbol(
        mut self,
        symbol: &'a str,
    ) -> Self {
        self.symbol = symbol;
        self
    }

    pub fn set_separator(
        mut self,
        separator: &'a str,
    ) -> Self {
        self.separator = separator;
        self
    }

    pub fn set_decimal(
        mut self,
        decimal: &'a str,
    ) -> Self {
        self.decimal = decimal;
        self
    }

    pub fn set_precision(
        mut self,
        precision: i64,
    ) -> Self {
        self.precision = precision as f64;
        self
    }

    pub fn set_pattern(
        mut self,
        pattern: &'a str,
    ) -> Self {
        self.pattern = pattern;
        self
    }

    pub fn set_negative_pattern(
        mut self,
        negative_pattern: &'a str,
    ) -> Self {
        self.negative_pattern = negative_pattern;
        self
    }

    pub fn set_from_cents(
        mut self,
        from_cents: bool,
    ) -> Self {
        self.from_cents = from_cents;
        self
    }

    pub fn set_increment(
        mut self,
        increment: f64,
    ) -> Self {
        self.increment = Some(increment);
        self
    }

    pub fn set_unset_increment(mut self) -> Self {
        self.increment = None;
        self
    }

    pub fn set_use_vedic(
        mut self,
        use_vedic: bool,
    ) -> Self {
        self.use_vedic = use_vedic;
        self
    }

    pub fn set_error_on_invalid(
        mut self,
        error_on_invalid: bool,
    ) -> Self {
        self.error_on_invalid = error_on_invalid;
        self
    }
}

// GETTERS
impl CurrencyOpts<'_> {
    pub fn symbol(&self) -> &str {
        self.symbol
    }

    pub fn separator(&self) -> &str {
        self.separator
    }

    pub fn decimal(&self) -> &str {
        self.decimal
    }

    pub fn precision(&self) -> f64 {
        self.precision
    }

    pub fn pattern(&self) -> &str {
        self.pattern
    }

    pub fn negative_pattern(&self) -> &str {
        self.negative_pattern
    }

    pub fn from_cents(&self) -> bool {
        self.from_cents
    }

    pub fn increment(&self) -> f64 {
        if let Some(inc) = self.increment {
            inc
        } else {
            1. / Self::pow(self.precision)
        }
    }

    pub fn use_vedic(&self) -> bool {
        self.use_vedic
    }

    pub fn error_on_invalid(&self) -> bool {
        self.error_on_invalid
    }
}