tackler-core 0.17.0

Core components for Tackler accounting engine with native GIT SCM support
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
/*
 * Tackler-NG 2023-2026
 * SPDX-License-Identifier: Apache-2.0
 */

use crate::parser::{is_valid_identifier, is_valid_name};
use crate::tackler;
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::sync::Arc;

#[derive(Debug, Clone, Default, Hash, Eq, Ord, PartialEq, PartialOrd)]
pub struct Commodity {
    pub name: String,
}

impl Display for Commodity {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl Commodity {
    pub fn is_any(&self) -> bool {
        !self.name.is_empty()
    }
    pub fn is_void(&self) -> bool {
        self.name.is_empty()
    }
}

impl Commodity {
    pub fn from(name: &str) -> Result<Commodity, tackler::Error> {
        match is_valid_identifier(name) {
            Err(e) => {
                let msg = format!("This is not a valid commodity: '{name}', error was: {e}");
                Err(msg.into())
            }
            Ok(_) => Ok(Self::unchecked_from(name)),
        }
    }

    pub fn unchecked_from(name: &str) -> Commodity {
        Commodity {
            name: name.to_string(),
        }
    }
}

#[allow(dead_code)]
#[derive(Debug, Clone, Eq)]
pub(crate) struct AccountTreeNode {
    pub(crate) depth: usize,
    root: String,
    /// parent account (path)
    pub(crate) parent: String,
    /// account as sub-accounts
    parts: Vec<String>,
    /// account of posting (path)
    pub(crate) account: String,
    /// account name (leaf)
    name: String,
}

impl AccountTreeNode {
    pub(crate) fn is_root(&self) -> bool {
        self.depth == 1
    }
    pub(crate) fn my_parent_is_root(&self) -> bool {
        self.depth == 2
    }
}

#[derive(Debug, Clone, Eq)]
pub struct TxnAccount {
    pub(crate) atn: Arc<AccountTreeNode>,
    pub(crate) comm: Arc<Commodity>,
}

impl Hash for TxnAccount {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.atn.account.hash(state);
        self.comm.name.hash(state);
    }
}
impl PartialEq for TxnAccount {
    fn eq(&self, other: &Self) -> bool {
        self.atn == other.atn && self.comm == other.comm
    }
}
impl Ord for TxnAccount {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.comm.cmp(&other.comm) {
            Ordering::Less => Ordering::Less,
            Ordering::Greater => Ordering::Greater,
            Ordering::Equal => self.atn.cmp(&other.atn),
        }
    }
}
impl PartialOrd for TxnAccount {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl TxnAccount {
    pub(crate) fn is_parent_of(&self, atn: &TxnAccount) -> bool {
        self.atn.account == atn.atn.parent && self.comm.name == atn.comm.name
    }
    pub(crate) fn is_root(&self) -> bool {
        self.atn.is_root()
    }

    pub(crate) fn my_parent_is_root(&self) -> bool {
        self.atn.my_parent_is_root()
    }
}

impl Display for AccountTreeNode {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.account)
    }
}

impl Hash for AccountTreeNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.account.hash(state);
    }
}

impl PartialEq for AccountTreeNode {
    fn eq(&self, other: &Self) -> bool {
        // depth is used as fast-path checking
        self.depth == other.depth && self.account == other.account
    }
}

impl PartialOrd for AccountTreeNode {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for AccountTreeNode {
    /// Compares 2 account names piecewise by sub-accounts
    /// Basic lexical sort order of account name can't be used because of
    /// numerical accounts will sort wrongly with ':' in the middle
    /// If `account_name_a` is a prefix of `account_name_b` then `account_name_a < account_name_b`
    fn cmp(&self, other: &Self) -> Ordering {
        for (acc_a, acc_b) in self.parts.iter().zip(other.parts.iter()) {
            match acc_a.cmp(acc_b) {
                Ordering::Equal => {}
                ordering => return ordering,
            }
        }
        // So far subcomponents are equal, but which one has more subcomponents?
        self.depth.cmp(&other.depth)
    }
}

#[cfg(test)]
impl AccountTreeNode {
    pub(crate) fn get_root(&self) -> &str {
        self.root.as_str()
    }
    pub(crate) fn get_name(&self) -> &str {
        self.name.as_str()
    }
}

impl AccountTreeNode {
    pub(crate) fn from(account: &str) -> Result<AccountTreeNode, tackler::Error> {
        if let Err(e) = is_valid_name(account) {
            let msg = format!("This is not a valid account name: '{account}', error was: {e}");
            return Err(msg.into());
        }
        Ok(Self::unchecked_from(account))
    }

