1
//! The Accounts tab's Balance column cells.
2

            
3
use std::collections::HashMap;
4

            
5
const LOADING: &str = "…";
6
const MORE: &str = "+";
7
const ZERO: &str = "0";
8
const SEPARATOR: &str = ", ";
9

            
10
/// What the Balance column knows: each account's amounts, as whole `10.00 USD` texts.
11
#[derive(Debug, Clone, PartialEq, Eq)]
12
pub enum Balances {
13
    Loading,
14
    Failed,
15
    Ready(HashMap<String, Vec<String>>),
16
}
17

            
18
impl Balances {
19
    /// The cell of account `id` in a column `width` characters wide.
20
6270
    pub fn cell(&self, id: Option<&String>, width: usize) -> String {
21
6270
        match self {
22
6030
            Self::Loading => LOADING.to_string(),
23
10
            Self::Failed => String::new(),
24
230
            Self::Ready(cells) => match id.and_then(|id| cells.get(id)) {
25
18
                Some(parts) if !parts.is_empty() => fit(parts, width),
26
213
                _ => ZERO.to_string(),
27
            },
28
        }
29
6270
    }
30
}
31

            
32
42
fn width_of(text: &str) -> usize {
33
42
    text.chars().count()
34
42
}
35

            
36
/// Whole amounts only; `+N` counts the amounts that do not fit.
37
21
fn fit(parts: &[String], width: usize) -> String {
38
21
    let all = parts.join(SEPARATOR);
39
21
    if width_of(&all) <= width {
40
16
        return all;
41
5
    }
42
5
    let mut shown = String::new();
43
5
    let mut count = 0;
44
5
    for part in parts {
45
7
        let next = if shown.is_empty() {
46
5
            part.clone()
47
        } else {
48
2
            format!("{shown}{SEPARATOR}{part}")
49
        };
50
7
        let hidden = parts.len() - count - 1;
51
7
        if width_of(&next) + width_of(SEPARATOR) + width_of(&more(hidden)) > width {
52
5
            break;
53
2
        }
54
2
        shown = next;
55
2
        count += 1;
56
    }
57
5
    let hidden = more(parts.len() - count);
58
5
    if shown.is_empty() {
59
3
        hidden
60
    } else {
61
2
        format!("{shown}{SEPARATOR}{hidden}")
62
    }
63
21
}
64

            
65
12
fn more(hidden: usize) -> String {
66
12
    format!("{MORE}{hidden}")
67
12
}
68

            
69
#[cfg(test)]
70
mod tests {
71
    use super::*;
72

            
73
3
    fn parts(texts: &[&str]) -> Vec<String> {
74
3
        texts.iter().map(ToString::to_string).collect()
75
3
    }
76

            
77
    #[test]
78
1
    fn whole_amounts_that_fit_are_joined() {
79
1
        assert_eq!(fit(&parts(&["1 EUR", "2 USD"]), 20), "1 EUR, 2 USD");
80
1
    }
81

            
82
    #[test]
83
1
    fn an_amount_is_never_cut() {
84
1
        let both = parts(&["12345.50 EUR", "5678.25 USD"]);
85
1
        assert_eq!(fit(&both, 19), "12345.50 EUR, +1");
86
1
        assert_eq!(fit(&both, 13), "+2");
87
1
        assert_eq!(fit(&parts(&["12345678901234567890 USD"]), 10), "+1");
88
1
    }
89

            
90
    #[test]
91
1
    fn cells_distinguish_loading_failed_and_zero() {
92
1
        let id = "a".to_string();
93
1
        assert_eq!(Balances::Loading.cell(Some(&id), 10), "…");
94
1
        assert_eq!(Balances::Failed.cell(Some(&id), 10), "");
95
1
        let ready = Balances::Ready(HashMap::new());
96
1
        assert_eq!(ready.cell(Some(&id), 10), "0");
97
1
        assert_eq!(ready.cell(None, 10), "0");
98
1
    }
99
}