From 0f1519302068913efcd122b3a4fcba4f147714ae Mon Sep 17 00:00:00 2001 From: forzafedor <139787392+forzafedor@users.noreply.github.com> Date: Mon, 24 Mar 2025 17:49:12 +0300 Subject: [PATCH 1/2] Fix called `Option::unwrap()` on a `None` value nom::combinator::ParserIterator::next() --- src/combinator/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index fe6655b98..0a644a635 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -925,7 +925,7 @@ where type Item = Output; fn next(&mut self) -> Option { - if let State::Running = self.state.take().unwrap() { + if let Some(State::Running) = self.state { let input = self.input.clone(); match (self.iterator).parse(input) { From a6bcf59fde571c8c5d533d385697d2c5bab7b23e Mon Sep 17 00:00:00 2001 From: forzafedor <139787392+forzafedor@users.noreply.github.com> Date: Mon, 24 Mar 2025 17:52:20 +0300 Subject: [PATCH 2/2] Add test for nom::combinator::ParserIterator::next function --- src/combinator/tests.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index d02713f26..3530f4d29 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -282,3 +282,26 @@ fn fail_test() { Err(Err::Error((b, ErrorKind::Fail))) ); } + +#[test] +fn parser_iterator_test() { + use crate::character::complete::alpha1; + use crate::sequence::terminated; + use crate::IResult; + + let input: &str = "next:test:"; + + let mut nom_it = iterator(input, terminated(alpha1, tag(":"))); + + assert_eq!(Some("next"), nom_it.next()); + assert_eq!(Some("test"), nom_it.next()); + + // and then None once it's over. + assert_eq!(None, nom_it.next()); + + // None for more calls + assert_eq!(None, nom_it.next()); + + let parser_result: IResult<_, _> = nom_it.finish(); + assert_eq!(Ok(("", ())), parser_result); +}