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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
MIT License

Copyright (c) 2020 Philipp Schuster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//! Formats a list of arbitrary fractional numbers (either string or f32/f64) so that they are
//! correctly aligned when printed line by line. It also removes unnecessary zeroes. This means
//! that it rather returns "7" instead of "7.000000".
//!
//! ## Example Input
//! ```text
//! "-42"
//! "0.3214"
//! "1000"
//! "-1000.2"
//! "2.00000"
//! ```
//! ## Example Output
//! ```text
//! "  -42     "
//! "    0.3214"
//! " 1000     "
//! "-1000.2   "
//! "    2     "

#![deny(
    clippy::all,
    clippy::cargo,
    clippy::nursery,
    // clippy::restriction,
    // clippy::pedantic
)]
// now allow a few rules which are denied by the above statement
// --> they are ridiculous and not necessary
#![allow(
    clippy::fallible_impl_from,
    clippy::needless_doctest_main,
    clippy::redundant_pub_crate,
    clippy::suboptimal_flops
)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(rustdoc::all)]

/// Abstraction over floating point types [`f32`] and [`f64`].
#[derive(Debug, Copy, Clone)]
pub enum FractionNumber {
    /// Variant for [`f32`].
    F32(f32),
    /// Variant for [`f64`].
    F64(f64),
}

impl From<f32> for FractionNumber {
    fn from(val: f32) -> Self {
        Self::F32(val)
    }
}

impl From<f64> for FractionNumber {
    fn from(val: f64) -> Self {
        Self::F64(val)
    }
}

impl FractionNumber {
    fn format(self, precision: FormatPrecision) -> String {
        match self {
            Self::F32(val) => {
                format!("{val:.precision$}", val = val, precision = precision.val())
            }
            Self::F64(val) => {
                format!("{val:.precision$}", val = val, precision = precision.val())
            }
        }
    }
}

/// The precision of decimal places for [`fmt_align_fractions`].
#[derive(Copy, Clone, Debug)]
pub enum FormatPrecision {
    /// Format with exactly `n` decimal places.
    Exact(u8),
    /// Format with a maximum of `n` decimal places. Might happen that there is not a
    /// single decimal place required.
    Max(u8),
}

impl FormatPrecision {
    const fn val(self) -> usize {
        let val = match self {
            Self::Exact(val) => val,
            Self::Max(val) => val,
        };
        val as usize
    }
}

/// Convenient wrapper around [`fmt_align_fraction_strings`] that takes
/// a slice of floating point values, formats them all with a maximum
/// precision and returns a list of aligned, formatted strings.
/// * `precision`: See [`FormatPrecision`].
pub fn fmt_align_fractions(
    fractions: &[FractionNumber],
    precision: FormatPrecision,
) -> Vec<String> {
    let fraction_strings = fractions
        .iter()
        .map(|fr| fr.format(precision))
        .collect::<Vec<String>>();

    let str_vec = fraction_strings
        .iter()
        .map(|s| s.as_str())
        .collect::<Vec<&str>>();

    fmt_align_fraction_strings(&str_vec)
}

/// Aligns a number of formatted fraction numbers. Valid strings are for example
/// `1`, `3.14`, and `-42`. Aligns all with additional padding on the left so that
/// all of them can be printed line by line in an aligned way. This means that
/// in every line the tens digits will be aligned, the once places will be aligned,
/// the decimal place will be aligned etc. (TODO are these the proper english terms?)
///
/// This takes the precision from the longest fractional part (without unnecessary zeroes)
/// to align all strings.
///
/// ## Example Input
/// ```text
/// "-42"
/// "0.3214"
/// "1000"
/// "-1000.2"
/// "2.00000"
/// ```
/// ## Example Output
/// ```text
/// "  -42     "
/// "    0.3214"
/// " 1000     "
/// "-1000.2   "
/// "    2     "
/// ```
pub fn fmt_align_fraction_strings(strings: &[&str]) -> Vec<String> {
    // normalize all fractional parts
    let strings = strings
        .iter()
        .map(|x| normalize_fraction_part(x))
        .collect::<Vec<&str>>();

    let max = strings
        .iter()
        .map(|x| get_whole_part(x))
        .map(|x| x.len())
        .max()
        .unwrap();

    // create n new strings
    let mut new_strings = vec![String::new(); strings.len()];
    strings.iter().enumerate().for_each(|(index, string)| {
        let whole_part = get_whole_part(string);
        let spaces = max - whole_part.len();
        new_strings[index].push_str(&" ".repeat(spaces));
        new_strings[index].push_str(string);
    });

    // now add spaces in the end so that all are exactly same aligned, on left
    // as well as right; technically this is not really needed, but it may
    // help in some situations. Also this can be easily revoked with a right trim.
    let max = new_strings.iter().map(|s| s.len()).max().unwrap();
    for string in &mut new_strings {
        let spaces = max - string.len();
        string.push_str(&" ".repeat(spaces))
    }

    new_strings
}

