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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

//! A parsing library for the [beancount language](https://beancount.github.io/docs/beancount_language_syntax.html)
//!
//! # Usage
//!
//! Use [`parse`] to get an instance of [`BeancountFile`].
//!
//! This is generic over the decimal type. The examples use `f64` as a decimal type.
//! You may also use `Decimal` from the [rust_decimal crate].
//!
//! [rust_decimal crate]: https://docs.rs/rust_decimal
//!
//! ```
//! use beancount_parser::{BeancountFile, DirectiveContent};
//!
//! # fn main() -> Result<(), beancount_parser::Error> {
//! let input = r#"
//! 2023-05-20 * "Coffee beans"
//!   Expenses:Groceries   10 CHF
//!   Assets:Checking
//! "#;
//!
//! // Parse into the `BeancountFile` struct:
//! let beancount: BeancountFile<f64> = input.parse()?;
//!
//! let directive = &beancount.directives[0];
//! assert_eq!(directive.date.year, 2023);
//! assert_eq!(directive.date.month, 5);
//! assert_eq!(directive.date.day, 20);
//!
//! let DirectiveContent::Transaction(trx) = &directive.content else {
//!     panic!("was not a transaction")
//! };
//! assert_eq!(trx.narration.as_deref(), Some("Coffee beans"));
//! assert_eq!(trx.postings[0].account.as_str(), "Expenses:Groceries");
//! assert_eq!(trx.postings[0].amount.as_ref().unwrap().value, 10.0);
//! assert_eq!(trx.postings[0].amount.as_ref().unwrap().currency.as_str(), "CHF");
//! assert_eq!(trx.postings[1].account.as_str(), "Assets:Checking");
//! assert_eq!(trx.postings[1].amount, None);
//! # Ok(()) }
//! ```

use std::{collections::HashSet, fs::File, io::Read, path::PathBuf, str::FromStr};

use nom::{
    branch::alt,
    bytes::complete::{tag, take_while},
    character::complete::{char, line_ending, not_line_ending, space0, space1},
    combinator::{all_consuming, cut, eof, iterator, map, not, opt, value},
    sequence::{delimited, preceded, terminated, tuple},
    Finish, Parser,
};
use nom_locate::position;

use crate::iterator::Iter;
pub use crate::{
    account::{Account, Balance, Close, Open, Pad},
    amount::{Amount, Currency, Decimal, Price},
    date::Date,
    error::{ConversionError, Error, ReadFileError},
    event::Event,
    transaction::{Cost, Link, Posting, PostingPrice, Tag, Transaction},
};

#[deprecated(note = "use `metadata::Value` instead", since = "1.0.0-beta.3")]
#[doc(hidden)]
pub type MetadataValue<D> = metadata::Value<D>;

mod account;
mod amount;
mod date;
mod error;
mod event;
mod iterator;
pub mod metadata;
mod transaction;

/// Parse the input beancount file and return an instance of [`BeancountFile`] on success
///
/// It is generic over the [`Decimal`] type `D`.
///
/// See the root crate documentation for an example.
///
/// # Errors
///
/// Returns an [`Error`] in case of invalid beancount syntax found.
pub fn parse<D: Decimal>(input: &str) -> Result<BeancountFile<D>, Error> {
    input.parse()
}

/// Parse the beancount file and return an iterator over `Result<Entry<D>, Result>`
///
/// It is generic over the [`Decimal`] type `D`.
///
/// See [`Entry`]
///
/// # Errors
///
/// The iterator will emit an [`Error`] in case of invalid beancount syntax found.
pub fn parse_iter<'a, D: Decimal + 'a>(
    input: &'a str,
) -> impl Iterator<Item = Result<Entry<D>, Error>> + 'a {
    Iter::new(input, iterator(Span::new(input), entry::<D>))
}

impl<D: Decimal> FromStr for BeancountFile<D> {
    type Err = Error;
    fn from_str(input: &str) -> Result<Self, Self::Err> {
        parse_iter(input).collect()
    }
}

