Lines
96.43 %
Functions
73.08 %
Branches
100 %
//! The Accounts tab's Balance column, filled from one `balance-report` call.
use std::collections::HashMap;
use cli_core::render::{WireValue, parse_wire};
use cli_core::reports::value_to_balance_rows;
use super::App;
use crate::route::RouteCtx;
use crate::tabs::fetch::Fetch;
use crate::tabs::list::{Balances, ListTab};
use crate::tabs::reports::table::amount_text;
use crate::view::ViewId;
use crate::window::WindowId;
impl App {
/// Refetches the balances of every loaded Accounts window after a transaction changed them.
pub(super) fn refetch_account_balances(&mut self) {
for window in self.window_ids(ViewId::Accounts) {
let loaded = self
.win::<ListTab>(window)
.is_some_and(|list| matches!(list.state, Fetch::Loaded(_)));
if loaded {
self.fetch_account_balances(window);
}
pub(super) fn fetch_account_balances(&mut self, window: WindowId) {
self.set_account_balances(window, Balances::Loading);
let Some(seq) = self
.win_mut::<ListTab>(window)
.map(|list| list.balances_seq.advance())
else {
return;
};
let ctx = RouteCtx::AccountBalances { seq };
if self
.dispatch_eval(window, ctx, "(balance-report)".to_string())
.is_none()
{
self.fail_account_balances(window, crate::tabs::nms_eval::UNAVAILABLE);
fn set_account_balances(&mut self, window: WindowId, state: Balances) {
if let Some(list) = self.win_mut::<ListTab>(window) {
list.set_balances(state);
pub(super) fn fail_account_balances(&mut self, window: WindowId, reason: &str) {
self.set_account_balances(window, Balances::Failed);
self.status = format!("balances failed: {reason}");
pub(super) fn deliver_account_balances(&mut self, window: WindowId, seq: u64, wire: &str) {
let awaited = self
.is_some_and(|list| list.balances_seq.awaits(seq));
if !awaited {
match balance_cells(wire) {
Ok(cells) => self.set_account_balances(window, Balances::Ready(cells)),
Err(reason) => self.fail_account_balances(window, &reason),
/// Own-balance amount texts, zero amounts dropped, by account id; accounts without any are absent.
fn balance_cells(wire: &str) -> Result<HashMap<String, Vec<String>>, String> {
let WireValue::Value(value) = parse_wire(wire).map_err(|err| err.to_string())?;
let rows = value_to_balance_rows(&value).map_err(|err| err.to_string())?;
Ok(rows
.iter()
.map(|row| {
let texts = row
.own
.filter(|a| *a.amount.numer() != 0)
.map(amount_text)
.collect();
(row.account_id.to_string(), texts)
})
.collect())