hledger_parser/directive/
auto_postings.rs

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
mod query;

use chumsky::prelude::*;

use crate::component::account_name::account_name;
use crate::component::amount::{amount, Amount};
use crate::component::comment::inline;
use crate::component::whitespace::whitespace;
use crate::directive::auto_postings::query::query;
use crate::state::State;
use crate::utils::end_of_line;

pub use crate::directive::auto_postings::query::{Query, Term};

#[derive(Clone, Debug, PartialEq)]
pub struct AutosPostingRule {
    pub query: Query,
    pub postings: Vec<AutoPosting>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct AutoPosting {
    pub account_name: Vec<String>,
    pub is_virtual: bool,
    pub amount: Amount,
    pub is_mul: bool,
}

pub fn auto_postings<'a>(
) -> impl Parser<'a, &'a str, AutosPostingRule, extra::Full<Rich<'a, char>, State, ()>> {
    let header = just("=")
        .ignore_then(whitespace().repeated())
        .ignore_then(query().then_ignore(end_of_line()))
        .then_ignore(text::newline());

    let account_name = account_name()
        .delimited_by(just('('), just(')'))
        .map(|name| (name, true))
        .or(account_name().map(|name| (name, false)));
    let posting = whitespace()
        .repeated()
        .at_least(1)
        .ignore_then(account_name)
        .then_ignore(whitespace().repeated().at_least(2))
        .then(just("*").or_not())
        .then(amount())
        .then_ignore(end_of_line())
        .map(
            |(((account_name, is_virtual), is_mul), amount)| AutoPosting {
                account_name,
                is_virtual,
                amount,
                is_mul: is_mul.is_some(),
            },
        );

    header
        .then_ignore(
            text::whitespace()
                .at_least(1)
                .then(inline())
                .then_ignore(text::newline())
                .or_not(),
        )
        .then(
            posting
                .separated_by(text::newline())
                .at_least(2)
                .collect::<Vec<_>>(),
        )
        .map(|(query, postings)| AutosPostingRule { query, postings })
}

#[cfg(test)]
mod tests {
    use rust_decimal::Decimal;

    use self::tests::query::Term;

    use super::*;

    #[test]
    fn full() {
        let result = auto_postings()
            .then_ignore(end())
            .parse(
                "= expenses:gifts
    assets:checking:gifts  *-1$
    (assets:checking)         1",
            )
            .into_result();
        assert_eq!(
            result,
            Ok(AutosPostingRule {
                query: Query {
                    terms: vec![Term {
                        r#type: None,
                        is_not: false,
                        value: String::from("expenses:gifts"),
                    }],
                },
                postings: vec![
                    AutoPosting {
                        account_name: vec![
                            String::from("assets"),
                            String::from("checking"),
                            String::from("gifts")
                        ],
                        is_virtual: false,
                        is_mul: true,
                        amount: Amount {
                            quantity: Decimal::new(-1, 0),
                            commodity: String::from("$"),
                        },
                    },
                    AutoPosting {
                        account_name: vec![String::from("assets"), String::from("checking"),],
                        is_virtual: true,
                        is_mul: false,
                        amount: Amount {
                            quantity: Decimal::new(1, 0),
                            commodity: String::new(),
                        },
                    }
                ],
            })
        );
    }
}