/// Read the files from disk and parse their content.
///
/// It follows the `include` directives found.
///
/// # Errors
///
/// Returns an error if any file could not be read (IO error)
/// or if there is a beancount syntax error in any file read
pub fn read_files<D: Decimal, F: FnMut(Entry<D>)>(
    files: impl IntoIterator<Item = PathBuf>,
    mut on_entry: F,
) -> Result<(), ReadFileError> {
    let mut loaded: HashSet<PathBuf> = HashSet::new();
    let mut pending: Vec<PathBuf> = files
        .into_iter()
        .map(|p| p.canonicalize())
        .collect::<Result<_, _>>()?;
    let mut buffer = String::new();
    while let Some(path) = pending.pop() {
        if loaded.contains(&path) {
            continue;
        }
        loaded.insert(path.clone());
        buffer.clear();
        File::open(&path)?.read_to_string(&mut buffer)?;
        for result in parse_iter::<D>(&buffer) {
            let entry = result?;
            match entry {
                Entry::Include(include) => {
                    let path = if include.is_relative() {
                        let Some(parent) = path.parent() else {
                            unreachable!("there must be a parent if the file was valid")
                        };
                        parent.join(include)
                    } else {
                        include
                    };
                    let path = path.canonicalize()?;
                    if !loaded.contains(&path) {
                        pending.push(path);
                    }
                }
                entry => on_entry(entry),
            }
        }
    }
    Ok(())
}

/// Main struct representing a parsed beancount file.
///
/// To get an instance of this, use [`parse`].
///
/// For an example, look at the root crate documentation.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BeancountFile<D> {
    /// List of beancount options
    ///
    /// See: <https://beancount.github.io/docs/beancount_language_syntax.html#options>
    pub options: Vec<BeanOption>,
    /// Paths of include directives
    ///
    /// See: <https://beancount.github.io/docs/beancount_language_syntax.html#includes>
    pub includes: Vec<PathBuf>,
    /// List of [`Directive`] found in the file
    pub directives: Vec<Directive<D>>,
}

impl<D> Default for BeancountFile<D> {
    fn default() -> Self {
        Self {
            options: Vec::new(),
            includes: Vec::new(),
            directives: Vec::new(),
        }
    }
}

impl<D> BeancountFile<D> {
    /// Returns the first value found for the option
    ///
    /// If the option is declared multiple times, this function returns the first one found.
    ///
    /// See [`Self::options`] to get all declared options.
    ///
    /// Syntax: <https://beancount.github.io/docs/beancount_language_syntax.html#options>
    ///
    /// # Example
    ///
    /// ```
    /// use beancount_parser::BeancountFile;
    /// let input = r#"
    /// option "favorite_color" "blue"
    /// option "operating_currency" "CHF"
    /// option "operating_currency" "PLN"
    /// "#;
    /// let beancount: BeancountFile<f64> = input.parse().unwrap();
    /// assert_eq!(beancount.option("favorite_color"), Some("blue"));
    /// assert_eq!(beancount.option("operating_currency"), Some("CHF"));
    /// assert_eq!(beancount.option("foo"), None);
    /// ```
    #[must_use]
    pub fn option(&self, key: &str) -> Option<&str> {
        self.options
            .iter()
            .find(|opt| opt.name == key)
            .map(|opt| &opt.value[..])
    }
}

impl<D> Extend<Entry<D>> for BeancountFile<D> {
    fn extend<T: IntoIterator<Item = Entry<D>>>(&mut self, iter: T) {
        for entry in iter {
            match entry {
                Entry::Directive(d) => self.directives.push(d),
                Entry::Option(o) => self.options.push(o),
                Entry::Include(p) => self.includes.push(p),
            }
        }
    }
}

impl<D> FromIterator<Entry<D>> for BeancountFile<D> {
    fn from_iter<T: IntoIterator<Item = Entry<D>>>(iter: T) -> Self {
        let mut file = BeancountFile::default();
        file.extend(iter);
        file
    }
}

