okane-core 0.21.0

Library to support parsing, emitting and processing Ledger (https://www.ledger-cli.org/) format files.
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Defines the [`BalanceTree`], tree of balance.

use std::collections::{HashMap, HashSet};

use super::account::{Account, AccountAggregate, AccountTreeKey};
use super::balance::Balance;
use super::context::ReportContext;
use super::eval::Amount;
use super::query::QueryError;

/// Accumulated balance, considering the account hierarchy.
#[derive(Debug)]
pub struct BalanceTree<'ctx> {
    /// Amount labeled with AccountAggregate.
    /// Sorted alphabetically, also stored in hierarchical.
    values: Vec<BalanceTreeNode<'ctx>>,
}

impl<'ctx> BalanceTree<'ctx> {
    pub fn create(ctx: &ReportContext<'ctx>, balance: Balance<'ctx>) -> Result<Self, QueryError> {
        let mut values: Vec<BalanceTreeNode<'ctx>> = vec![BalanceTreeNode::placeholder(
            AccountTreeKey::Root,
            0,
            Amount::zero(),
        )];
        // To tell if the account aggregate is required or not,
        // we need to maintain `required` to avoid going into unnecessary branch in accumulate DFS.
        let mut required: HashSet<AccountAggregate<'ctx>> = HashSet::new();
        for (account, _) in balance.iter() {
            let mut account = (*account).into();
            required.insert(account);
            while let Some(parent) = parent_of(ctx, account)? {
                if !required.insert(parent) {
                    // other already inserted, no need to do.
                    break;
                }
                account = parent;
            }
        }
        accumulate(&mut balance.into_map(), &mut values, ctx, &required, 0)?;
        Ok(Self { values })
    }

    /// Returns all nodes in the flat vector.
    ///
    /// The order is both alphabetical (by account name) and a pre-order
    /// (DFS) flattening of the account hierarchy; index 0 is always the
    /// synthetic [`AccountTreeKey::Root`].
    pub fn nodes(&self) -> &[BalanceTreeNode<'ctx>] {
        &self.values
    }

    /// Consumes the tree and returns the owned node vector.
    /// See [`Self::nodes`] for the ordering guarantees.
    pub fn into_nodes(self) -> Vec<BalanceTreeNode<'ctx>> {
        self.values
    }
}

fn parent_of<'ctx>(
    ctx: &ReportContext<'ctx>,
    account: AccountAggregate<'ctx>,
) -> Result<Option<AccountAggregate<'ctx>>, QueryError> {
    let parent = ctx.account_tree.parent(account).ok_or_else(|| {
        QueryError::Internal(format!(
            "account {} isn't registered in the AccountTree",
            account.as_str()
        ))
    })?;
    Ok(match parent {
        AccountTreeKey::Root => None,
        AccountTreeKey::Descendant(parent) => Some(parent),
    })
}

fn label(account: AccountTreeKey<'_>) -> &str {
    account
        .as_aggregate()
        .map(|x| x.as_str())
        .unwrap_or_default()
}

fn accumulate<'ctx>(
    balance: &mut HashMap<Account<'ctx>, Amount<'ctx>>,
    values: &mut Vec<BalanceTreeNode<'ctx>>,
    ctx: &ReportContext<'ctx>,
    required: &HashSet<AccountAggregate<'ctx>>,
    i: usize,
) -> Result<(Amount<'ctx>, NodeRange), QueryError> {
    debug_assert!(i < values.len());
    let account = values[i].account;
    let depth = values[i].depth;
    let mut total = values[i].self_amount.clone();
    let mut children: Vec<NodeRange> = Vec::new();
    for account in ctx.account_tree.children(account).ok_or_else(|| {
        QueryError::Internal(format!(
            "account {} isn't registered in the AccountTree",
            label(account)
        ))
    })? {
        if !required.contains(account) {
            continue;
        }
        let j = values.len();
        let amount = match account {
            AccountAggregate::Account(account) => balance.remove(account).unwrap_or_default(),
            AccountAggregate::Ancestor(_) => Amount::default(),
        };
        values.push(BalanceTreeNode::placeholder(
            (*account).into(),
            depth + 1,
            amount,
        ));
        let (child_amount, child_range) = accumulate(balance, values, ctx, required, j)?;
        total += child_amount;
        children.push(child_range);
    }
    let cover = NodeRange::cover(i, &children)?;
    total.remove_zero_entries();
    values[i].subtree_amount = total.clone();
    values[i].children = children;
    values[i].range = cover;
    Ok((total, cover))
}

