//! A library for creating new Rust packages with ROS package.xml support.

use clap::ArgMatches;
use ros_new::{args_to_cargo_toml, args_to_package_xml, env_to_matches};
use std::{collections::HashMap, env, io::ErrorKind};

fn main() -> Result<(), Box<ErrorKind>> {
    // When invoked as `cargo ros-new`, the first argument is "ros-new"
    // So we need to skip it if present
    let matches: ArgMatches = env_to_matches(env::args().collect());
    let arguments_map: HashMap<String, String> = [
        "package_name",
        "format",
        "description",
        "mail",
        "maintainer",
        "license",
    ]
    .iter()
    .map(|argument| {
        matches
            .get_one::<String>(argument)
            .ok_or(ErrorKind::InvalidInput)
            .map(|value| ((*argument).to_string(), value.clone()))
    })
    .collect::<Result<HashMap<String, String>, ErrorKind>>()?;
    args_to_cargo_toml(&matches, &arguments_map)?;
    args_to_package_xml(&arguments_map)?;
    Ok(())
}