    pub(crate) fn unchecked_from(account: &str) -> AccountTreeNode {
        let parts: Vec<&str> = account.split(':').collect();

        let depth = parts.len();
        let root = String::from(parts[0]);
        let acc_parts = parts.iter().map(ToString::to_string).collect();

        let mut rev_parts = parts;
        rev_parts.reverse();
        let name = String::from(rev_parts.remove(0));

        rev_parts.reverse();
        let parent = rev_parts.join(":");

        AccountTreeNode {
            depth,
            root,
            parent,
            parts: acc_parts,
            account: account.to_string(),
            name,
        }
    }
}

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

    #[test]
    // test: 270d505b-76f6-4e49-a24c-2fbdfb6e5adf
    fn commodity_ok() {
        let res = Commodity::from("He·bar");
        assert!(res.is_ok());
        let c = res.unwrap(/*:test:*/);
        assert_eq!(c.name, "He·bar");

        assert!(Commodity::from("$").is_ok());
        assert!(Commodity::from("¢").is_ok());
        assert!(Commodity::from("£").is_ok());
        assert!(Commodity::from("¤").is_ok());
        assert!(Commodity::from("¥").is_ok());
    }

    #[test]
    // test: 699aadb1-d1ba-44b6-ae6a-158cf5be13e5
    fn commodity_err() {
        assert!(Commodity::from("123").is_err());
        assert!(Commodity::from("-USD").is_err());
        assert!(Commodity::from("_USD").is_err());
        assert!(Commodity::from("·USD").is_err());
        assert!(Commodity::from("He:bar").is_err());
        assert!(Commodity::from("He bar").is_err());
    }