/// Node of the each element.
#[derive(Debug, PartialEq, Eq)]
pub struct BalanceTreeNode<'ctx> {
    /// Account key of this node ([`AccountTreeKey::Root`] for the synthetic root).
    pub account: AccountTreeKey<'ctx>,
    /// Depth in the tree: root is `0`, its children `1`, and so on.
    pub depth: u16,
    /// Amount of the node itself, excluding descendants.
    pub self_amount: Amount<'ctx>,
    /// Amount of the node and all its descendants.
    pub subtree_amount: Amount<'ctx>,
    /// Children, expressed as Vector index range.
    children: Vec<NodeRange>,
    /// Contiguous span of this node plus all its descendants in the flat vector.
    range: NodeRange,
}

impl<'ctx> BalanceTreeNode<'ctx> {
    /// Creates a new instance with place holder.
    fn placeholder(account: AccountTreeKey<'ctx>, depth: u16, self_amount: Amount<'ctx>) -> Self {
        Self {
            account,
            depth,
            self_amount,
            // all placeholders below
            subtree_amount: Amount::zero(),
            children: Vec::new(),
            range: NodeRange { start: 0, size: 0 },
        }
    }

    /// Returns `true` when the node has at least one child.
    pub fn has_children(&self) -> bool {
        !self.children.is_empty()
    }

    /// Range of this node and all its descendants in [`BalanceTree::nodes`].
    ///
    /// The node itself is at `range.start`; its descendants (if any) occupy
    /// `range.start + 1 .. range.end`.
    pub fn subtree_range(&self) -> std::ops::Range<usize> {
        self.range.into()
    }
}

/// Range with Copy dedicated for node range.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct NodeRange {
    pub start: usize,
    pub size: usize,
}

impl NodeRange {
    /// Constructs a single NodeRange as a cover of given `[i]` + `ranges`.
    /// Each range must be in order (smaller to larger), continuous.
    fn cover(i: usize, ranges: &[NodeRange]) -> Result<Self, QueryError> {
        let start = i;
        let mut size = 1;
        for range in ranges {
            if start + size != range.start {
                return Err(QueryError::Internal(format!(
                    "NodeRange::cover can only cover continuous and mutually exclusive ranges: total start={start}, size={size}, current start={}",
                    range.start
                )));
            }
            size += range.size;
        }
        Ok(Self { start, size })
    }
}

impl From<std::ops::Range<usize>> for NodeRange {
    fn from(value: std::ops::Range<usize>) -> Self {
        Self {
            start: value.start,
            size: value.end.checked_sub(value.start).unwrap(),
        }
    }
}

