autorip 0.1.1

Composes other programs to automatically rip optical media
Documentation
use anyhow::Result;
use autorip::{rip, search, Config, RipConfig, SearchConfig};
use log::{Level, LevelFilter, Metadata, Record};
use structopt::StructOpt;

use std::io::{self, Write};

struct Logger;

impl log::Log for Logger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= Level::Debug
    }

    fn log(&self, record: &Record) {
        if self.enabled(record.metadata()) {
            eprintln!("Rust says: {} - {}", record.level(), record.args());
        }
    }

    fn flush(&self) {}
}

static LOGGER: Logger = Logger;

fn main() -> Result<()> {
    let config = Config::from_args();

    match config {
        Config::Rip(RipConfig { verbosity, .. })
        | Config::Search(SearchConfig { verbosity, .. }) => {
            let v = match verbosity {
                0 => LevelFilter::Error,
                1 => LevelFilter::Warn,
                2 => LevelFilter::Info,
                3 => LevelFilter::Debug,
                4..=u8::MAX => LevelFilter::Trace,
            };

            log::set_logger(&LOGGER).map(|()| log::set_max_level(v))?;
        }
    }

    log::debug!("Using config: {:?}", &config);
    match config {
        Config::Rip(rc) => rip(&rc).and_then(|pb| {
            Ok(writeln!(io::stdout(), "Ripped to {}", pb.display())?)
        }),
        Config::Search(sc) => search(&sc),
    }
}