Skip to content

Commit

Permalink
a consumer can fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jun 15, 2015
1 parent 9e17cd4 commit 35a7bfa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
//! }
//! }
//!
//! fn failed(&mut self, error_code: u32) {
//! println!("failed with error code {}", error_code);
//! }
//!
//! fn end(&mut self) {
//! println!("finished");
//! }
Expand Down Expand Up @@ -95,6 +99,7 @@ pub enum ConsumerState {
/// The run function takes care of continuing or not
pub trait Consumer {
fn consume(&mut self, input: &[u8]) -> ConsumerState;
fn failed(&mut self, error_code: u32);
fn end(&mut self);

fn run(&mut self, producer: &mut Producer) {
Expand All @@ -106,6 +111,7 @@ pub trait Consumer {
let mut seek_from:SeekFrom = SeekFrom::Current(0);
let mut eof = false;
let mut end = false;
let mut err: Option<u32> = None;

loop {
//self.getDataFromProducer(producer, seek_from, needed, acc);
Expand Down Expand Up @@ -163,9 +169,9 @@ pub trait Consumer {
//println!("full:\n{}", acc.to_hex(8));
//println!("truncated:\n{}", (&acc[0..needed]).to_hex(16));
match self.consume(&acc[0..needed]) {
ConsumerError(_) => {
//ConsumerError(e) => {
ConsumerError(e) => {
//println!("consumer error, stopping: {}", e);
err = Some(e);
},
ConsumerDone => {
//println!("data, done");
Expand All @@ -190,6 +196,10 @@ pub trait Consumer {
//println!("incomplete");
}
}
if let Some(e) = err {
self.failed(e);
break;
}
if eof {
self.end();
break;
Expand Down Expand Up @@ -257,6 +267,10 @@ macro_rules! take(
assert_eq!(self.counter, 5);
self.ended = true;
}

fn failed(&mut self, error_code: u32) {
println!("failed with error code: {}", error_code);
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
//! }
//! }
//!
//! fn failed(&mut self, error_code: u32) {
//! println!("failed with error code: {}", error_code);
//! }
//!
//! fn end(&mut self) {
//! println!("we counted {} noms", self.counter);
//! }
Expand Down
4 changes: 4 additions & 0 deletions tests/mp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ impl Consumer for MP4Consumer {
}
}

fn failed(&mut self, error_code: u32) {
println!("failed with error code: {}", error_code);
}

fn end(&mut self) {
println!("finish!");
}
Expand Down
4 changes: 4 additions & 0 deletions tests/omnom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl Consumer for TestConsumer {
}
}

fn failed(&mut self, error_code: u32) {
println!("failed with error code: {}", error_code);
}

fn end(&mut self) {
println!("counted {} noms", self.counter);
}
Expand Down

0 comments on commit 35a7bfa

Please sign in to comment.