/// A beancount "directive"
///
/// It has fields common to all directives, and a [`Directive::content`] field with
/// a different content for each directive type.
///
/// ```
/// # use beancount_parser::{BeancountFile, DirectiveContent};
/// let input = r#"
/// 2022-01-01 open Assets:Cash
/// 2022-01-01 * "Grocery shopping"
///   Expenses:Groceries  10 CHF
///   Assets:Cash
/// "#;
/// let beancount: BeancountFile<f64> = input.parse().unwrap();
/// assert_eq!(beancount.directives.len(), 2);
/// for directive in beancount.directives {
///    println!("line: {}", directive.line_number);
///    println!("metadata: {:#?}", directive.metadata);
///    match directive.content {
///       DirectiveContent::Open(open) => println!("open account directive: {open:?}"),
///       DirectiveContent::Transaction(trx) => println!("transaction: {trx:?}"),
///       other => println!("unknown directive: {other:?}"),
///    }
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Directive<D> {
    /// Date of the directive
    pub date: Date,
    /// Content of the directive that is specific to each directive type
    pub content: DirectiveContent<D>,
    /// Metadata associated to the directive
    ///
    /// See the [`metadata`] module for more
    pub metadata: metadata::Map<D>,
    /// Line number where the directive was found in the input file
    pub line_number: u32,
}

impl<D: Decimal> FromStr for Directive<D> {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match all_consuming(directive)(Span::new(s)).finish() {
            Ok((_, d)) => Ok(d),
            Err(err) => Err(Error::new(s, err.input)),
        }
    }
}

/// Directive specific content
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum DirectiveContent<D> {
    Transaction(Transaction<D>),
    Price(Price<D>),
    Balance(Balance<D>),
    Open(Open),
    Close(Close),
    Pad(Pad),
    Commodity(Currency),
    Event(Event),
}

type Span<'a> = nom_locate::LocatedSpan<&'a str>;
type IResult<'a, O> = nom::IResult<Span<'a>, O>;

/// Entry in the beancount syntax
///
/// It is more general than `Directive` as an entry can also be option or an include.
#[allow(missing_docs)]
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum Entry<D> {
    Directive(Directive<D>),
    Option(BeanOption),
    Include(PathBuf),
}

enum RawEntry<D> {
    Directive(Directive<D>),
    Option(BeanOption),
    Include(PathBuf),
    PushTag(Tag),
    PopTag(Tag),
    Comment,
}

/// An beancount option
///
/// See: <https://beancount.github.io/docs/beancount_language_syntax.html#options>
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BeanOption {
    /// Name of the option
    pub name: String,
    /// Value of the option
    pub value: String,
}

