|
@@ -2,7 +2,7 @@ use crate::data::{
|
|
|
AccountName, DataError, Decimal, SourceFile, Span, Spanned, UnitName, spec::SpecRoot,
|
|
|
};
|
|
|
|
|
|
-use super::{Balance, LedgerEntry};
|
|
|
+use super::{Balance, Transaction, LedgerEntry};
|
|
|
|
|
|
use chumsky::{prelude::*, text::inline_whitespace};
|
|
|
|
|
@@ -11,7 +11,7 @@ type InputWithContext<'a> = chumsky::input::WithContext<Span, &'a str>;
|
|
|
fn ledger_parser<'a>() -> impl Parser<
|
|
|
'a,
|
|
|
InputWithContext<'a>,
|
|
|
- Vec<Spanned<LedgerEntry>>,
|
|
|
+ Vec<LedgerEntry>,
|
|
|
chumsky::extra::Full<
|
|
|
chumsky::error::Rich<'a, char, Span>,
|
|
|
chumsky::extra::SimpleState<&'a SpecRoot>,
|
|
@@ -101,7 +101,7 @@ fn ledger_parser<'a>() -> impl Parser<
|
|
|
.then_ignore(mark(']'))
|
|
|
.map(|v: &str| String::from(v.trim()));
|
|
|
|
|
|
- let entry = group((
|
|
|
+ let transaction = group((
|
|
|
chumsky::text::whitespace(),
|
|
|
datestamp,
|
|
|
mark(':'),
|
|
@@ -122,26 +122,28 @@ fn ledger_parser<'a>() -> impl Parser<
|
|
|
))
|
|
|
.map_with(
|
|
|
|(_, datestamp, _, _, title, _, annotations, balances, _), e| {
|
|
|
- Spanned::new(
|
|
|
- LedgerEntry {
|
|
|
+ LedgerEntry::Transaction(Spanned::new(
|
|
|
+ Transaction {
|
|
|
datestamp,
|
|
|
title: (!title.is_empty()).then_some(title),
|
|
|
annotations,
|
|
|
balances,
|
|
|
},
|
|
|
e.span(),
|
|
|
- )
|
|
|
+ ))
|
|
|
},
|
|
|
);
|
|
|
|
|
|
- entry.repeated().collect()
|
|
|
+ let comment = mark('#').ignore_then(none_of("\n").repeated()).padded().to_slice().map_with(|s: &str, e| LedgerEntry::Comment(Spanned::new(s.into(), e.span())));
|
|
|
+
|
|
|
+ (transaction.or(comment)).repeated().collect()
|
|
|
}
|
|
|
|
|
|
pub fn parse_ledger(
|
|
|
source: SourceFile,
|
|
|
spec: &SpecRoot,
|
|
|
data: &str,
|
|
|
-) -> Result<Vec<Spanned<LedgerEntry>>, DataError> {
|
|
|
+) -> Result<Vec<LedgerEntry>, DataError> {
|
|
|
let parser = ledger_parser();
|
|
|
|
|
|
let (presult, errors) = parser
|