use clap::Command;

mod cmd;
pub mod crockford;
pub mod doi_utils;
pub mod utils;

fn main() {
    if let Err(e) = run() {
        eprintln!("{}", e);
        std::process::exit(1);
    }
}

fn run() -> Result<(), String> {
    let matches = Command::new("commonmeta")
        .version(env!("CARGO_PKG_VERSION"))
        .author("Front Matter <info@front-matter.de>")
        .about("Commonmeta")
        .subcommand(cmd::convert::command())
        .subcommand(cmd::decode::command())
        .subcommand(cmd::dump::command())
        .subcommand(cmd::encode::command())
        .subcommand(cmd::import::command())
        .subcommand(cmd::list::command())
        .subcommand(cmd::r#match::command())
        .subcommand(cmd::migrate::command())
        .subcommand(cmd::push::command())
        .subcommand(cmd::put::command())
        .subcommand(cmd::settings::command())
        .subcommand(cmd::validate::command())
        .get_matches();

    match matches.subcommand() {
        Some(("convert", sub_matches)) => cmd::convert::execute(sub_matches),
        Some(("decode", sub_matches)) => cmd::decode::execute(sub_matches),
        Some(("package", sub_matches)) => cmd::dump::execute(sub_matches),
        Some(("encode", sub_matches)) => cmd::encode::execute(sub_matches),
        Some(("import", sub_matches)) => cmd::import::execute(sub_matches),
        Some(("list", sub_matches)) => cmd::list::execute(sub_matches),
        Some(("match", sub_matches)) => cmd::r#match::execute(sub_matches),
        Some(("migrate", sub_matches)) => cmd::migrate::execute(sub_matches),
        Some(("push", sub_matches)) => cmd::push::execute(sub_matches),
        Some(("put", sub_matches)) => cmd::put::execute(sub_matches),
        Some(("settings", sub_matches)) => cmd::settings::execute(sub_matches),
        Some(("validate", sub_matches)) => cmd::validate::execute(sub_matches),
        _ => Ok(()),
    }
}
