diff --git a/Cargo.lock b/Cargo.lock index 467cca0b..983c77b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,7 +68,6 @@ dependencies = [ "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)", "console 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", - "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "git2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -195,11 +194,6 @@ dependencies = [ "shared_child 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "either" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "error-chain" version = "0.11.0" @@ -826,7 +820,6 @@ dependencies = [ "checksum directories 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc2561db021b6f1321d0f16b67ed28ce843ef4610dfaa432e3ffa2e8a3050ebf" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum duct 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "166298c17c5b4fe5997b962c2f22e887c7c5adc44308eb9103ce5b66af45a423" -"checksum either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0" "checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3" "checksum flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fac2277e84e5e858483756647a9d0aa8d9a2b7cba517fd84325a0aaa69a0909" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" diff --git a/Cargo.toml b/Cargo.toml index 8b85bf1e..ccb1f44d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ version = "0.3.0" atty = "0.2.2" ansi_term = "0.10" console = "0.6" -either = "1.4" error-chain = "0.11" directories = "0.10" lazy_static = "1.0" diff --git a/src/main.rs b/src/main.rs index 8c1949c5..7e4124cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,6 @@ extern crate ansi_term; extern crate atty; extern crate console; extern crate directories; -extern crate either; extern crate git2; extern crate syntect; @@ -37,7 +36,6 @@ use ansi_term::Style; use atty::Stream; use clap::{App, AppSettings, Arg, ArgGroup, SubCommand}; use directories::ProjectDirs; -use either::Either; use git2::{DiffOptions, IntoCString, Repository}; use syntect::dumps::{dump_to_file, from_binary, from_reader}; @@ -54,6 +52,7 @@ lazy_static! { mod errors { error_chain! { foreign_links { + Clap(::clap::Error); Io(::std::io::Error); } @@ -72,12 +71,32 @@ mod errors { use errors::*; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Copy, Clone)] pub enum OutputComponent { Changes, Grid, Header, Numbers, + Full, + Plain, +} + +impl OutputComponent { + fn components(&self) -> &'static [OutputComponent] { + match *self { + OutputComponent::Changes => &[OutputComponent::Changes], + OutputComponent::Grid => &[OutputComponent::Grid], + OutputComponent::Header => &[OutputComponent::Header], + OutputComponent::Numbers => &[OutputComponent::Numbers], + OutputComponent::Full => &[ + OutputComponent::Changes, + OutputComponent::Grid, + OutputComponent::Header, + OutputComponent::Numbers, + ], + OutputComponent::Plain => &[], + } + } } impl FromStr for OutputComponent { @@ -89,38 +108,8 @@ impl FromStr for OutputComponent { "grid" => Ok(OutputComponent::Grid), "header" => Ok(OutputComponent::Header), "numbers" => Ok(OutputComponent::Numbers), - _ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()), - } - } -} - -#[derive(Debug, Eq, PartialEq)] -enum PredefinedStyle { - Full, - Plain, -} - -impl PredefinedStyle { - fn components(&self) -> &'static [OutputComponent] { - match *self { - PredefinedStyle::Full => &[ - OutputComponent::Changes, - OutputComponent::Grid, - OutputComponent::Header, - OutputComponent::Numbers, - ], - PredefinedStyle::Plain => &[], - } - } -} - -impl FromStr for PredefinedStyle { - type Err = Error; - - fn from_str(s: &str) -> Result { - match s { - "full" => Ok(PredefinedStyle::Full), - "plain" => Ok(PredefinedStyle::Plain), + "full" => Ok(OutputComponent::Full), + "plain" => Ok(OutputComponent::Plain), _ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()), } } @@ -609,63 +598,17 @@ fn run() -> Result<()> { }) .unwrap_or_else(|| vec![None]); // read from stdin (None) if no args are given - // Split the user-supplied parameter on commas (','), then go over each part and map it - // to either a predefined style or a simple output style. Once this mapping is done, - // split it up into two lists: one for the matched predefined styles and one for the - // simple output styles. - let (_predefined_styles, _output_components): (Vec<_>, Vec<_>) = app_matches - .values_of("style") - .unwrap() - .map(|s| { - let predefined_style = PredefinedStyle::from_str(s); - if let Ok(style) = predefined_style { - Some(Either::Left(style)) - } else { - match OutputComponent::from_str(s) { - Ok(style) => Some(Either::Right(style)), - Err(error) => { - eprintln!("{}: {}", Red.paint("[bat error]"), error); - None - } - } - } - }) - .filter_map(|s| s) - .partition(|style| match *style { - Either::Left(_) => true, - Either::Right(_) => false, - }); - - // Once we have the two lists of predefined styles and simple output styles, we take the - // first matched predefined style and use that as our style, discarding every other - // predefined or simple output style. If we have no predefined styles though, we take - // all matched simple output styles and pass those to the `Options` struct. - let output_components: Vec; - let output_components = if let Some(predefined_style) = _predefined_styles + let output_components = values_t!(app_matches.values_of("style"), OutputComponent)? .into_iter() - .map(|e| e.left().unwrap()) - .next() - { - predefined_style.components() - } else { - output_components = _output_components - .into_iter() - .map(|e| e.right().unwrap()) - .collect::>(); - - // If we are in this else-block, `output_components` is empty and the requested style - // wasn't the empty string, the user supplied only wrong style arguments. In this - // case we inform them about this. - if output_components.is_empty() && app_matches.value_of("style").unwrap() != "" { - return Err(ErrorKind::NoCorrectStylesSpecified.into()); - } - - &output_components - }; + .map(|style| style.components()) + .fold(vec![], |mut acc, x| { + acc.extend_from_slice(x); + acc + }); let options = Options { true_color: is_truecolor_terminal(), - output_components, + output_components: &output_components, language: app_matches.value_of("language"), colored_output: match app_matches.value_of("color") { Some("always") => true,