1
//! Exact report tables built straight from the reply's row views: every row
2
//! (sub-accounts indented by depth), every commodity amount, in reply order.
3

            
4
use server::command::report::view::{
5
    AmountView, BreakdownPeriodView, PeriodActivityView, ReportRowView,
6
};
7

            
8
const INDENT: &str = "  ";
9
const UNCATEGORIZED: &str = "(uncategorized)";
10

            
11
enum Line {
12
    Heading(String),
13
    Row { label: String, amounts: String },
14
}
15

            
16
285
pub(crate) fn amount_text(a: &AmountView) -> String {
17
285
    format!(
18
        "{} {}",
19
285
        amount::display(*a.amount.numer(), *a.amount.denom()),
20
285
        a.commodity_symbol
21
    )
22
285
}
23

            
24
253
fn amounts_text(amounts: &[AmountView]) -> String {
25
253
    amounts
26
253
        .iter()
27
253
        .map(amount_text)
28
253
        .collect::<Vec<_>>()
29
253
        .join(", ")
30
253
}
31

            
32
253
fn row(depth: usize, name: &str, amounts: &[AmountView]) -> Line {
33
253
    Line::Row {
34
253
        label: format!("{}{name}", INDENT.repeat(depth)),
35
253
        amounts: amounts_text(amounts),
36
253
    }
37
253
}
38

            
39
20
fn heading(label: &str) -> Option<Line> {
40
20
    (!label.is_empty()).then(|| Line::Heading(label.to_string()))
41
20
}
42

            
43
120
fn render(lines: Vec<Line>) -> Vec<String> {
44
120
    if lines.is_empty() {
45
94
        return vec!["  (no data)".to_string()];
46
26
    }
47
26
    let width = lines
48
26
        .iter()
49
269
        .filter_map(|l| match l {
50
253
            Line::Row { label, .. } => Some(label.chars().count()),
51
16
            Line::Heading(_) => None,
52
269
        })
53
        .max()
54
26
        .unwrap_or(0);
55
26
    lines
56
26
        .into_iter()
57
269
        .map(|l| match l {
58
16
            Line::Heading(text) => text,
59
253
            Line::Row { label, amounts } => format!("  {label:<width$}  {amounts}"),
60
269
        })
61
        .collect()
62
120
}
63

            
64
#[must_use]
65
84
pub fn balance_table(rows: &[ReportRowView]) -> Vec<String> {
66
84
    render(
67
84
        rows.iter()
68
12
            .map(|r| row(r.depth, &r.account_name, &r.amounts))
69
            .collect(),
70
    )
71
84
}
72

            
73
#[must_use]
74
12
pub fn activity_table(periods: &[PeriodActivityView]) -> Vec<String> {
75
12
    let mut lines = Vec::new();
76
12
    for period in periods {
77
7
        lines.extend(heading(&period.label));
78
7
        for group in &period.groups {
79
10
            lines.push(Line::Heading(group.label.clone()));
80
10
            lines.extend(
81
10
                group
82
10
                    .rows
83
10
                    .iter()
84
11
                    .map(|r| row(r.depth, &r.account_name, &r.amounts)),
85
            );
86
10
            lines.push(row(0, "Total", &group.total));
87
        }
88
7
        lines.push(row(0, "Net", &period.net));
89
    }
90
12
    render(lines)
91
12
}
92

            
93
#[must_use]
94
24
pub fn breakdown_table(periods: &[BreakdownPeriodView]) -> Vec<String> {
95
24
    let mut lines = Vec::new();
96
24
    for period in periods {
97
13
        lines.extend(heading(&period.label));
98
13
        for r in &period.rows {
99
213
            let name = if r.is_uncategorized {
100
1
                UNCATEGORIZED
101
            } else {
102
212
                &r.tag_value
103
            };
104
213
            lines.push(row(0, name, &r.amounts));
105
        }
106
    }
107
24
    render(lines)
108
24
}