hledger_parser/directive/
account.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
use chumsky::prelude::*;

use crate::component::account_name::account_name;
use crate::component::whitespace::whitespace;
use crate::state::State;
use crate::utils::end_of_line_prefixed;

#[derive(Debug, Clone, PartialEq)]
pub struct Account {
    pub account_name: Vec<String>,
}

pub fn account<'a>() -> impl Parser<'a, &'a str, Account, extra::Full<Rich<'a, char>, State, ()>> {
    just("account")
        .ignore_then(whitespace().repeated().at_least(1))
        .ignore_then(account_name())
        .then_ignore(
            end_of_line_prefixed(2), // The two-space requirement for same-line account comments is because ; is allowed in account names.
        )
        .map(|account_name| Account { account_name })
}

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

    #[test]
    fn ok_simple() {
        let result = account()
            .then_ignore(end())
            .parse("account one:two:three")
            .into_result();
        assert_eq!(
            result,
            Ok(Account {
                account_name: vec![
                    String::from("one"),
                    String::from("two"),
                    String::from("three"),
                ]
            })
        );
    }

    #[test]
    fn ok_with_padding() {
        let result = account()
            .then_ignore(end())
            .parse("account     one:two:three   ")
            .into_result();
        assert_eq!(
            result,
            Ok(Account {
                account_name: vec![
                    String::from("one"),
                    String::from("two"),
                    String::from("three"),
                ]
            })
        );
    }

    #[test]
    fn ok_comment_merged() {
        let result = account()
            .then_ignore(end())
            .parse("account     one:two:three ; comment ")
            .into_result();
        assert_eq!(
            result,
            Ok(Account {
                account_name: vec![
                    String::from("one"),
                    String::from("two"),
                    String::from("three ; comment"),
                ]
            })
        );
    }

    #[test]
    fn ok_with_comment() {
        let result = account()
            .then_ignore(end())
            .parse("account     one:two:three   ; comment ")
            .into_result();
        assert_eq!(
            result,
            Ok(Account {
                account_name: vec![
                    String::from("one"),
                    String::from("two"),
                    String::from("three"),
                ]
            })
        );
    }

    #[test]
    fn err() {
        let result = account()
            .then_ignore(end())
            .parse("acount     one:two:three   ; comment ")
            .into_result();
        assert!(result.is_err());
    }
}