/// Get the whole part (TODO is this the right term?)
/// from a formatted fraction number string.
/// * `123` => `123`
/// * `123.13` => `123`
/// * `0.1234` => `0`
/// * `-10.1234` => `-10`
fn get_whole_part(string: &str) -> &str {
    // if it doesn't contain "." the whole thing is returned
    string.split('.').next().unwrap()
}

/// Get the fractional part from a formatted fraction number string.
/// * `123` => `None`
/// * `123.13` => `Some(13)`
/// * `0.1234` => `Some(1234)`
/// * `-10.1234` => `Some(1234)`
fn get_fractional_part(string: &str) -> Option<&str> {
    let mut split = string.split('.');
    let _whole_part = split.next().unwrap();
    split.next()
}

/// Consumes the whole number string and normalizes
/// (if present) the fraction part. This means:
/// * `123` => `123`
/// * `123.13` => `123.13`
/// * `0.1234000` => `0.1234`
/// * `-10.000000` => `-10`
fn normalize_fraction_part(string: &str) -> &str {
    let whole_part = get_whole_part(string);
    let fraction_part = get_fractional_part(string);
    if fraction_part.is_none() {
        return whole_part;
    }
    let fraction_part = fraction_part.unwrap();
    let zeroes = fraction_part_count_zeroes(fraction_part);
    if fraction_part.len() == zeroes {
        whole_part
    } else {
        &string[0..string.len() - zeroes]
    }
}

/// Takes only the fraction part of a string without ".".
/// Counts that in "123000" (fractional part of "0.123000") are three unnecessary zeroes.
/// In "0.0000" there are four unnecessary zeroes.
fn fraction_part_count_zeroes(fraction_part: &str) -> usize {
    let mut zeroes = 0;
    let chars = fraction_part.chars().collect::<Vec<char>>();
    for i in 0..fraction_part.len() {
        // go backwards
        let i = fraction_part.len() - 1 - i;
        let char = chars[i];
        if char == '0' {
            zeroes += 1;
        } else {
            break;
        }
    }
    zeroes
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_fraction_part_count_zeroes() {
        assert_eq!(3, fraction_part_count_zeroes("123000"));
        assert_eq!(0, fraction_part_count_zeroes("123"));
        assert_eq!(1, fraction_part_count_zeroes("0"));
        assert_eq!(11, fraction_part_count_zeroes("00000012800000000000"));
    }

    #[test]
    fn test_fmt_align_fraction_strings() {
        let res = fmt_align_fraction_strings(
            &vec!["-42", "0.3214", "1000", "-1000.2"].into_boxed_slice(),
        );
        assert_eq!("  -42     ", res[0]);
        assert_eq!("    0.3214", res[1]);
        assert_eq!(" 1000     ", res[2]);
        assert_eq!("-1000.2   ", res[3]);

        let res = fmt_align_fractions(
            &vec![
                FractionNumber::F64(-42.0),
                FractionNumber::F64(0.3214),
                FractionNumber::F64(1000.0),
                FractionNumber::F64(-1000.2),
            ]
            .into_boxed_slice(),
            FormatPrecision::Max(4),
        );
        assert_eq!("  -42     ", res[0]);
        assert_eq!("    0.3214", res[1]);
        assert_eq!(" 1000     ", res[2]);
        assert_eq!("-1000.2   ", res[3]);
    }

    #[test]
    fn test_fmt_unnecessary_zeroes_are_removed() {
        let res = fmt_align_fraction_strings(&vec!["1.000000000"].into_boxed_slice());
        assert_eq!("1", res[0]);

        let res = fmt_align_fractions(
            &vec![FractionNumber::F32(1.0), FractionNumber::F64(1.0)].into_boxed_slice(),
            FormatPrecision::Max(4),
        );
        assert_eq!("1", res[0]);
        assert_eq!("1", res[1]);
    }

    // tests that we get "NaN" and not a panic or so
    #[test]
    fn test_fmt_nan() {
        let res = fmt_align_fractions(
            &[FractionNumber::F32(f32::NAN), FractionNumber::F64(f64::NAN)],
            FormatPrecision::Max(20), // not important here
        );

        assert_eq!("NaN", res[0]);
        assert_eq!("NaN", res[1]);
    }
}