From ab11740104a6c4f545bc1b35a1bc002044e0fd59 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 7 Oct 2019 04:04:39 -0700 Subject: [PATCH] Add explicit `Subcommand` enum (#484) --- src/common.rs | 6 +++--- src/config.rs | 45 +++++++++++++++++++++++++++++++++++---------- src/lib.rs | 1 + src/run.rs | 12 ++++++------ src/subcommand.rs | 9 +++++++++ 5 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 src/subcommand.rs diff --git a/src/common.rs b/src/common.rs index cf692d2..a559a87 100644 --- a/src/common.rs +++ b/src/common.rs @@ -44,9 +44,9 @@ pub(crate) use crate::{ lexer::Lexer, output_error::OutputError, parameter::Parameter, parser::Parser, platform::Platform, position::Position, recipe::Recipe, recipe_context::RecipeContext, recipe_resolver::RecipeResolver, runtime_error::RuntimeError, search_error::SearchError, - shebang::Shebang, state::State, string_literal::StringLiteral, token::Token, - token_kind::TokenKind, use_color::UseColor, variables::Variables, verbosity::Verbosity, - warning::Warning, + shebang::Shebang, state::State, string_literal::StringLiteral, subcommand::Subcommand, + token::Token, token_kind::TokenKind, use_color::UseColor, variables::Variables, + verbosity::Verbosity, warning::Warning, }; pub(crate) type CompilationResult<'a, T> = Result>; diff --git a/src/config.rs b/src/config.rs index b2e3b15..118f803 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,6 +5,7 @@ use clap::{App, AppSettings, Arg, ArgGroup, ArgMatches}; pub(crate) const DEFAULT_SHELL: &str = "sh"; pub(crate) struct Config<'a> { + pub(crate) subcommand: Subcommand<'a>, pub(crate) dry_run: bool, pub(crate) evaluate: bool, pub(crate) highlight: bool, @@ -16,6 +17,14 @@ pub(crate) struct Config<'a> { pub(crate) arguments: Vec<&'a str>, } +mod arg { + pub(crate) const EDIT: &str = "EDIT"; + pub(crate) const SUMMARY: &str = "SUMMARY"; + pub(crate) const DUMP: &str = "DUMP"; + pub(crate) const LIST: &str = "LIST"; + pub(crate) const SHOW: &str = "SHOW"; +} + impl<'a> Config<'a> { pub(crate) fn app() -> App<'static, 'static> { let app = App::new(env!("CARGO_PKG_NAME")) @@ -43,12 +52,12 @@ impl<'a> Config<'a> { .conflicts_with("QUIET"), ) .arg( - Arg::with_name("DUMP") + Arg::with_name(arg::DUMP) .long("dump") .help("Print entire justfile"), ) .arg( - Arg::with_name("EDIT") + Arg::with_name(arg::EDIT) .short("e") .long("edit") .help("Open justfile with $EDITOR"), @@ -71,7 +80,7 @@ impl<'a> Config<'a> { .help("Use as justfile."), ) .arg( - Arg::with_name("LIST") + Arg::with_name(arg::LIST) .short("l") .long("list") .help("List available recipes and their arguments"), @@ -100,7 +109,7 @@ impl<'a> Config<'a> { .help("Invoke to run recipes"), ) .arg( - Arg::with_name("SHOW") + Arg::with_name(arg::SHOW) .short("s") .long("show") .takes_value(true) @@ -108,7 +117,7 @@ impl<'a> Config<'a> { .help("Show information about "), ) .arg( - Arg::with_name("SUMMARY") + Arg::with_name(arg::SUMMARY) .long("summary") .help("List names of available recipes"), ) @@ -128,11 +137,11 @@ impl<'a> Config<'a> { .requires("JUSTFILE"), ) .group(ArgGroup::with_name("EARLY-EXIT").args(&[ - "DUMP", - "EDIT", - "LIST", - "SHOW", - "SUMMARY", + arg::DUMP, + arg::EDIT, + arg::LIST, + arg::SHOW, + arg::SUMMARY, "ARGUMENTS", "EVALUATE", ])); @@ -229,12 +238,27 @@ impl<'a> Config<'a> { }) .collect::>(); + let subcommand = if matches.is_present(arg::EDIT) { + Subcommand::Edit + } else if matches.is_present(arg::SUMMARY) { + Subcommand::Summary + } else if matches.is_present(arg::DUMP) { + Subcommand::Dump + } else if matches.is_present(arg::LIST) { + Subcommand::List + } else if let Some(name) = matches.value_of(arg::SHOW) { + Subcommand::Show { name } + } else { + Subcommand::Run + }; + Config { dry_run: matches.is_present("DRY-RUN"), evaluate: matches.is_present("EVALUATE"), highlight: matches.is_present("HIGHLIGHT"), quiet: matches.is_present("QUIET"), shell: matches.value_of("SHELL").unwrap(), + subcommand, verbosity, color, overrides, @@ -246,6 +270,7 @@ impl<'a> Config<'a> { impl<'a> Default for Config<'a> { fn default() -> Config<'static> { Config { + subcommand: Subcommand::Run, dry_run: false, evaluate: false, highlight: false, diff --git a/src/lib.rs b/src/lib.rs index d6b1b72..77b2d70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,7 @@ mod search_error; mod shebang; mod state; mod string_literal; +mod subcommand; mod token; mod token_kind; mod use_color; diff --git a/src/run.rs b/src/run.rs index 22a7a4a..df363f8 100644 --- a/src/run.rs +++ b/src/run.rs @@ -64,7 +64,7 @@ pub fn run() { let text; if let (Some(justfile), Some(directory)) = (justfile, working_directory) { - if matches.is_present("EDIT") { + if config.subcommand == Subcommand::Edit { edit(justfile); } @@ -85,7 +85,7 @@ pub fn run() { }; match search::justfile(¤t_dir) { Ok(name) => { - if matches.is_present("EDIT") { + if config.subcommand == Subcommand::Edit { edit(name); } text = fs::read_to_string(&name) @@ -121,7 +121,7 @@ pub fn run() { } } - if matches.is_present("SUMMARY") { + if config.subcommand == Subcommand::Summary { if justfile.count() == 0 { eprintln!("Justfile contains no recipes."); } else { @@ -138,12 +138,12 @@ pub fn run() { process::exit(EXIT_SUCCESS); } - if matches.is_present("DUMP") { + if config.subcommand == Subcommand::Dump { println!("{}", justfile); process::exit(EXIT_SUCCESS); } - if matches.is_present("LIST") { + if config.subcommand == Subcommand::List { // Construct a target to alias map. let mut recipe_aliases: BTreeMap<&str, Vec<&str>> = BTreeMap::new(); for alias in justfile.aliases.values() { @@ -230,7 +230,7 @@ pub fn run() { process::exit(EXIT_SUCCESS); } - if let Some(name) = matches.value_of("SHOW") { + if let Subcommand::Show { name } = config.subcommand { if let Some(alias) = justfile.get_alias(name) { let recipe = justfile.get_recipe(alias.target).unwrap(); println!("{}", alias); diff --git a/src/subcommand.rs b/src/subcommand.rs new file mode 100644 index 0000000..33f9655 --- /dev/null +++ b/src/subcommand.rs @@ -0,0 +1,9 @@ +#[derive(PartialEq)] +pub(crate) enum Subcommand<'a> { + Edit, + Summary, + Dump, + List, + Show { name: &'a str }, + Run, +}