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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::formatting::prefix::Prefix;
use crate::formatting::unit::Unit;

use std::borrow::Cow;
use std::ops::RangeInclusive;

use super::*;

const DEFAULT_NUMBER_WIDTH: usize = 2;

pub const DEFAULT_NUMBER_FORMATTER: EngFormatter = EngFormatter {
    width: DEFAULT_NUMBER_WIDTH,
    unit: None,
    unit_has_space: false,
    unit_hidden: false,
    prefix: None,
    prefix_has_space: false,
    prefix_hidden: false,
    prefix_forced: false,
    pad_with: DEFAULT_NUMBER_PAD_WITH,
    range: f64::NEG_INFINITY..=f64::INFINITY,
};

#[derive(Debug)]
pub struct EngFormatter {
    width: usize,
    unit: Option<Unit>,
    unit_has_space: bool,
    unit_hidden: bool,
    prefix: Option<Prefix>,
    prefix_has_space: bool,
    prefix_hidden: bool,
    prefix_forced: bool,
    pad_with: PadWith,
    range: RangeInclusive<f64>,
}

impl EngFormatter {
    pub(super) fn from_args(args: &[Arg]) -> Result<Self> {
        let mut result = DEFAULT_NUMBER_FORMATTER;

        for arg in args {
            match arg.key {
                "width" | "w" => {
                    result.width = arg.val.parse().error("Width must be a positive integer")?;
                }
                "unit" | "u" => {
                    result.unit = Some(arg.val.parse()?);
                }
                "hide_unit" => {
                    result.unit_hidden = arg
                        .val
                        .parse()
                        .ok()
                        .error("hide_unit must be true or false")?;
                }
                "unit_space" => {
                    result.unit_has_space = arg
                        .val
                        .parse()
                        .ok()
                        .error("unit_space must be true or false")?;
                }
                "prefix" | "p" => {
                    result.prefix = Some(arg.val.parse()?);
                }
                "hide_prefix" => {
                    result.prefix_hidden = arg
                        .val
                        .parse()
                        .ok()
                        .error("hide_prefix must be true or false")?;
                }
                "prefix_space" => {
                    result.prefix_has_space = arg
                        .val
                        .parse()
                        .ok()
                        .error("prefix_space must be true or false")?;
                }
                "force_prefix" => {
                    result.prefix_forced = arg
                        .val
                        .parse()
                        .ok()
                        .error("force_prefix must be true or false")?;
                }
                "pad_with" => {
                    if arg.val.graphemes(true).count() < 2 {
                        result.pad_with = Cow::Owned(arg.val.into());
                    } else {
                        return Err(Error::new(
                            "pad_with must be an empty string or a single character",
                        ));
                    }
                }
                "range" => {
                    let (start, end) = arg.val.split_once("..").error("invalid range")?;
                    if !start.is_empty() {
                        result.range = start.parse::<f64>().error("invalid range start")?
                            ..=*result.range.end();
                    }
                    if !end.is_empty() {
                        result.range = *result.range.start()
                            ..=end.parse::<f64>().error("invalid range end")?;
                    }
                }
                other => {
                    return Err(Error::new(format!("Unknown argument for 'eng': '{other}'")));
                }
            }
        }

        Ok(result)
    }
}