    /*
    todo: commodity tests as part of AccountTreeNode vs. TxnAccount
    #[test]
    // test: e11d5d26-c149-4d8c-b150-5cb2e2f80608
    fn atn_commodity() {
        let atn = AccountTreeNode::from(
            "a",
            Some(Commodity::from("He·bar".to_string()).unwrap(/*:test:*/)),
        )
        .unwrap(/*:test:*/);

        assert!(!atn.commodity.name.is_empty());
        assert_eq!(atn.commodity.name, "He·bar".to_string());
    }
     */

    #[test]
    fn atn_is_root() {
        let atn_a = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        let atn_ab = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a:b")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        let atn_abc = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a:b:c")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        assert_eq!(atn_a.is_root(), true);
        assert_eq!(atn_ab.is_root(), false);
        assert_eq!(atn_abc.is_root(), false);
    }
    #[test]
    fn atn_my_parent_is_root() {
        let atn_a = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        let atn_ab = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a:b")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        let atn_abc = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a:b:c")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        assert_eq!(atn_a.my_parent_is_root(), false);
        assert_eq!(atn_ab.my_parent_is_root(), true);
        assert_eq!(atn_abc.my_parent_is_root(), false);
    }

    #[test]
    fn atn_is_parent() {
        let parent = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a:b")
                .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        let leaf = TxnAccount {
            atn: Arc::new(AccountTreeNode::from("a:b:c")
            .unwrap(/*:test:*/)),
            comm: Arc::new(Commodity::default()),
        };
        assert!(parent.is_parent_of(&leaf));
        assert!(!parent.is_parent_of(&parent));
    }

    #[test]
    fn atn_ok_display() {
        let atn = AccountTreeNode::from("a:b:c")
        .unwrap(/*:test:*/);

        let atn_str = format!("{atn}");
        assert_eq!(atn_str, String::from("a:b:c"));
    }

    #[test]
    // test: 88c5cb23-5995-4b93-8c26-a3f7374e96d9
    fn atn_root() {
        let atn = AccountTreeNode::from("a").unwrap(/*:test:*/);

        assert_eq!(atn.depth, 1);
        assert_eq!(atn.root, "a");
        assert_eq!(atn.parent, "");
        assert_eq!(atn.account, "a");
        assert_eq!(atn.name, "a");
    }

    #[test]
    // test: fc69f9b2-1faf-425c-87d3-aed63d66171b
    fn atn_two() {
        let atn = AccountTreeNode::from("a:b").unwrap(/*:test:*/);

        assert_eq!(atn.depth, 2);
        assert_eq!(atn.root, "a");
        assert_eq!(atn.parent, "a");
        assert_eq!(atn.account, "a:b");
        assert_eq!(atn.name, "b");
    }

    #[test]
    // test: 38c103d3-4cc7-4af7-86cd-bf24ca37d026
    fn atn_three() {
        let atn = AccountTreeNode::from("a:b:c").unwrap(/*:test:*/);

        assert_eq!(atn.depth, 3);
        assert_eq!(atn.root, "a");
        assert_eq!(atn.parent, "a:b");
        assert_eq!(atn.account, "a:b:c");
        assert_eq!(atn.name, "c");
    }

    #[test]
    // test: 76a6c300-5569-4e1d-a0a1-ae2ee31d919a
    fn atn_more() {
        let atn = AccountTreeNode::from("a:b:c:leaf").unwrap(/*:test:*/);

        assert_eq!(atn.depth, 4);
        assert_eq!(atn.root, "a");
        assert_eq!(atn.parent, "a:b:c");
        assert_eq!(atn.account, "a:b:c:leaf");
        assert_eq!(atn.name, "leaf");
    }

    #[test]
    // test: 55407835-34d8-4de9-a362-4172f0e4d54b
    fn err_empty() {
        let atn = AccountTreeNode::from("");
        assert!(atn.is_err());
    }

    #[test]
    // test: 0609e72f-c509-4b62-950e-fce432122d10
    fn err_empty_sub() {
        // new tests
        assert!(AccountTreeNode::from("a:").is_err());
        assert!(AccountTreeNode::from(":a").is_err());
        assert!(AccountTreeNode::from("a::b").is_err());
        assert!(AccountTreeNode::from("::").is_err());
        // old tests
        assert!(AccountTreeNode::from(":").is_err());
        assert!(AccountTreeNode::from(": :").is_err());
    }

    #[test]
    fn err_invalid_ids_sep_chars() {
        assert!(AccountTreeNode::from("-Err").is_err());
        assert!(AccountTreeNode::from("_Err").is_err());
        assert!(AccountTreeNode::from("·Err").is_err());

        assert!(AccountTreeNode::from("A:-Err").is_err());
        assert!(AccountTreeNode::from("A:_Err").is_err());
        assert!(AccountTreeNode::from("A:·Err").is_err());
    }

    #[test]
    fn err_invalid_ids_spaces() {
        assert!(AccountTreeNode::from(" a").is_err());
        assert!(AccountTreeNode::from("a ").is_err());
        assert!(AccountTreeNode::from(" a ").is_err());

        assert!(AccountTreeNode::from(" a:b:c").is_err());
        assert!(AccountTreeNode::from("a:b:c ").is_err());
        assert!(AccountTreeNode::from(" a:b:c ").is_err());
        assert!(AccountTreeNode::from("a:b c:d").is_err());
    }

    #[test]
    fn err_invalid_ids_numerical() {
        assert!(AccountTreeNode::from("123:Err").is_err());
        assert!(AccountTreeNode::from("-123:Err").is_err());
        assert!(AccountTreeNode::from("1ABC:Err").is_err());
        assert!(AccountTreeNode::from("-1ABC:Err").is_err());
    }
}