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
//! Rust library for parsing [Ledger-cli](https://www.ledger-cli.org/) input files.
//!
//! Only a subset of the ledger-cli's file format is implemented.
//!
//! Supported elements:
//!
//! * Line comments (starting with: ``; # % | *``) except comments between postings
//!
//! * Inline comments (starting with ``;``)
//!
//! * Transaction headers with format:
//!
//!   ```
//!   DATE[=EDATE] [*|!] [(CODE)] DESC
//!   ```
//!
//! * Transaction postings with format (minimum two spaces or one tab between ``ACCOUNT`` and ``AMOUNT``):
//!
//!   ```
//!     ACCOUNT  AMOUNT [; NOTE]
//!   ```
//!
//! * Commodity prices with format:
//!
//!   ```
//!   P DATE SYMBOL PRICE
//!   ```

extern crate chrono;
extern crate nom;
extern crate rust_decimal;

mod model;
mod model_internal;
mod parser;

pub use model::*;

/// Parses ledger-cli source.
///
/// # Examples
///
/// ```
/// let result = parse(r#"; Example 1
/// 2018-10-01=2018-10-14 ! (123) Marek Ogarek
///  TEST:ABC 123  $1.20
///  TEST:ABC 123  $1.20"#);
/// ```
pub fn parse(input: &str) -> Result<Ledger, String> {
    use nom::types::CompleteStr;

    let result = parser::parse_ledger_items(CompleteStr(input));
    match result {
        Ok((CompleteStr(""), result)) => Ok(model_internal::convert_items_to_ledger(result)),
        Ok((rest, _)) => Err(rest.0.to_string()),
        Err(error) => Err(format!("{:?}", error)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::NaiveDate;
    use rust_decimal::Decimal;

    #[test]
    fn parse_ledger_test() {
        assert_eq!(
            parse(
                r#"; Example 1

P 2017-11-12 12:00:00 mBH 5.00 PLN

; Comment Line 1
; Comment Line 2
2018-10-01=2018-10-14 ! (123) Marek Ogarek
 TEST:ABC 123  $1.20; dd
 TEST:ABC 123  $1.20

2018-10-01=2018-10-14 ! (123) Marek Ogarek
 TEST:ABC 123  $1.20
 TEST:ABC 123  $1.20
"#
            ),
            Ok(Ledger {
                transactions: vec![
                    Transaction {
                        comment: Some("Comment Line 1\nComment Line 2".to_string()),
                        date: NaiveDate::from_ymd(2018, 10, 01),
                        effective_date: Some(NaiveDate::from_ymd(2018, 10, 14)),
                        status: Some(TransactionStatus::Pending),
                        code: Some("123".to_string()),
                        description: "Marek Ogarek".to_string(),
                        postings: vec![
                            Posting {
                                account: "TEST:ABC 123".to_string(),
                                amount: Amount {
                                    quantity: Decimal::new(120, 2),
                                    commodity: Commodity {
                                        name: "$".to_string(),
                                        position: CommodityPosition::Left
                                    }
                                },
                                status: None,
                                comment: Some("dd".to_string())
                            },
                            Posting {
                                account: "TEST:ABC 123".to_string(),
                                amount: Amount {
                                    quantity: Decimal::new(120, 2),
                                    commodity: Commodity {
                                        name: "$".to_string(),
                                        position: CommodityPosition::Left
                                    }
                                },
                                status: None,
                                comment: None
                            }
                        ]
                    },
                    Transaction {
                        comment: None,
                        date: NaiveDate::from_ymd(2018, 10, 01),
                        effective_date: Some(NaiveDate::from_ymd(2018, 10, 14)),
                        status: Some(TransactionStatus::Pending),
                        code: Some("123".to_string()),
                        description: "Marek Ogarek".to_string(),
                        postings: vec![
                            Posting {
                                account: "TEST:ABC 123".to_string(),
                                amount: Amount {
                                    quantity: Decimal::new(120, 2),
                                    commodity: Commodity {
                                        name: "$".to_string(),
                                        position: CommodityPosition::Left
                                    }
                                },
                                status: None,
                                comment: None
                            },
                            Posting {
                                account: "TEST:ABC 123".to_string(),
                                amount: Amount {
                                    quantity: Decimal::new(120, 2),
                                    commodity: Commodity {
                                        name: "$".to_string(),
                                        position: CommodityPosition::Left
                                    }
                                },
                                status: None,
                                comment: None
                            }
                        ]
                    }
                ],
                commodity_prices: vec![CommodityPrice {
                    datetime: NaiveDate::from_ymd(2017, 11, 12).and_hms(12, 00, 00),
                    commodity_name: "mBH".to_string(),
                    amount: Amount {
                        quantity: Decimal::new(500, 2),
                        commodity: Commodity {
                            name: "PLN".to_string(),
                            position: CommodityPosition::Right
                        }
                    }
                }]
            })
        );
    }

    #[test]
    fn parse_ledger_err_test() {
        assert_eq!(parse("wrong input"), Err("wrong input".to_string()));
    }
}