impl Formatter for EngFormatter {
    fn format(&self, val: &Value, _config: &SharedConfig) -> Result<String, FormatError> {
        match val {
            Value::Number { mut val, mut unit } => {
                if !self.range.contains(&val) {
                    return Err(FormatError::NumberOutOfRange(val));
                }

                let is_negative = val.is_sign_negative();
                if is_negative {
                    val = -val;
                }

                if let Some(new_unit) = self.unit {
                    val = unit.convert(val, new_unit)?;
                    unit = new_unit;
                }

                let (min_prefix, max_prefix) = match (self.prefix, self.prefix_forced) {
                    (Some(prefix), true) => (prefix, prefix),
                    (Some(prefix), false) => (prefix, Prefix::max_available()),
                    (None, _) => (Prefix::min_available(), Prefix::max_available()),
                };

                let prefix = unit
                    .clamp_prefix(if min_prefix.is_binary() {
                        Prefix::eng_binary(val)
                    } else {
                        Prefix::eng(val)
                    })
                    .clamp(min_prefix, max_prefix);
                val = prefix.apply(val);

                let mut digits = (val.max(1.).log10().floor() + 1.0) as i32 + is_negative as i32;

                // handle rounding
                if self.width as i32 - digits >= 1 {
                    let round_up_to = self.width as i32 - digits - 1;
                    let m = 10f64.powi(round_up_to);
                    val = (val * m).round() / m;
                    digits = (val.max(1.).log10().floor() + 1.0) as i32 + is_negative as i32;
                }

                let sign = if is_negative { "-" } else { "" };
                let mut retval = match self.width as i32 - digits {
                    i32::MIN..=0 => format!("{sign}{}", val.round()),
                    1 => format!("{}{sign}{}", self.pad_with, val.round() as i64),
                    rest => format!("{sign}{val:.*}", rest as usize - 1),
                };

                let display_prefix =
                    !self.prefix_hidden && prefix != Prefix::One && prefix != Prefix::OneButBinary;
                let display_unit = !self.unit_hidden && unit != Unit::None;

                if display_prefix {
                    if self.prefix_has_space {
                        retval.push(' ');
                    }
                    retval.push_str(&prefix.to_string());
                }
                if display_unit {
                    if self.unit_has_space || (self.prefix_has_space && !display_prefix) {
                        retval.push(' ');
                    }
                    retval.push_str(&unit.to_string());
                }

                Ok(retval)
            }
            other => Err(FormatError::IncompatibleFormatter {
                ty: other.type_name(),
                fmt: "eng",
            }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn eng_rounding_and_negatives() {
        let fmt = new_fmt!(eng, w: 3).unwrap();
        let config = SharedConfig::default();

        let result = fmt
            .format(
                &Value::Number {
                    val: -1.0,
                    unit: Unit::None,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, " -1");

        let result = fmt
            .format(
                &Value::Number {
                    val: 9.9999,
                    unit: Unit::None,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, " 10");

        let result = fmt
            .format(
                &Value::Number {
                    val: 999.9,
                    unit: Unit::Bytes,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, "1.0KB");

        let result = fmt
            .format(
                &Value::Number {
                    val: -9.99,
                    unit: Unit::None,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, "-10");

        let result = fmt
            .format(
                &Value::Number {
                    val: 9.94,
                    unit: Unit::None,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, "9.9");

        let result = fmt
            .format(
                &Value::Number {
                    val: 9.95,
                    unit: Unit::None,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, " 10");

        let fmt = new_fmt!(eng, w: 5, p: 1).unwrap();
        let result = fmt
            .format(
                &Value::Number {
                    val: 321_600_000_000.,
                    unit: Unit::Bytes,
                },
                &config,
            )
            .unwrap();
        assert_eq!(result, "321.6GB");
    }

    #[test]
    fn eng_prefixes() {
        let config = SharedConfig::default();
        // 14.96 GiB
        let val = Value::Number {
            val: 14.96 * 1024. * 1024. * 1024.,
            unit: Unit::Bytes,
        };

        let fmt = new_fmt!(eng, w: 5, p: Mi).unwrap();
        let result = fmt.format(&val, &config).unwrap();
        assert_eq!(result, "14.96GiB");

        let fmt = new_fmt!(eng, w: 4, p: Mi).unwrap();
        let result = fmt.format(&val, &config).unwrap();
        assert_eq!(result, "15.0GiB");

        let fmt = new_fmt!(eng, w: 3, p: Mi).unwrap();
        let result = fmt.format(&val, &config).unwrap();
        assert_eq!(result, " 15GiB");

        let fmt = new_fmt!(eng, w: 2, p: Mi).unwrap();
        let result = fmt.format(&val, &config).unwrap();
        assert_eq!(result, "15GiB");
    }
}