From b90ea91fa5612a7d17d604dbd1e206ab14b85115 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Sun, 3 Mar 2024 13:54:50 +0100 Subject: [PATCH] internal: parse posting cost with chumsky --- src/transaction.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/transaction.rs b/src/transaction.rs index bd49c47..236e295 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -405,6 +405,7 @@ mod chumsky { .then(crate::account::chumksy::account()) .then_ignore(whitespace()) .then(crate::amount::chumsky::amount().or_not()) + .then(whitespace().ignore_then(cost::()).or_not()) .then( choice(( just('@') @@ -418,11 +419,11 @@ mod chumsky { )) .or_not(), ) - .map(|(((flag, account), amount), price)| Posting { + .map(|((((flag, account), amount), cost), price)| Posting { flag, account, amount, - cost: None, + cost, price, metadata: HashMap::new(), }) @@ -502,6 +503,16 @@ mod chumsky { assert_eq!(posting.price, expected); } + #[rstest] + #[case::none("Assets:Cash 1 CHF", None)] + #[case::empty("Assets:Cash 1 CHF {}", Some(Cost::default()))] + #[case::some("Assets:Cash 1 CHF {2023-03-03}", Some(Cost { date: Some(Date::new(2023,3,3)), ..Cost::default() }))] + #[case::some_before_price("Assets:Cash 1 CHF {2023-03-03} @ 3 PLN", Some(Cost { date: Some(Date::new(2023,3,3)), ..Cost::default() }))] + fn should_parse_posting_cost(#[case] input: &str, #[case] expected: Option>) { + let posting: Posting = posting().parse(input).unwrap(); + assert_eq!(posting.cost, expected); + } + #[rstest] fn should_parse_empty_cost(#[values("{}", "{ }")] input: &str) { let cost: Cost = cost().parse(input).unwrap();