impl From<NodeRange> for std::ops::Range<usize> {
    fn from(value: NodeRange) -> Self {
        Self {
            start: value.start,
            end: value.start + value.size,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use assert_matches::assert_matches;
    use bumpalo::Bump;
    use maplit::hashmap;
    use pretty_assertions::assert_eq;
    use rust_decimal::Decimal;
    use rust_decimal_macros::dec;

    #[test]
    fn construct_fails_on_unknown_account() {
        let arena = Bump::new();
        let mut ctx = ReportContext::new(&arena);
        let usd = ctx.commodities.ensure("USD");

        // balance is constructed after tree construction.
        // this will make the balance creation in invalid state.
        ctx.account_tree.construct(&ctx.accounts);
        let balance = Balance::from_iter(hashmap! {
            ctx.accounts.ensure("Assets:Bank") => Amount::from_value(usd, dec!(1)),
            ctx.accounts.ensure("Assets:Bank:Checking") => Amount::from_value(usd, dec!(3)),
        });

        let got_err = BalanceTree::create(&ctx, balance).unwrap_err();
        assert_matches!(got_err, QueryError::Internal(msg) if msg.contains("isn't registered in the AccountTree"));
    }

    #[test]
    fn simple_tree_for_non_overlapping_balance() {
        let arena = Bump::new();
        let mut ctx = ReportContext::new(&arena);
        let chf = ctx.commodities.ensure("CHF");
        let jpy = ctx.commodities.ensure("JPY");
        let usd = ctx.commodities.ensure("USD");

        let balance = Balance::from_iter(hashmap! {
            ctx.accounts.ensure("Assets") =>
                Amount::from_iter([(chf, dec!(10)), (usd, dec!(1))]),
            ctx.accounts.ensure("Expenses") =>
                Amount::from_value(jpy, Decimal::ZERO),
        });
        ctx.account_tree.construct(&ctx.accounts);

        let tree = BalanceTree::create(&ctx, balance).unwrap();
        let got = tree.into_nodes();
        let want = vec![
            BalanceTreeNode {
                account: AccountTreeKey::Root,
                depth: 0,
                self_amount: Amount::zero(),
                subtree_amount: Amount::from_iter([(chf, dec!(10)), (usd, dec!(1))]),
                children: vec![(1..2).into(), (2..3).into()],
                range: (0..3).into(),
            },
            BalanceTreeNode {
                account: ctx.accounts.resolve("Assets").unwrap().into(),
                depth: 1,
                self_amount: Amount::from_iter([(chf, dec!(10)), (usd, dec!(1))]),
                subtree_amount: Amount::from_iter([(chf, dec!(10)), (usd, dec!(1))]),
                children: vec![],
                range: (1..2).into(),
            },
            BalanceTreeNode {
                account: ctx.accounts.resolve("Expenses").unwrap().into(),
                depth: 1,
                self_amount: Amount::zero(),
                subtree_amount: Amount::zero(),
                children: vec![],
                range: (2..3).into(),
            },
        ];
        assert_eq!(want, got);

        assert_eq!(2..3, got[2].subtree_range());
        assert!(got[0].has_children());
        assert!(!got[2].has_children());
    }

    /// Resolves `name` to its tree key, whether it is a posted account or an
    /// ancestor-only prefix.
    fn account_key<'a>(ctx: &ReportContext<'a>, name: &str) -> AccountTreeKey<'a> {
        if let Some(account) = ctx.accounts.resolve(name) {
            account.into()
        } else {
            ctx.account_tree
                .resolve_ancestor(name)
                .expect("account or prefix")
                .into()
        }
    }

