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
#![deny(
    future_incompatible,
    nonstandard_style,
    unsafe_code,
    private_in_public,
    missing_docs,
    unused_results,
    missing_docs
)]
#![warn(rust_2018_idioms, clippy::pedantic)]
#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
#![cfg_attr(
    not(test),
    warn(
        missing_debug_implementations,
        clippy::get_unwrap,
        clippy::unwrap_in_result,
        clippy::unwrap_used
    )
)]
#![cfg_attr(nightly, feature(doc_auto_cfg))]

//! A rust parsing library for [beancount](https://beancount.github.io/docs/) files
//!
//! At its core, this library provides is a [`Parser`] type that
//! is an iterator over the directives.
//!
//! ## Example
//! ```
//! use beancount_parser::{Directive, Parser, Error};
//!
//! # fn main() -> Result<(), Error> {
//! let beancount = r#"
//! 2022-09-11 * "Coffee beans"
//!   Expenses:Groceries   10 CHF
//!   Assets:Bank
//! "#;
//!
//! let directives: Vec<Directive<'_>> = Parser::new(beancount).collect::<Result<_, _>>()?;
//! let transaction = directives[0].as_transaction().unwrap();
//! assert_eq!(transaction.narration(), Some("Coffee beans"));
//!
//! let first_posting_amount = transaction.postings()[0].amount().unwrap();
//! assert_eq!(first_posting_amount.currency(), "CHF");
//! assert_eq!(first_posting_amount.value().try_into_f64()?, 10.0);
//! # Ok(()) }
//! ```

#[cfg(test)]
#[macro_use]
extern crate rstest;

pub use crate::include::Include;
pub use crate::nom_parser::Parser;
pub use crate::{
    account::{Account, Type},
    amount::Amount,
    assertion::Assertion,
    close::Close,
    date::Date,
    directive::Directive,
    error::Error,
    open::Open,
    pad::Pad,
    price::Price,
    transaction::Transaction,
};

#[cfg(all(test, feature = "unstable"))]
pub use crate::commodity::Commodity;

#[cfg(all(test, feature = "unstable"))]
pub use crate::option::Option;

pub mod account;
pub mod amount;
mod assertion;
mod close;
mod commodity;
mod date;
mod directive;
mod error;
mod include;
mod metadata;
mod nom_parser;
mod open;
mod option;
mod pad;
mod pest_parser;
mod price;
mod string;
pub mod transaction;