decode shows warnings on corrupted frames.

Closes #27.
This commit is contained in:
Alex Helfet 2017-12-31 16:16:46 +00:00
commit 3b87f4e94e
2 changed files with 34 additions and 4 deletions

View file

@ -3,6 +3,7 @@ use csv;
use framed;
use serde_json;
use std;
use std::io;
#[derive(Debug, Error)]
pub enum Error {
@ -16,6 +17,9 @@ pub enum Error {
#[error(non_std)]
Framed(framed::Error),
/// Error in library `std::io`.
Io(io::Error),
/// Error in library `serde_json`.
SerdeJson(serde_json::Error),
}

View file

@ -14,7 +14,7 @@ mod error;
use error::{Error, Result};
use clap::Arg;
use std::io::{stdin, stdout};
use std::io::{stdin, stdout, stderr, Write};
arg_enum! {
#[derive(Debug, Eq, PartialEq)]
@ -58,18 +58,29 @@ fn try() -> Result<()> {
};
loop {
// Note: Output is flushed after each line so output on stdout
// and messages on stderr are in sync.
let res = r.recv();
match res {
Ok(v) => match out_fmt {
OutputFormat::Csv => csvw.as_mut()
.expect("Should've been initialized")
.serialize(&v)?,
OutputFormat::Csv => {
let csvw = csvw.as_mut()
.expect("Should've been initialized");
csvw.serialize(&v)?;
csvw.flush()?;
}
OutputFormat::Debug => println!("{:#?}", v),
OutputFormat::Json => {
serde_json::to_writer(stdout(), &v)?;
println!("");
stdout().flush()?;
},
},
Err(ref e) if e.is_corrupt_frame() => {
eprintln!("WARN: Corrupt frame, error: {:?}", e);
stderr().flush()?;
}
Err(framed::Error::EofBeforeFrame) => return Ok(()),
Err(e) => return Err(Error::from(e)),
};