~ajk/bsr

7b9aacc50eeee4e9c3abe1dee604586a7f54249d — Andrew Kay 3 years ago c0a08eb
Refactor run parameters
2 files changed, 10 insertions(+), 5 deletions(-)

M src/lib.rs
M src/main.rs
M src/lib.rs => src/lib.rs +6 -4
@@ 15,20 15,22 @@ fn convert_bitstream<R: Read, W: Write>(writer: &mut W, bitstream: &mut Bitstrea
    bitstream.try_for_each(|w1| w1.and_then(|w2| writer.write_all(&w2.to_le_bytes())))
}

pub fn run<R: BufRead>(input: &mut R, show_info: bool, only_show_info: bool) -> io::Result<()> {
pub fn run<R: BufRead>(input: &mut R, should_show_info: bool, should_convert: bool) -> io::Result<()> {
    let header = read_header(input)?;

    if show_info || only_show_info {
    if should_show_info {
        let message = format_header(&header);

        if only_show_info {
        // If not converting we write the info to stdout, otherwise stderr to keep it
        // separate from the converted bitstream.
        if !should_convert {
            println!("{}", message);
        } else {
            eprintln!("{}", message);
        }
    }

    if !only_show_info && header.bitstream_length > 0 {
    if should_convert && header.bitstream_length > 0 {
        let mut bitstream = Bitstream::new(input, &header);

        convert_bitstream(&mut io::stdout(), &mut bitstream)?;

M src/main.rs => src/main.rs +4 -1
@@ 30,7 30,10 @@ fn main() {
        _ => Box::new(File::open(path).expect("error opening bitstream"))
    };

    if let Err(e) = bsr::run(&mut BufReader::new(input), show_info, only_show_info) {
    let should_show_info = show_info || only_show_info;
    let should_convert = !only_show_info;

    if let Err(e) = bsr::run(&mut BufReader::new(input), should_show_info, should_convert) {
        eprintln!("{}", e);
        process::exit(1);
    }