fn entry<D: Decimal>(input: Span<'_>) -> IResult<'_, RawEntry<D>> {
    alt((
        directive.map(RawEntry::Directive),
        option.map(|(name, value)| RawEntry::Option(BeanOption { name, value })),
        include.map(|p| RawEntry::Include(p)),
        tag_stack_operation,
        line.map(|()| RawEntry::Comment),
    ))(input)
}

fn directive<D: Decimal>(input: Span<'_>) -> IResult<'_, Directive<D>> {
    let (input, position) = position(input)?;
    let (input, date) = date::parse(input)?;
    let (input, _) = cut(space1)(input)?;
    let (input, (content, metadata)) = alt((
        map(transaction::parse, |(t, m)| {
            (DirectiveContent::Transaction(t), m)
        }),
        tuple((
            terminated(
                alt((
                    map(
                        preceded(tag("price"), cut(preceded(space1, amount::price))),
                        DirectiveContent::Price,
                    ),
                    map(
                        preceded(tag("balance"), cut(preceded(space1, account::balance))),
                        DirectiveContent::Balance,
                    ),
                    map(
                        preceded(tag("open"), cut(preceded(space1, account::open))),
                        DirectiveContent::Open,
                    ),
                    map(
                        preceded(tag("close"), cut(preceded(space1, account::close))),
                        DirectiveContent::Close,
                    ),
                    map(
                        preceded(tag("pad"), cut(preceded(space1, account::pad))),
                        DirectiveContent::Pad,
                    ),
                    map(
                        preceded(tag("commodity"), cut(preceded(space1, amount::currency))),
                        DirectiveContent::Commodity,
                    ),
                    map(
                        preceded(tag("event"), cut(preceded(space1, event::parse))),
                        DirectiveContent::Event,
                    ),
                )),
                end_of_line,
            ),
            metadata::parse,
        )),
    ))(input)?;
    Ok((
        input,
        Directive {
            date,
            content,
            metadata,
            line_number: position.location_line(),
        },
    ))
}

fn option(input: Span<'_>) -> IResult<'_, (String, String)> {
    let (input, _) = tag("option")(input)?;
    let (input, key) = preceded(space1, string)(input)?;
    let (input, value) = preceded(space1, string)(input)?;
    let (input, ()) = end_of_line(input)?;
    Ok((input, (key, value)))
}

fn include(input: Span<'_>) -> IResult<'_, PathBuf> {
    let (input, _) = tag("include")(input)?;
    let (input, path) = cut(delimited(space1, string, end_of_line))(input)?;
    Ok((input, path.into()))
}

fn tag_stack_operation<D>(input: Span<'_>) -> IResult<'_, RawEntry<D>> {
    alt((
        preceded(tuple((tag("pushtag"), space1)), transaction::parse_tag).map(RawEntry::PushTag),
        preceded(tuple((tag("poptag"), space1)), transaction::parse_tag).map(RawEntry::PopTag),
    ))(input)
}

fn end_of_line(input: Span<'_>) -> IResult<'_, ()> {
    let (input, _) = space0(input)?;
    let (input, _) = opt(comment)(input)?;
    let (input, _) = alt((line_ending, eof))(input)?;
    Ok((input, ()))
}

fn comment(input: Span<'_>) -> IResult<'_, ()> {
    let (input, _) = char(';')(input)?;
    let (input, _) = not_line_ending(input)?;
    Ok((input, ()))
}

fn line(input: Span<'_>) -> IResult<'_, ()> {
    let (input, _) = not_line_ending(input)?;
    let (input, _) = line_ending(input)?;
    Ok((input, ()))
}

fn empty_line(input: Span<'_>) -> IResult<'_, ()> {
    let (input, ()) = not(eof)(input)?;
    end_of_line(input)
}

fn string(input: Span<'_>) -> IResult<'_, String> {
    let (input, _) = char('"')(input)?;
    let mut string = String::new();
    let take_data = take_while(|c: char| c != '"' && c != '\\');
    let (mut input, mut part) = take_data(input)?;
    while !part.fragment().is_empty() {
        string.push_str(part.fragment());
        let (new_input, escaped) =
            opt(alt((value('"', tag("\\\"")), value('\\', tag("\\\\")))))(input)?;
        let Some(escaped) = escaped else { break };
        string.push(escaped);
        let (new_input, new_part) = take_data(new_input)?;
        input = new_input;
        part = new_part;
    }
    let (input, _) = char('"')(input)?;
    Ok((input, string))
}

#[cfg(test)]
type ChumskyError = chumsky::error::Simple<char>;

#[cfg(test)]
trait ChumskyParser<O>: chumsky::Parser<char, O, Error = ChumskyError> {}

#[cfg(test)]
impl<O, P: chumsky::Parser<char, O, Error = ChumskyError>> ChumskyParser<O> for P {}

#[cfg(test)]
mod chumksy {
    use chumsky::prelude::*;

    use crate::ChumskyParser;

    pub(crate) fn string() -> impl ChumskyParser<String> {
        choice((just("\\\"").to('"'), just("\\\\").to('\\'), just('"').not()))
            .repeated()
            .delimited_by(just('"'), just('"'))
            .collect()
            .labelled("string")
    }

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

        #[rstest]
        #[case::empty("\"\"", "")]
        #[case::normal("\"hello\"", "hello")]
        #[case::escaped_quote("\"hello \\\"world\\\"\"", "hello \"world\"")]
        #[case::escaped_backslash("\"hello\\\\world\"", "hello\\world")]
        fn should_parse_valid_string(#[case] input: &str, #[case] expected: &str) {
            let string: String = string().then_ignore(end()).parse(input).unwrap();
            assert_eq!(string, expected);
        }

        #[rstest]
        #[case::nothing("")]
        #[case::not_quoted("hello")]
        #[case::not_closed("\"hello")]
        #[case::not_closed_escaped("\"hello\\\"")]
        fn should_not_parse_invalid_string(#[case] input: &str) {
            let result: Result<String, _> = string().then_ignore(end()).parse(input);
            assert!(result.is_err(), "{result:?}");
        }
    }
}