incomplete implementation of pull parser

This commit is contained in:
Geoffroy Couprie 2014-11-29 16:24:21 +01:00
parent 4fb62b658e
commit 5c35514038

View File

@ -10,6 +10,7 @@ use std::io::fs::File;
use std::io::{IoResult, IoErrorKind};
use self::IResult::*;
use self::ProducerState::*;
use self::ConsumerState::*;
use std::kinds::Sized;
use std::str;
@ -432,9 +433,7 @@ macro_rules! pusher (
break;
},
Incomplete(_) => {
println!("incomplete, stopping (BUT SHOULDN'T)");
//acc = v2;
//v2 = Vec::new();
println!("incomplete");
},
Done(i, _) => {
println!("data, done");
@ -447,6 +446,80 @@ macro_rules! pusher (
);
)
#[deriving(Show,PartialEq,Eq)]
pub enum ConsumerState {
Await,
//Incomplete,
ConsumerDone,
ConsumerError(Err)
}
pub trait Consumer {
fn consume(&mut self, input: &[u8]) -> ConsumerState;
fn run(&mut self, producer: &mut Producer) {
let mut acc: Vec<u8> = Vec::new();
//let mut v2: Vec<u8> = Vec::new();
loop {
let state = producer.produce();
match state {
Data(v) => {
println!("got data");
acc.push_all(v)
},
Eof([]) => {
println!("eof empty");
break;
}
Eof(v) => {
println!("eof with {} bytes", v.len());
acc.push_all(v)
}
_ => {break;}
}
//v2.push_all(acc.as_slice());
//match consumer.consume(v2.as_slice()) {
match self.consume(acc.as_slice()) {
ConsumerError(e) => {
println!("consumer error, stopping: {}", e);
},
ConsumerDone => {
println!("data, done");
acc.clear();
//acc.push_all(i);
break;
},
Await => {
println!("await");
acc.clear();
//acc.push_all(i);
}
}
}
}
}
struct TestPrintConsumer {
counter: uint
}
impl TestPrintConsumer {
fn new() -> TestPrintConsumer {
TestPrintConsumer { counter: 0 }
}
}
impl Consumer for TestPrintConsumer {
fn consume(&mut self, input: &[u8]) -> ConsumerState {
println!("{} -> {}", self.counter, str::from_utf8(input).unwrap());
self.counter = self.counter + 1;
if self.counter <=4 {
Await
} else {
ConsumerDone
}
}
}
pub fn print<'a,T: Show>(input: T) -> IResult<T, ()> {
println!("{}", input);
Done(input, ())
@ -754,6 +827,14 @@ fn accu_test_2() {
ps(&mut p);
//assert!(false);
}
#[test]
fn pull_test() {
let mut p = MemProducer::new("abcdefghijklmnopqrstuvwx".as_bytes(), 4);
let mut c = TestPrintConsumer::new();
c.run(&mut p);
}
/* FIXME: this makes rustc weep
fn pr(par: IResult<(),&[u8]>) -> IResult<&[u8], ()> {
Error(0)