From 55be4582404faefc91a507d3fffbd73383dabe46 Mon Sep 17 00:00:00 2001 From: Tomasz Zurkowski Date: Sun, 7 Jan 2024 11:31:20 +0100 Subject: [PATCH] allow commodities that end with numbers --- src/amount.rs | 7 ++++++- tests/parser_spec.rs | 10 +++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/amount.rs b/src/amount.rs index 9d30c13..146cf70 100644 --- a/src/amount.rs +++ b/src/amount.rs @@ -173,7 +173,12 @@ pub(crate) fn currency(input: Span<'_>) -> IResult<'_, Currency> { take_while(|c: char| { c.is_uppercase() || c.is_numeric() || c == '-' || c == '_' || c == '.' || c == '\'' }), - |s: &Span<'_>| s.fragment().chars().last().map_or(true, char::is_uppercase), + |s: &Span<'_>| { + s.fragment() + .chars() + .last() + .map_or(true, |c| c.is_uppercase() || c.is_numeric()) + }, ), )))(input)?; Ok((input, Currency(Arc::from(*currency.fragment())))) diff --git a/tests/parser_spec.rs b/tests/parser_spec.rs index ad2f478..f2d107b 100644 --- a/tests/parser_spec.rs +++ b/tests/parser_spec.rs @@ -239,6 +239,15 @@ fn should_parse_commodity() { assert_eq!(commodity.as_str(), "USD"); } +#[rstest] +fn should_parse_commodity_that_ends_with_number() { + let input = "1792-01-01 commodity A1"; + let DirectiveContent::Commodity(commodity) = parse_single_directive(input).content else { + panic!("was not an commodity directive"); + }; + assert_eq!(commodity.as_str(), "A1"); +} + #[rstest] fn should_parse_event() { let input = "2020-12-09 event \"location\" \"New Metropolis\""; @@ -377,7 +386,6 @@ fn should_reject_invalid_input( "2014-05-01 open Assets:Checking Hello", "2014-05-01 open Assets:Checking USD CHF", "2014-05-01 open Assets:Checking 1SD", - "2014-05-01 open Assets:Checking US2", "2014-05-01 open Assets:Checking US-", "2014-05-01 open Assets:Checking -US", "2014-05-01close Assets:Cash",