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
//! Types for representing an [`Transaction`]

use std::str;

use nom::{
    branch::alt,
    bytes::complete::{self, take_till},
    character::complete::{char, line_ending, space0, space1},
    combinator::{cut, eof, map, opt},
    multi::many0,
    sequence::{preceded, separated_pair, terminated, tuple},
    Parser,
};

use posting::posting;
pub use posting::{Posting, PriceType};

#[cfg(feature = "unstable")]
use crate::metadata::Metadata;
use crate::{
    date::date,
    string::{comment, string},
    Date,
};
use crate::{IResult, Span};

pub(crate) mod posting;

/// A transaction
///
/// Contains, a potential narration as well as the [`Posting`]s.
///
/// # Example
/// ```beancount
/// 2022-09-11 * "Coffee beans"
///   Expenses:Groceries   10 CHF
///   Assets:Bank
/// ```
#[derive(Debug, Clone)]
pub struct Transaction<'a> {
    date: Date,
    flag: Option<Flag>,
    payee: Option<String>,
    narration: Option<String>,
    tags: Vec<&'a str>,
    comment: Option<&'a str>,
    #[cfg(feature = "unstable")]
    metadata: Metadata<'a>,
    postings: Vec<Posting<'a>>,
}

/// The transaction flag
///
/// It is either cleared (`*`) of pending (`!`)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Flag {
    /// Cleared flag (the `*` character)
    Cleared,
    /// Pending flag (the `!` character)
    Pending,
}

impl Default for Flag {
    fn default() -> Self {
        Self::Cleared
    }
}

impl<'a> Transaction<'a> {
    /// Returns the "payee" if one was defined
    #[must_use]
    pub fn payee(&self) -> Option<&str> {
        self.payee.as_deref()
    }

    /// Returns the "narration" if one was defined
    #[must_use]
    pub fn narration(&self) -> Option<&str> {
        self.narration.as_deref()
    }

    /// Returns the metadata
    #[must_use]
    #[cfg(feature = "unstable")]
    pub fn metadata(&self) -> &Metadata<'a> {
        &self.metadata
    }

    /// Returns the postings
    #[must_use]
    pub fn postings(&self) -> &[Posting<'a>] {
        &self.postings
    }

    /// Returns the flag of the transaction (if present)
    #[must_use]
    pub fn flag(&self) -> Option<Flag> {
        self.flag
    }

    /// Returns the tags attached to this transaction
    #[must_use]
    pub fn tags(&self) -> &[&'a str] {
        &self.tags
    }

    /// Returns the comment (if present)
    #[must_use]
    pub fn comment(&self) -> Option<&str> {
        self.comment
    }

    /// The date of the transaction
    #[must_use]
    pub fn date(&self) -> Date {
        self.date
    }

    pub(crate) fn append_tags(&mut self, tags: &[&'a str]) {
        self.tags.extend(tags);
    }
}

pub(crate) fn transaction(input: Span<'_>) -> IResult<'_, Transaction<'_>> {
    let (input, date) = terminated(date, space1)(input)?;
    let (input, flag) = alt((map(flag, Some), map(complete::tag("txn"), |_| None)))(input)?;
    let (input, (payee, narration)) = payee_and_narration(input)?;
    let (input, tags) = many0(preceded(space0, tag))(input)?;
    let (input, _) = space0(input)?;
    let (input, comment) = opt(comment)(input)?;
    #[cfg_attr(not(feature = "unstable"), allow(unused_variables))]
    let (input, metadata) = crate::metadata::metadata(input)?;
    let (input, postings) = many0(preceded(tuple((line_ending, space1)), posting))(input)?;
    let (input, _) = cut(alt((line_ending, eof)))(input)?;
    Ok((
        input,
        Transaction {
            date,
            flag,
            payee,
            narration,
            tags,
            comment,
            #[cfg(feature = "unstable")]
            metadata,
            postings,
        },
    ))
}

fn payee_and_narration(input: Span<'_>) -> IResult<'_, (Option<String>, Option<String>)> {
    let (input, opt) = opt(preceded(
        space1,
        alt((
            separated_pair(string.map(Some), space1, string.map(Some)),
            string.map(|n| (None, Some(n))),
        )),
    ))(input)?;
    Ok((input, opt.unwrap_or((None, None))))
}

pub(crate) fn tag(input: Span<'_>) -> IResult<'_, &str> {
    preceded(
        char('#'),
        take_till(|c: char| c.is_whitespace() || c == '#').map(|s: Span<'_>| *s.fragment()),
    )(input)
}

fn flag(input: Span<'_>) -> IResult<'_, Flag> {
    alt((
        map(char('*'), |_| Flag::Cleared),
        map(char('!'), |_| Flag::Pending),
    ))(input)
}