From 965f1e74310ea1f92bbc2855dfc976d4290bec25 Mon Sep 17 00:00:00 2001 From: Jonathan Cornaz Date: Thu, 20 Oct 2022 21:18:20 +0200 Subject: [PATCH] fix: poptag being ignored --- src/lib.rs | 16 +++++++++++++++- tests/spec.rs | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3e9a175..dfc4822 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,6 +93,7 @@ impl<'a> Iterator for Parser<'a> { return Some(Ok(directive)); } Chunk::PushTag(tag) => self.tags.push(tag), + Chunk::PopTag(tag) => self.tags.retain(|&t| t != tag), Chunk::Comment => (), } } else { @@ -108,6 +109,7 @@ fn chunk(input: &str) -> IResult<&str, Chunk<'_>> { alt(( map(directive, Chunk::Directive), map(pushtag, Chunk::PushTag), + map(poptag, Chunk::PopTag), value(Chunk::Comment, tuple((not_line_ending, opt(line_ending)))), ))(input) } @@ -116,11 +118,16 @@ fn pushtag(input: &str) -> IResult<&str, &str> { preceded(tuple((tag("pushtag"), space1)), transaction::tag)(input) } +fn poptag(input: &str) -> IResult<&str, &str> { + preceded(tuple((tag("poptag"), space1)), transaction::tag)(input) +} + #[derive(Debug, Clone)] enum Chunk<'a> { Directive(Directive<'a>), Comment, PushTag(&'a str), + PopTag(&'a str), } #[cfg(test)] @@ -128,9 +135,16 @@ mod tests { use super::*; #[test] - fn next_pushtag() { + fn pushtag() { let input = "pushtag #test"; let (_, chunk) = chunk(input).expect("should successfully parse the input"); assert!(matches!(chunk, Chunk::PushTag("test"))); } + + #[test] + fn poptag() { + let input = "poptag #test"; + let (_, chunk) = chunk(input).expect("should successfully parse the input"); + assert!(matches!(chunk, Chunk::PopTag("test"))); + } } diff --git a/tests/spec.rs b/tests/spec.rs index a1d9f34..5f57abd 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -78,6 +78,26 @@ fn multiple_pushtags_add_tags_to_next_transaction() { assert_eq!(transaction.tags(), &["hello", "world"]); } +#[test] +fn poptag_removes_tag_from_stack() { + let input = "pushtag #hello\npoptag #hello\n2022-10-20 txn"; + let transaction = Parser::new(input) + .assert_single_directive() + .into_transaction() + .expect("should be a transaction"); + assert!(transaction.tags().is_empty()); +} + +#[test] +fn poptag_removes_only_concerned_tag_from_stack() { + let input = "pushtag #hello\npushtag #world\npoptag #hello\n2022-10-20 txn"; + let transaction = Parser::new(input) + .assert_single_directive() + .into_transaction() + .expect("should be a transaction"); + assert_eq!(transaction.tags(), &["world"]); +} + #[test] fn transaction_with_lot_date() { let beancount = r#"