diff --git a/src/parsers.rs b/src/parsers.rs index e9059c5..591957b 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -217,22 +217,38 @@ named!( pub struct ParserIterator<'a> { dat: &'a[u8], + done: bool } impl<'a> Iterator for ParserIterator<'a> { type Item = Output<'a>; fn next(&mut self) -> Option { - let parse = parse_output(self.dat.as_bytes()) - .unwrap(); + if self.done { + return None; + } + let parse = parse_output(self.dat); - self.dat = parse.0; - Some(parse.1) + if parse.is_ok() { + let parse = parse.unwrap(); + + self.dat = parse.0; + Some(parse.1) + }else{ + if self.done { + None + }else{ + self.done = true; + Some(Output::TextBlock(std::str::from_utf8(self.dat) + .unwrap())) + } + } } } -pub fn iterate_on<'a>(bytes: &'a[u8]) -> ParserIterator<'a> { +pub fn iterate_on<'a>(bytes: &'a str) -> ParserIterator<'a> { ParserIterator { - dat: bytes + dat: bytes.as_bytes(), + done: false } } diff --git a/src/parsers/tests.rs b/src/parsers/tests.rs index 3c408a2..eec8b40 100644 --- a/src/parsers/tests.rs +++ b/src/parsers/tests.rs @@ -35,8 +35,7 @@ fn test_reset_mode() { fn test_parser_iterator() { let parse_str = "Hello, world? How are \x27[=7lyou? I hope you're doing well."; - let strings: Vec = iterate_on(parse_str.as_bytes()) - .take(2) + let strings: Vec = iterate_on(parse_str) .collect(); println!("{:#?}", strings);