    #[test]
    fn tree_with_overlapping_and_ancestor_accounts() {
        let arena = Bump::new();
        let mut ctx = ReportContext::new(&arena);
        let usd = ctx.commodities.ensure("USD");

        let balance = Balance::from_iter(hashmap! {
            ctx.accounts.ensure("Assets:Bank") => Amount::from_value(usd, dec!(1)),
            ctx.accounts.ensure("Assets:Bank:Checking") => Amount::from_value(usd, dec!(3)),
            ctx.accounts.ensure("Assets:Cash") => Amount::from_value(usd, dec!(5)),
            ctx.accounts.ensure("Expenses:Property") => Amount::from_value(usd, dec!(1_000)),
            ctx.accounts.ensure("Expenses:Property:Maintenance") => Amount::from_value(usd, dec!(23)),
            ctx.accounts.ensure("Expenses:Property Tax") => Amount::from_value(usd, dec!(13)),
            ctx.accounts.ensure("Expenses:Travel:Taxi") => Amount::from_value(usd, dec!(11)),
            // want to see if Expenses:Travel gets Amount::zero.
            ctx.accounts.ensure("Expenses:Travel:Train") => Amount::from_value(usd, dec!(-11)),
        });
        // this account exists, but not included in the balance.
        ctx.accounts.ensure("Income:Salary");
        ctx.account_tree.construct(&ctx.accounts);

        let amount = |v| Amount::from_value(usd, v);
        let key = |name: &str| account_key(&ctx, name);

        let tree = BalanceTree::create(&ctx, balance).unwrap();
        let got = tree.into_nodes();
        let want = vec![
            // 0: Root — spans the whole vector.
            BalanceTreeNode {
                account: AccountTreeKey::Root,
                depth: 0,
                self_amount: Amount::zero(),
                subtree_amount: amount(dec!(1045)),
                children: vec![(1..5).into(), (5..12).into()],
                range: (0..12).into(),
            },
            // 1: Assets — ancestor with no posting of its own.
            BalanceTreeNode {
                account: key("Assets"),
                depth: 1,
                self_amount: Amount::zero(),
                subtree_amount: amount(dec!(9)),
                children: vec![(2..4).into(), (4..5).into()],
                range: (1..5).into(),
            },
            // 2: Assets:Bank — posted *and* a parent; keeps its own 1 USD.
            BalanceTreeNode {
                account: key("Assets:Bank"),
                depth: 2,
                self_amount: amount(dec!(1)),
                subtree_amount: amount(dec!(4)),
                children: vec![(3..4).into()],
                range: (2..4).into(),
            },
            // 3: Assets:Bank:Checking — leaf.
            BalanceTreeNode {
                account: key("Assets:Bank:Checking"),
                depth: 3,
                self_amount: amount(dec!(3)),
                subtree_amount: amount(dec!(3)),
                children: vec![],
                range: (3..4).into(),
            },
            // 4: Assets:Cash — leaf sibling of Assets:Bank.
            BalanceTreeNode {
                account: key("Assets:Cash"),
                depth: 2,
                self_amount: amount(dec!(5)),
                subtree_amount: amount(dec!(5)),
                children: vec![],
                range: (4..5).into(),
            },
            // 5: Expenses — ancestor.
            BalanceTreeNode {
                account: key("Expenses"),
                depth: 1,
                self_amount: Amount::zero(),
                subtree_amount: amount(dec!(1036)),
                children: vec![(6..8).into(), (8..9).into(), (9..12).into()],
                range: (5..12).into(),
            },
            // 6: Expenses:Property — posted and the parent of Maintenance.
            BalanceTreeNode {
                account: key("Expenses:Property"),
                depth: 2,
                self_amount: amount(dec!(1_000)),
                subtree_amount: amount(dec!(1_023)),
                children: vec![(7..8).into()],
                range: (6..8).into(),
            },
            // 7: Expenses:Property:Maintenance — child of Property; stays grouped
            // under it even though "Property Tax" sorts before it by full name.
            BalanceTreeNode {
                account: key("Expenses:Property:Maintenance"),
                depth: 3,
                self_amount: amount(dec!(23)),
                subtree_amount: amount(dec!(23)),
                children: vec![],
                range: (7..8).into(),
            },
            // 8: Expenses:Property Tax — sibling leaf of Property (not its child).
            BalanceTreeNode {
                account: key("Expenses:Property Tax"),
                depth: 2,
                self_amount: amount(dec!(13)),
                subtree_amount: amount(dec!(13)),
                children: vec![],
                range: (8..9).into(),
            },
            // 9: Expenses:Travel — ancestor with a single posted descendant.
            BalanceTreeNode {
                account: key("Expenses:Travel"),
                depth: 2,
                self_amount: Amount::zero(),
                subtree_amount: Amount::zero(),
                children: vec![(10..11).into(), (11..12).into()],
                range: (9..12).into(),
            },
            // 10: Expenses:Travel:Taxi — leaf.
            BalanceTreeNode {
                account: key("Expenses:Travel:Taxi"),
                depth: 3,
                self_amount: amount(dec!(11)),
                subtree_amount: amount(dec!(11)),
                children: vec![],
                range: (10..11).into(),
            },
            // 11: Expenses:Travel:Train — leaf.
            BalanceTreeNode {
                account: key("Expenses:Travel:Train"),
                depth: 3,
                self_amount: amount(dec!(-11)),
                subtree_amount: amount(dec!(-11)),
                children: vec![],
                range: (11..12).into(),
            },
        ];
        assert_eq!(want, got);
    }
}