Skip to content

Commit

Permalink
internal: parse posting cost with chumsky
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornaz committed Mar 3, 2024
1 parent a2a2198 commit b90ea91
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<D>()).or_not())
.then(
choice((
just('@')
Expand All @@ -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(),
})
Expand Down Expand Up @@ -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<Cost<i32>>) {
let posting: Posting<i32> = posting().parse(input).unwrap();
assert_eq!(posting.cost, expected);
}

#[rstest]
fn should_parse_empty_cost(#[values("{}", "{ }")] input: &str) {
let cost: Cost<i32> = cost().parse(input).unwrap();
Expand Down

0 comments on commit b90ea91

Please sign in to comment.