etop_format/number_format/
builder.rs

1use super::types::{FormatType, NumberAlign, NumberFormat, Sign, Timezone};
2
3impl NumberFormat {
4    /// create new number format
5    pub fn new() -> NumberFormat {
6        NumberFormat::default()
7    }
8
9    // zero padding
10
11    /// add zero padding
12    pub fn zero_padding(mut self) -> NumberFormat {
13        self.zero_padding = true;
14        self
15    }
16
17    /// remove zero padding
18    pub fn no_zero_padding(mut self) -> NumberFormat {
19        self.zero_padding = false;
20        self
21    }
22
23    // fill
24
25    /// set fill char
26    pub fn fill(mut self, fill_char: char) -> NumberFormat {
27        self.fill = fill_char;
28        self
29    }
30
31    // align
32
33    /// left align
34    pub fn left_align(mut self) -> NumberFormat {
35        self.align = NumberAlign::Left;
36        self
37    }
38
39    /// right align
40    pub fn right_align(mut self) -> NumberFormat {
41        self.align = NumberAlign::Right;
42        self
43    }
44
45    /// center align
46    pub fn center_align(mut self) -> NumberFormat {
47        self.align = NumberAlign::Center;
48        self
49    }
50
51    /// signed right align
52    pub fn left_sign_right_align(mut self) -> NumberFormat {
53        self.align = NumberAlign::SignedRight;
54        self
55    }
56
57    // sign
58
59    /// always add sign
60    pub fn unsigned(mut self) -> NumberFormat {
61        self.sign = Sign::OnlyNegative;
62        self
63    }
64
65    /// always add sign
66    pub fn signed(mut self) -> NumberFormat {
67        self.sign = Sign::Always;
68        self
69    }
70
71    /// unsigned but with space
72    pub fn unsigned_space(mut self) -> NumberFormat {
73        self.sign = Sign::SpaceOrDash;
74        self
75    }
76
77    // type_prefix
78
79    /// add type prefix
80    pub fn type_prefix(mut self) -> NumberFormat {
81        self.type_prefix = true;
82        self
83    }
84
85    /// no type prefix
86    pub fn no_type_prefix(mut self) -> NumberFormat {
87        self.type_prefix = false;
88        self
89    }
90
91    // width
92
93    /// set width
94    pub fn width(mut self, width: usize) -> NumberFormat {
95        self.min_width = width;
96        self.max_width = width;
97        self
98    }
99
100    /// set min_width
101    pub fn min_width(mut self, min_width: usize) -> NumberFormat {
102        self.min_width = min_width;
103        self
104    }
105
106    /// set max_width
107    pub fn max_width(mut self, max_width: usize) -> NumberFormat {
108        self.max_width = max_width;
109        self
110    }
111
112    /// set width
113    pub fn width_option(self, width: Option<usize>) -> NumberFormat {
114        self.min_width_option(width).max_width_option(width)
115    }
116
117    /// set min_width
118    pub fn min_width_option(mut self, width: Option<usize>) -> NumberFormat {
119        match width {
120            Some(width) => {
121                self.min_width = width;
122                self
123            }
124            None => self,
125        }
126    }
127
128    /// set max_width
129    pub fn max_width_option(mut self, width: Option<usize>) -> NumberFormat {
130        match width {
131            Some(width) => {
132                self.max_width = width;
133                self
134            }
135            None => self,
136        }
137    }
138
139    // commas
140
141    /// show commas
142    pub fn commas(mut self) -> NumberFormat {
143        self.commas = true;
144        self
145    }
146
147    /// do not show commas
148    pub fn no_commas(mut self) -> NumberFormat {
149        self.commas = false;
150        self
151    }
152
153    // precision
154
155    /// set precision
156    pub fn precision(mut self, precision: usize) -> NumberFormat {
157        self.precision = precision;
158        self
159    }
160
161    // timezone
162
163    /// use local timezone
164    pub fn timezone_local(mut self) -> NumberFormat {
165        self.timezone = Timezone::Local;
166        self
167    }
168
169    /// use utc timezone
170    pub fn timezone_utc(mut self) -> NumberFormat {
171        self.timezone = Timezone::Utc;
172        self
173    }
174
175    // format_type
176
177    /// format as scientific notation
178    pub fn scientific_notation(mut self) -> NumberFormat {
179        self.format_type = FormatType::Exponent;
180        self
181    }
182
183    /// format as SI (order of magnitude)
184    pub fn si(mut self) -> NumberFormat {
185        self.format_type = FormatType::SI;
186        self
187    }
188
189    /// format as pecentage
190    pub fn percentage(mut self) -> NumberFormat {
191        self.format_type = FormatType::Percentage;
192        self
193    }
194
195    /// format as binary
196    pub fn binary(mut self) -> NumberFormat {
197        self.format_type = FormatType::Binary;
198        self
199    }
200
201    /// format as octal
202    pub fn octal(mut self) -> NumberFormat {
203        self.format_type = FormatType::Octal;
204        self
205    }
206
207    /// format as hex
208    pub fn hex(mut self) -> NumberFormat {
209        self.format_type = FormatType::Octal;
210        self
211    }
212
213    /// format as integer order of magnitude
214    pub fn integer_oom(mut self) -> NumberFormat {
215        self.format_type = FormatType::IntegerOrderOfMagnitude;
216        self
217    }
218
219    /// format as float order of magnitude
220    pub fn float_oom(mut self) -> NumberFormat {
221        self.format_type = FormatType::FloatOrderOfMagnitude;
222        self
223    }
224
225    /// format as float order of magnitude
226    pub fn timestamp(mut self) -> NumberFormat {
227        self.format_type = FormatType::TimestampPretty;
228        self
229    }
230
231    /// set format type
232    pub fn format_type(mut self, format_type: &FormatType) -> NumberFormat {
233        self.format_type = format_type.clone();
234        self
235    }
236}