Lines
100 %
Functions
57.69 %
Branches
//! Exact report tables built straight from the reply's row views: every row
//! (sub-accounts indented by depth), every commodity amount, in reply order.
use server::command::report::view::{
AmountView, BreakdownPeriodView, PeriodActivityView, ReportRowView,
};
const INDENT: &str = " ";
const UNCATEGORIZED: &str = "(uncategorized)";
enum Line {
Heading(String),
Row { label: String, amounts: String },
}
pub(crate) fn amount_text(a: &AmountView) -> String {
format!(
"{} {}",
amount::display(*a.amount.numer(), *a.amount.denom()),
a.commodity_symbol
)
fn amounts_text(amounts: &[AmountView]) -> String {
amounts
.iter()
.map(amount_text)
.collect::<Vec<_>>()
.join(", ")
fn row(depth: usize, name: &str, amounts: &[AmountView]) -> Line {
Line::Row {
label: format!("{}{name}", INDENT.repeat(depth)),
amounts: amounts_text(amounts),
fn heading(label: &str) -> Option<Line> {
(!label.is_empty()).then(|| Line::Heading(label.to_string()))
fn render(lines: Vec<Line>) -> Vec<String> {
if lines.is_empty() {
return vec![" (no data)".to_string()];
let width = lines
.filter_map(|l| match l {
Line::Row { label, .. } => Some(label.chars().count()),
Line::Heading(_) => None,
})
.max()
.unwrap_or(0);
lines
.into_iter()
.map(|l| match l {
Line::Heading(text) => text,
Line::Row { label, amounts } => format!(" {label:<width$} {amounts}"),
.collect()
#[must_use]
pub fn balance_table(rows: &[ReportRowView]) -> Vec<String> {
render(
rows.iter()
.map(|r| row(r.depth, &r.account_name, &r.amounts))
.collect(),
pub fn activity_table(periods: &[PeriodActivityView]) -> Vec<String> {
let mut lines = Vec::new();
for period in periods {
lines.extend(heading(&period.label));
for group in &period.groups {
lines.push(Line::Heading(group.label.clone()));
lines.extend(
group
.rows
.map(|r| row(r.depth, &r.account_name, &r.amounts)),
);
lines.push(row(0, "Total", &group.total));
lines.push(row(0, "Net", &period.net));
render(lines)
pub fn breakdown_table(periods: &[BreakdownPeriodView]) -> Vec<String> {
for r in &period.rows {
let name = if r.is_uncategorized {
UNCATEGORIZED
} else {
&r.tag_value
lines.push(row(0, name, &r.amounts));