From 0adfa6aae623b3988a688b8b6eef14ce96e213c0 Mon Sep 17 00:00:00 2001 From: Grisha Kruglov Date: Mon, 4 Jun 2018 20:59:27 -0400 Subject: [PATCH] Convert tools/cli to failure. --- tools/cli/Cargo.toml | 3 +- tools/cli/src/mentat_cli/command_parser.rs | 36 ++++++++++++++-------- tools/cli/src/mentat_cli/input.rs | 4 +-- tools/cli/src/mentat_cli/lib.rs | 10 ++++-- tools/cli/src/mentat_cli/repl.rs | 9 ++++-- 5 files changed, 43 insertions(+), 19 deletions(-) diff --git a/tools/cli/Cargo.toml b/tools/cli/Cargo.toml index 081a2d4f..6a6eb088 100644 --- a/tools/cli/Cargo.toml +++ b/tools/cli/Cargo.toml @@ -20,6 +20,8 @@ test = false [dependencies] combine = "2.2.2" env_logger = "0.5" +failure = "0.1.1" +failure_derive = "0.1.1" getopts = "0.2" lazy_static = "0.2" linefeed = "0.4" @@ -28,7 +30,6 @@ tabwriter = "1" tempfile = "1.1" termion = "1" time = "0.1" -error-chain = { git = "https://github.com/rnewman/error-chain", branch = "rnewman/sync" } [dependencies.rusqlite] version = "0.13" diff --git a/tools/cli/src/mentat_cli/command_parser.rs b/tools/cli/src/mentat_cli/command_parser.rs index ec49618d..69de29e3 100644 --- a/tools/cli/src/mentat_cli/command_parser.rs +++ b/tools/cli/src/mentat_cli/command_parser.rs @@ -30,14 +30,26 @@ use combine::combinator::{ try, }; -use errors as cli; +use CliError; use edn; +use failure::{ + Compat, + Error, +}; + use mentat::{ CacheDirection, }; +#[macro_export] +macro_rules! bail { + ($e:expr) => ( + return Err($e.into()); + ) +} + pub static COMMAND_CACHE: &'static str = &"cache"; pub static COMMAND_CLOSE: &'static str = &"close"; pub static COMMAND_EXIT_LONG: &'static str = &"exit"; @@ -188,7 +200,7 @@ impl Command { } } -pub fn command(s: &str) -> Result { +pub fn command(s: &str) -> Result { let path = || many1::(satisfy(|c: char| !c.is_whitespace())); let argument = || many1::(satisfy(|c: char| !c.is_whitespace())); let arguments = || sep_end_by::, _, _>(many1(satisfy(|c: char| !c.is_whitespace())), many1::, _>(space())).expected("arguments"); @@ -202,7 +214,7 @@ pub fn command(s: &str) -> Result { let edn_arg_parser = || spaces() .with(look_ahead(string("[").or(string("{"))) .with(many1::, _>(try(any()))) - .and_then(|args| -> Result { + .and_then(|args| -> Result> { Ok(args.iter().collect()) }) ); @@ -217,10 +229,10 @@ pub fn command(s: &str) -> Result { .with(arguments()) .map(move |args| { if args.len() < num_args { - bail!(cli::ErrorKind::CommandParse("Missing required argument".to_string())); + bail!(CliError::CommandParse("Missing required argument".to_string())); } if args.len() > num_args { - bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[num_args]))); + bail!(CliError::CommandParse(format!("Unrecognized argument {:?}", args[num_args]))); } Ok(args) }) @@ -239,7 +251,7 @@ pub fn command(s: &str) -> Result { .with(no_arg_parser()) .map(|args| { if !args.is_empty() { - bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[0])) ); + bail!(CliError::CommandParse(format!("Unrecognized argument {:?}", args[0])) ); } Ok(Command::Close) }); @@ -248,7 +260,7 @@ pub fn command(s: &str) -> Result { .with(no_arg_parser()) .map(|args| { if !args.is_empty() { - bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[0])) ); + bail!(CliError::CommandParse(format!("Unrecognized argument {:?}", args[0])) ); } Ok(Command::Exit) }); @@ -302,7 +314,7 @@ pub fn command(s: &str) -> Result { .with(no_arg_parser()) .map(|args| { if !args.is_empty() { - bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[0])) ); + bail!(CliError::CommandParse(format!("Unrecognized argument {:?}", args[0])) ); } Ok(Command::Schema) }); @@ -312,10 +324,10 @@ pub fn command(s: &str) -> Result { .with(arguments()) .map(|args| { if args.len() < 1 { - bail!(cli::ErrorKind::CommandParse("Missing required argument".to_string())); + bail!(CliError::CommandParse("Missing required argument".to_string())); } if args.len() > 2 { - bail!(cli::ErrorKind::CommandParse(format!("Unrecognized argument {:?}", args[2]))); + bail!(CliError::CommandParse(format!("Unrecognized argument {:?}", args[2]))); } Ok(Command::Sync(args.clone())) }); @@ -335,7 +347,7 @@ pub fn command(s: &str) -> Result { spaces() .skip(token('.')) - .with(choice::<[&mut Parser>; 16], _> + .with(choice::<[&mut Parser>; 16], _> ([&mut try(help_parser), &mut try(import_parser), &mut try(timer_parser), @@ -353,7 +365,7 @@ pub fn command(s: &str) -> Result { &mut try(sync_parser), &mut try(transact_parser)])) .parse(s) - .unwrap_or((Err(cli::ErrorKind::CommandParse(format!("Invalid command {:?}", s)).into()), "")).0 + .unwrap_or((Err(CliError::CommandParse(format!("Invalid command {:?}", s)).into()), "")).0 } #[cfg(test)] diff --git a/tools/cli/src/mentat_cli/input.rs b/tools/cli/src/mentat_cli/input.rs index 007511ce..9c9f3945 100644 --- a/tools/cli/src/mentat_cli/input.rs +++ b/tools/cli/src/mentat_cli/input.rs @@ -28,7 +28,7 @@ use command_parser::{ command, }; -use errors as cli; +use failure::Error; /// Starting prompt const DEFAULT_PROMPT: &'static str = "mentat=> "; @@ -97,7 +97,7 @@ impl InputReader { /// Reads a single command, item, or statement from `stdin`. /// Returns `More` if further input is required for a complete result. /// In this case, the input received so far is buffered internally. - pub fn read_input(&mut self) -> Result { + pub fn read_input(&mut self) -> Result { let prompt = if self.in_process_cmd.is_some() { MORE_PROMPT } else { DEFAULT_PROMPT }; let prompt = format!("{blue}{prompt}{reset}", blue = color::Fg(::BLUE), diff --git a/tools/cli/src/mentat_cli/lib.rs b/tools/cli/src/mentat_cli/lib.rs index 692257c3..bb637e19 100644 --- a/tools/cli/src/mentat_cli/lib.rs +++ b/tools/cli/src/mentat_cli/lib.rs @@ -10,12 +10,13 @@ #![crate_name = "mentat_cli"] +#[macro_use] extern crate failure_derive; #[macro_use] extern crate log; #[macro_use] extern crate lazy_static; -#[macro_use] extern crate error_chain; extern crate combine; extern crate env_logger; +extern crate failure; extern crate getopts; extern crate linefeed; extern crate rusqlite; @@ -41,7 +42,12 @@ static GREEN: color::Rgb = color::Rgb(0x77, 0xFF, 0x99); pub mod command_parser; pub mod input; pub mod repl; -pub mod errors; + +#[derive(Debug, Fail)] +pub enum CliError { + #[fail(display = "{}", _0)] + CommandParse(String), +} pub fn run() -> i32 { env_logger::init(); diff --git a/tools/cli/src/mentat_cli/repl.rs b/tools/cli/src/mentat_cli/repl.rs index ba74dd2c..b4f66197 100644 --- a/tools/cli/src/mentat_cli/repl.rs +++ b/tools/cli/src/mentat_cli/repl.rs @@ -11,6 +11,11 @@ use std::io::Write; use std::process; +use failure::{ + err_msg, + Error, +}; + use tabwriter::TabWriter; use termion::{ @@ -383,7 +388,7 @@ impl Repl { if self.path.is_empty() || path != self.path { let next = match encryption_key { #[cfg(not(feature = "sqlcipher"))] - Some(_) => bail!(".open_encrypted and .empty_encrypted require the sqlcipher Mentat feature"), + Some(_) => return Err(err_msg(".open_encrypted and .empty_encrypted require the sqlcipher Mentat feature")), #[cfg(feature = "sqlcipher")] Some(k) => { if empty { @@ -467,7 +472,7 @@ impl Repl { output.flush().unwrap(); } - fn print_results(&self, query_output: QueryOutput) -> Result<(), ::errors::Error> { + fn print_results(&self, query_output: QueryOutput) -> Result<(), Error> { let stdout = ::std::io::stdout(); let mut output = TabWriter::new(stdout.lock());