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

use crate::component::amount::{amount, Amount};
use crate::component::commodity::commodity as parse_commodity;
use crate::component::whitespace::whitespace;
use crate::state::State;
use crate::utils::end_of_line;

#[derive(Clone, Debug, PartialEq)]
pub enum Commodity {
    Amount(Amount),
    Commodity(String),
}

pub fn commodity<'a>() -> impl Parser<'a, &'a str, Commodity, extra::Full<Rich<'a, char>, State, ()>>
{
    just("commodity")
        .ignore_then(whitespace().repeated().at_least(1))
        .ignore_then(
            amount()
                .map(Commodity::Amount)
                .or(parse_commodity().map(Commodity::Commodity)),
        )
        .then_ignore(end_of_line())
}

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

    use super::*;

    #[test]
    fn with_symbol() {
        let result = commodity()
            .then_ignore(end())
            .parse("commodity $1000.00")
            .into_result();
        assert_eq!(
            result,
            Ok(Commodity::Amount(Amount {
                commodity: String::from("$"),
                quantity: Decimal::new(100_000, 2),
            }))
        );
    }

    #[test]
    fn no_symbol() {
        let result = commodity()
            .then_ignore(end())
            .parse("commodity 1,000,000.0000")
            .into_result();
        assert_eq!(
            result,
            Ok(Commodity::Amount(Amount {
                commodity: String::new(),
                quantity: Decimal::new(10_000_000_000, 4),
            }))
        );
    }

    #[test]
    fn comment() {
        let result = commodity()
            .then_ignore(end())
            .parse("commodity 1. USD ; with comment")
            .into_result();
        assert_eq!(
            result,
            Ok(Commodity::Amount(Amount {
                commodity: String::from("USD"),
                quantity: Decimal::new(1, 0),
            }))
        );
    }

    #[test]
    fn just_currency() {
        let result = commodity()
            .then_ignore(end())
            .parse("commodity \"AAAA 2023\"  ")
            .into_result();
        assert_eq!(result, Ok(Commodity::Commodity(String::from("AAAA 2023"))));
    }
}