hledger_parser/component/
amount.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
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
use chumsky::prelude::*;
use rust_decimal::Decimal;

use crate::component::commodity::commodity;
use crate::component::quantity::quantity;
use crate::component::whitespace::whitespace;
use crate::state::State;

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Amount {
    pub quantity: Decimal,
    pub commodity: String,
}

pub fn amount<'a>() -> impl Parser<'a, &'a str, Amount, extra::Full<Rich<'a, char>, State, ()>> {
    let sign_quantity_commodity = one_of("-+")
        .then_ignore(whitespace().repeated())
        .then(quantity())
        .then_ignore(whitespace().repeated())
        .then(commodity())
        .map(|((sign, mut quantity), commodity)| {
            if sign == '-' {
                quantity.set_sign_negative(true);
            }
            Amount {
                quantity,
                commodity,
            }
        });
    let quantity_sign_commodity = quantity()
        .then_ignore(whitespace().repeated())
        .then(one_of("-+"))
        .then_ignore(whitespace().repeated())
        .then(commodity())
        .map(|((mut quantity, sign), commodity)| {
            if sign == '-' {
                quantity.set_sign_negative(true);
            }
            Amount {
                quantity,
                commodity,
            }
        });
    let sign_commodity_quantity = one_of("-+")
        .then_ignore(whitespace().repeated())
        .then(commodity())
        .then_ignore(whitespace().repeated())
        .then(quantity())
        .map(|((sign, commodity), mut quantity)| {
            if sign == '-' {
                quantity.set_sign_negative(true);
            }
            Amount {
                quantity,
                commodity,
            }
        });
    let commodity_sign_quantity = commodity()
        .then_ignore(whitespace().repeated())
        .then(one_of("-+"))
        .then_ignore(whitespace().repeated())
        .then(quantity())
        .map(|((commodity, sign), mut quantity)| {
            if sign == '-' {
                quantity.set_sign_negative(true);
            }
            Amount {
                quantity,
                commodity,
            }
        });
    let quantity_commodity = quantity()
        .then_ignore(whitespace().repeated())
        .then(commodity())
        .map(|(quantity, commodity)| Amount {
            quantity,
            commodity,
        });
    let commodity_quantity = commodity()
        .then_ignore(whitespace().repeated())
        .then(quantity())
        .map(|(commodity, quantity)| Amount {
            quantity,
            commodity,
        });
    let just_quantity = quantity().map(|quantity| Amount {
        quantity,
        ..Amount::default()
    });
    sign_quantity_commodity
        .or(quantity_sign_commodity)
        .or(sign_commodity_quantity)
        .or(commodity_sign_quantity)
        .or(quantity_commodity)
        .or(commodity_quantity)
        .or(just_quantity)
}

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

    #[test]
    fn quantity_no_commodity() {
        let result = amount().then_ignore(end()).parse("1").into_result();
        assert_eq!(
            result,
            Ok(Amount {
                quantity: Decimal::new(1, 0),
                ..Amount::default()
            })
        );
    }

    #[test]
    fn quantity_with_commodity() {
        for (input, expected) in [
            (
                "$1",
                Amount {
                    quantity: Decimal::new(1, 0),
                    commodity: String::from("$"),
                },
            ),
            (
                "4000 AAPL",
                Amount {
                    quantity: Decimal::new(4000, 0),
                    commodity: String::from("AAPL"),
                },
            ),
            (
                "3 \"green apples\"",
                Amount {
                    quantity: Decimal::new(3, 0),
                    commodity: String::from("green apples"),
                },
            ),
        ] {
            let result = amount().then_ignore(end()).parse(input).into_result();
            assert_eq!(result, Ok(expected), "{input}");
        }
    }

    #[test]
    fn signed_quantity_with_commodity() {
        for (input, expected) in [
            (
                "-$1",
                Amount {
                    quantity: Decimal::new(-1, 0),
                    commodity: String::from("$"),
                },
            ),
            (
                "$-1",
                Amount {
                    quantity: Decimal::new(-1, 0),
                    commodity: String::from("$"),
                },
            ),
            (
                "+ $1",
                Amount {
                    quantity: Decimal::new(1, 0),
                    commodity: String::from("$"),
                },
            ),
            (
                "$-      1",
                Amount {
                    quantity: Decimal::new(-1, 0),
                    commodity: String::from("$"),
                },
            ),
            (
                "-1 USD",
                Amount {
                    quantity: Decimal::new(-1, 0),
                    commodity: String::from("USD"),
                },
            ),
        ] {
            let result = amount().then_ignore(end()).parse(input).into_result();
            assert_eq!(result, Ok(expected), "{input}");
        }
    }
}