done, kinda hacky solution but it works

This commit is contained in:
David Bittner
2019-04-26 20:01:34 -04:00
parent c2b8f5ed57
commit 3ef51ec9e0
2 changed files with 23 additions and 8 deletions

View File

@@ -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<Self::Item> {
let parse = parse_output(self.dat.as_bytes())
.unwrap();
if self.done {
return None;
}
let parse = parse_output(self.dat);
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
}
}

View File

@@ -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<Output> = iterate_on(parse_str.as_bytes())
.take(2)
let strings: Vec<Output> = iterate_on(parse_str)
.collect();
println!("{:#?}", strings);