From 2fba2e4e496a9cc8f37351e3b7b889b5e73606c5 Mon Sep 17 00:00:00 2001 From: "C. K. Young" Date: Tue, 31 Jan 2023 02:28:55 -0500 Subject: [PATCH] Enable `Parser`-based `ParserIterator` to work. --- src/combinator/mod.rs | 8 ++++---- src/combinator/tests.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/combinator/mod.rs b/src/combinator/mod.rs index 4810568e3..f85e17c74 100644 --- a/src/combinator/mod.rs +++ b/src/combinator/mod.rs @@ -736,18 +736,18 @@ impl ParserIterator { } } -impl<'a, Input, Output, Error, F> core::iter::Iterator for &'a mut ParserIterator +impl<'a, Input, F> core::iter::Iterator for &'a mut ParserIterator where - F: FnMut(Input) -> IResult, + F: Parser, Input: Clone, { - type Item = Output; + type Item = F::Output; fn next(&mut self) -> Option { if let State::Running = self.state.take().unwrap() { let input = self.input.clone(); - match (self.iterator)(input) { + match self.iterator.parse(input) { Ok((i, o)) => { self.input = i; self.state = Some(State::Running); diff --git a/src/combinator/tests.rs b/src/combinator/tests.rs index d209e2b11..9adcf4cb3 100644 --- a/src/combinator/tests.rs +++ b/src/combinator/tests.rs @@ -273,3 +273,15 @@ fn fail_test() { assert_eq!(fail::<_, &str, _>(a), Err(Err::Error((a, ErrorKind::Fail)))); assert_eq!(fail::<_, &str, _>(b), Err(Err::Error((b, ErrorKind::Fail)))); } + +#[test] +fn test_iterator() { + use crate::character::complete::{line_ending, not_line_ending}; + + let input = "foo\nbar\r\nbaz"; + let mut iter = iterator::<_, (), _>(input, (not_line_ending, line_ending)); + assert_eq!((&mut iter).next(), Some(("foo", "\n"))); + assert_eq!((&mut iter).next(), Some(("bar", "\r\n"))); + assert_eq!((&mut iter).next(), None); + assert_eq!(iter.finish(), Ok(("baz", ()))); +}