Use STRUM for agument parsing

This commit is contained in:
Roey Darwish Dror
2019-09-28 14:41:06 +03:00
parent ef7ba46786
commit f6b4bb00b2
3 changed files with 29 additions and 39 deletions

View File

@@ -2,12 +2,13 @@ use super::error::{Error, ErrorKind};
use super::utils::editor;
use directories::BaseDirs;
use failure::ResultExt;
use lazy_static::lazy_static;
use strum::{EnumString, EnumVariantNames};
use log::{debug, error, LevelFilter};
use pretty_env_logger::formatted_timed_builder;
use serde::Deserialize;
use shellexpand;
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::fs::write;
use std::path::PathBuf;
use std::process::Command;
@@ -17,30 +18,9 @@ use toml;
type Commands = BTreeMap<String, String>;
lazy_static! {
// While this is used to automatically generate possible value list everywhere in the code, the
// README.md file still needs to be manually updated.
static ref STEPS_MAPPING: HashMap<&'static str, Step> = {
let mut m = HashMap::new();
m.insert("system", Step::System);
m.insert("git-repos", Step::GitRepos);
m.insert("vim", Step::Vim);
m.insert("emacs", Step::Emacs);
m.insert("gem", Step::Gem);
m.insert("node", Step::Node);
m.insert("sdkman", Step::Sdkman);
m.insert("remotes", Step::Remotes);
m.insert("rustup", Step::Rustup);
m.insert("cargo", Step::Cargo);
m.insert("shell", Step::Shell);
m
};
}
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[derive(EnumString, EnumVariantNames, Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "snake_case")]
pub enum Step {
/// Don't perform system upgrade
System,
@@ -66,19 +46,6 @@ pub enum Step {
Shell,
}
impl Step {
fn possible_values() -> Vec<&'static str> {
STEPS_MAPPING.keys().cloned().collect()
}
}
impl std::str::FromStr for Step {
type Err = structopt::clap::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(STEPS_MAPPING.get(s).unwrap().clone())
}
}
#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
/// Configuration file
@@ -171,7 +138,7 @@ pub struct CommandLineArgs {
no_retry: bool,
/// Do not perform upgrades for the given steps
#[structopt(long = "disable", possible_values = &Step::possible_values())]
#[structopt(long = "disable", possible_values = &Step::variants())]
disable: Vec<Step>,
/// Output logs