From 017f0e4e53dfb1fb1b8a1844cf99b253dd9dbd25 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:02:10 +0100 Subject: Refactor arg parsing --- crates/ra_cli/src/main.rs | 284 +++++++++++++++++++++++++++------------------- 1 file changed, 169 insertions(+), 115 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 4a428faff..7479fbd80 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -14,6 +14,15 @@ use ra_syntax::{AstNode, SourceFile}; type Result = std::result::Result>; +fn main() -> Result<()> { + env_logger::try_init()?; + + let command = Command::from_args()?; + command.run()?; + + Ok(()) +} + #[derive(Clone, Copy)] pub enum Verbosity { Spammy, @@ -37,17 +46,40 @@ impl Verbosity { } } -fn main() -> Result<()> { - env_logger::try_init()?; +enum Command { + Parse { + no_dump: bool, + }, + Symbols, + Highlight { + rainbow: bool, + }, + Stats { + verbosity: Verbosity, + randomize: bool, + memory_usage: bool, + only: Option, + with_deps: bool, + path: String, + }, + Bench { + verbose: bool, + path: String, + op: analysis_bench::Op, + }, + HelpPrinted, +} - let mut matches = Arguments::from_env(); - let subcommand = matches.subcommand()?.unwrap_or_default(); +impl Command { + fn from_args() -> Result { + let mut matches = Arguments::from_env(); + let subcommand = matches.subcommand()?.unwrap_or_default(); - match subcommand.as_str() { - "parse" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + let command = match subcommand.as_str() { + "parse" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-parse USAGE: @@ -56,24 +88,18 @@ USAGE: FLAGS: -h, --help Prints help inforamtion --no-dump" - ); - return Ok(()); - } - - let no_dump = matches.contains("--no-dump"); - matches.finish().or_else(handle_extra_flags)?; + ); + return Ok(Command::HelpPrinted); + } - let _p = profile("parsing"); - let file = file()?; - if !no_dump { - println!("{:#?}", file.syntax()); + let no_dump = matches.contains("--no-dump"); + matches.finish().or_else(handle_extra_flags)?; + Command::Parse { no_dump } } - std::mem::forget(file); - } - "symbols" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + "symbols" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-symbols USAGE: @@ -81,21 +107,18 @@ USAGE: FLAGS: -h, --help Prints help inforamtion" - ); - return Ok(()); - } + ); + return Ok(Command::HelpPrinted); + } - matches.finish().or_else(handle_extra_flags)?; + matches.finish().or_else(handle_extra_flags)?; - let file = file()?; - for s in file_structure(&file) { - println!("{:?}", s); + Command::Symbols } - } - "highlight" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + "highlight" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-highlight USAGE: @@ -104,21 +127,18 @@ USAGE: FLAGS: -h, --help Prints help information -r, --rainbow" - ); - return Ok(()); - } - - let rainbow_opt = matches.contains(["-r", "--rainbow"]); - matches.finish().or_else(handle_extra_flags)?; + ); + return Ok(Command::HelpPrinted); + } - let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); - let html = analysis.highlight_as_html(file_id, rainbow_opt).unwrap(); - println!("{}", html); - } - "analysis-stats" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + let rainbow = matches.contains(["-r", "--rainbow"]); + matches.finish().or_else(handle_extra_flags)?; + Command::Highlight { rainbow } + } + "analysis-stats" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra-cli-analysis-stats USAGE: @@ -135,47 +155,40 @@ OPTIONS: ARGS: " - ); - return Ok(()); - } - - let verbosity = match ( - matches.contains(["-vv", "--spammy"]), - matches.contains(["-v", "--verbose"]), - matches.contains(["-q", "--quiet"]), - ) { - (true, _, true) => Err("Invalid flags: -q conflicts with -vv")?, - (true, _, false) => Verbosity::Spammy, - (false, false, false) => Verbosity::Normal, - (false, false, true) => Verbosity::Quiet, - (false, true, false) => Verbosity::Verbose, - (false, true, true) => Err("Invalid flags: -q conflicts with -v")?, - }; - let randomize = matches.contains("--randomize"); - let memory_usage = matches.contains("--memory-usage"); - let only: Option = matches.opt_value_from_str(["-o", "--only"])?; - let with_deps: bool = matches.contains("--with-deps"); - let path = { - let mut trailing = matches.free()?; - if trailing.len() != 1 { - Err("Invalid flags")?; + ); + return Ok(Command::HelpPrinted); } - trailing.pop().unwrap() - }; - - analysis_stats::run( - verbosity, - memory_usage, - path.as_ref(), - only.as_ref().map(String::as_ref), - with_deps, - randomize, - )?; - } - "analysis-bench" => { - if matches.contains(["-h", "--help"]) { - eprintln!( - "\ + + let verbosity = match ( + matches.contains(["-vv", "--spammy"]), + matches.contains(["-v", "--verbose"]), + matches.contains(["-q", "--quiet"]), + ) { + (true, _, true) => Err("Invalid flags: -q conflicts with -vv")?, + (true, _, false) => Verbosity::Spammy, + (false, false, false) => Verbosity::Normal, + (false, false, true) => Verbosity::Quiet, + (false, true, false) => Verbosity::Verbose, + (false, true, true) => Err("Invalid flags: -q conflicts with -v")?, + }; + let randomize = matches.contains("--randomize"); + let memory_usage = matches.contains("--memory-usage"); + let only: Option = matches.opt_value_from_str(["-o", "--only"])?; + let with_deps: bool = matches.contains("--with-deps"); + let path = { + let mut trailing = matches.free()?; + if trailing.len() != 1 { + Err("Invalid flags")?; + } + trailing.pop().unwrap() + }; + + Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } + } + "analysis-bench" => { + if matches.contains(["-h", "--help"]) { + eprintln!( + "\ ra_cli-analysis-bench USAGE: @@ -191,29 +204,28 @@ OPTIONS: ARGS: Project to analyse" - ); - return Ok(()); - } + ); + return Ok(Command::HelpPrinted); + } - let verbose = matches.contains(["-v", "--verbose"]); - let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default(); - let highlight_path: Option = matches.opt_value_from_str("--highlight")?; - let complete_path: Option = matches.opt_value_from_str("--complete")?; - let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; - let op = match (highlight_path, complete_path, goto_def_path) { - (Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() }, - (None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?), - (None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?), - _ => panic!( - "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" - ), - }; - matches.finish().or_else(handle_extra_flags)?; - - analysis_bench::run(verbose, path.as_ref(), op)?; - } - _ => eprintln!( - "\ + let verbose = matches.contains(["-v", "--verbose"]); + let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default(); + let highlight_path: Option = matches.opt_value_from_str("--highlight")?; + let complete_path: Option = matches.opt_value_from_str("--complete")?; + let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; + let op = match (highlight_path, complete_path, goto_def_path) { + (Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() }, + (None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?), + (None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?), + _ => panic!( + "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" + ), + }; + Command::Bench { verbose, path, op } + } + _ => { + eprintln!( + "\ ra-cli USAGE: @@ -228,9 +240,51 @@ SUBCOMMANDS: highlight parse symbols" - ), + ); + return Ok(Command::HelpPrinted); + } + }; + Ok(command) + } + + fn run(self) -> Result<()> { + match self { + Command::Parse { no_dump } => { + let _p = profile("parsing"); + let file = file()?; + if !no_dump { + println!("{:#?}", file.syntax()); + } + std::mem::forget(file); + } + Command::Symbols => { + let file = file()?; + for s in file_structure(&file) { + println!("{:?}", s); + } + } + Command::Highlight { rainbow } => { + let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); + let html = analysis.highlight_as_html(file_id, rainbow).unwrap(); + println!("{}", html); + } + Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => { + analysis_stats::run( + verbosity, + memory_usage, + path.as_ref(), + only.as_ref().map(String::as_ref), + with_deps, + randomize, + )?; + } + Command::Bench { verbose, path, op } => { + analysis_bench::run(verbose, path.as_ref(), op)?; + } + Command::HelpPrinted => (), + } + Ok(()) } - Ok(()) } fn handle_extra_flags(e: pico_args::Error) -> Result<()> { -- cgit v1.2.3 From a51b2603f993dd02275d275d37bbd0a0f24dbe33 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:02:59 +0100 Subject: Stronger Types --- crates/ra_cli/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 7479fbd80..42f0b3e17 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -5,7 +5,7 @@ mod analysis_stats; mod analysis_bench; mod progress_report; -use std::{error::Error, fmt::Write, io::Read}; +use std::{error::Error, fmt::Write, io::Read, path::PathBuf}; use pico_args::Arguments; use ra_ide::{file_structure, Analysis}; @@ -60,11 +60,11 @@ enum Command { memory_usage: bool, only: Option, with_deps: bool, - path: String, + path: PathBuf, }, Bench { verbose: bool, - path: String, + path: PathBuf, op: analysis_bench::Op, }, HelpPrinted, @@ -180,7 +180,7 @@ ARGS: if trailing.len() != 1 { Err("Invalid flags")?; } - trailing.pop().unwrap() + trailing.pop().unwrap().into() }; Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } @@ -209,7 +209,7 @@ ARGS: } let verbose = matches.contains(["-v", "--verbose"]); - let path: String = matches.opt_value_from_str("--path")?.unwrap_or_default(); + let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); let highlight_path: Option = matches.opt_value_from_str("--highlight")?; let complete_path: Option = matches.opt_value_from_str("--complete")?; let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; -- cgit v1.2.3 From fa482a9fee6596f9b3021b188a2120873d5bb0d3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:03:48 +0100 Subject: Move interesting stuff to main --- crates/ra_cli/src/main.rs | 75 ++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 42f0b3e17..f952d0f6c 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -18,7 +18,41 @@ fn main() -> Result<()> { env_logger::try_init()?; let command = Command::from_args()?; - command.run()?; + match command { + Command::Parse { no_dump } => { + let _p = profile("parsing"); + let file = file()?; + if !no_dump { + println!("{:#?}", file.syntax()); + } + std::mem::forget(file); + } + Command::Symbols => { + let file = file()?; + for s in file_structure(&file) { + println!("{:?}", s); + } + } + Command::Highlight { rainbow } => { + let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); + let html = analysis.highlight_as_html(file_id, rainbow).unwrap(); + println!("{}", html); + } + Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => { + analysis_stats::run( + verbosity, + memory_usage, + path.as_ref(), + only.as_ref().map(String::as_ref), + with_deps, + randomize, + )?; + } + Command::Bench { verbose, path, op } => { + analysis_bench::run(verbose, path.as_ref(), op)?; + } + Command::HelpPrinted => (), + } Ok(()) } @@ -246,45 +280,6 @@ SUBCOMMANDS: }; Ok(command) } - - fn run(self) -> Result<()> { - match self { - Command::Parse { no_dump } => { - let _p = profile("parsing"); - let file = file()?; - if !no_dump { - println!("{:#?}", file.syntax()); - } - std::mem::forget(file); - } - Command::Symbols => { - let file = file()?; - for s in file_structure(&file) { - println!("{:?}", s); - } - } - Command::Highlight { rainbow } => { - let (analysis, file_id) = Analysis::from_single_file(read_stdin()?); - let html = analysis.highlight_as_html(file_id, rainbow).unwrap(); - println!("{}", html); - } - Command::Stats { verbosity, randomize, memory_usage, only, with_deps, path } => { - analysis_stats::run( - verbosity, - memory_usage, - path.as_ref(), - only.as_ref().map(String::as_ref), - with_deps, - randomize, - )?; - } - Command::Bench { verbose, path, op } => { - analysis_bench::run(verbose, path.as_ref(), op)?; - } - Command::HelpPrinted => (), - } - Ok(()) - } } fn handle_extra_flags(e: pico_args::Error) -> Result<()> { -- cgit v1.2.3 From c818f5c65e67c93a55cbf7b1b6ded983d8f392b8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:05:45 +0100 Subject: Unify verbosity handling --- crates/ra_cli/src/main.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index f952d0f6c..58fea2cd4 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -48,8 +48,8 @@ fn main() -> Result<()> { randomize, )?; } - Command::Bench { verbose, path, op } => { - analysis_bench::run(verbose, path.as_ref(), op)?; + Command::Bench { verbosity, path, op } => { + analysis_bench::run(verbosity, path.as_ref(), op)?; } Command::HelpPrinted => (), } @@ -97,7 +97,7 @@ enum Command { path: PathBuf, }, Bench { - verbose: bool, + verbosity: Verbosity, path: PathBuf, op: analysis_bench::Op, }, @@ -109,6 +109,19 @@ impl Command { let mut matches = Arguments::from_env(); let subcommand = matches.subcommand()?.unwrap_or_default(); + let verbosity = match ( + matches.contains(["-vv", "--spammy"]), + matches.contains(["-v", "--verbose"]), + matches.contains(["-q", "--quiet"]), + ) { + (true, _, true) => Err("Invalid flags: -q conflicts with -vv")?, + (true, _, false) => Verbosity::Spammy, + (false, false, false) => Verbosity::Normal, + (false, false, true) => Verbosity::Quiet, + (false, true, false) => Verbosity::Verbose, + (false, true, true) => Err("Invalid flags: -q conflicts with -v")?, + }; + let command = match subcommand.as_str() { "parse" => { if matches.contains(["-h", "--help"]) { @@ -193,18 +206,6 @@ ARGS: return Ok(Command::HelpPrinted); } - let verbosity = match ( - matches.contains(["-vv", "--spammy"]), - matches.contains(["-v", "--verbose"]), - matches.contains(["-q", "--quiet"]), - ) { - (true, _, true) => Err("Invalid flags: -q conflicts with -vv")?, - (true, _, false) => Verbosity::Spammy, - (false, false, false) => Verbosity::Normal, - (false, false, true) => Verbosity::Quiet, - (false, true, false) => Verbosity::Verbose, - (false, true, true) => Err("Invalid flags: -q conflicts with -v")?, - }; let randomize = matches.contains("--randomize"); let memory_usage = matches.contains("--memory-usage"); let only: Option = matches.opt_value_from_str(["-o", "--only"])?; @@ -242,7 +243,6 @@ ARGS: return Ok(Command::HelpPrinted); } - let verbose = matches.contains(["-v", "--verbose"]); let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); let highlight_path: Option = matches.opt_value_from_str("--highlight")?; let complete_path: Option = matches.opt_value_from_str("--complete")?; @@ -255,7 +255,7 @@ ARGS: "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" ), }; - Command::Bench { verbose, path, op } + Command::Bench { verbosity, path, op } } _ => { eprintln!( -- cgit v1.2.3 From 3ef916061b208ce6f746562bc732b4a437639505 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:09:07 +0100 Subject: More precise types --- crates/ra_cli/src/main.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 58fea2cd4..5302f6ed2 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -12,12 +12,15 @@ use ra_ide::{file_structure, Analysis}; use ra_prof::profile; use ra_syntax::{AstNode, SourceFile}; -type Result = std::result::Result>; +type Result> = std::result::Result; fn main() -> Result<()> { env_logger::try_init()?; - let command = Command::from_args()?; + let command = match Command::from_args()? { + Ok(it) => it, + Err(HelpPrinted) => return Ok(()), + }; match command { Command::Parse { no_dump } => { let _p = profile("parsing"); @@ -51,7 +54,6 @@ fn main() -> Result<()> { Command::Bench { verbosity, path, op } => { analysis_bench::run(verbosity, path.as_ref(), op)?; } - Command::HelpPrinted => (), } Ok(()) @@ -101,11 +103,12 @@ enum Command { path: PathBuf, op: analysis_bench::Op, }, - HelpPrinted, } +struct HelpPrinted; + impl Command { - fn from_args() -> Result { + fn from_args() -> Result> { let mut matches = Arguments::from_env(); let subcommand = matches.subcommand()?.unwrap_or_default(); @@ -136,7 +139,7 @@ FLAGS: -h, --help Prints help inforamtion --no-dump" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let no_dump = matches.contains("--no-dump"); @@ -155,7 +158,7 @@ USAGE: FLAGS: -h, --help Prints help inforamtion" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } matches.finish().or_else(handle_extra_flags)?; @@ -175,7 +178,7 @@ FLAGS: -h, --help Prints help information -r, --rainbow" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let rainbow = matches.contains(["-r", "--rainbow"]); @@ -203,7 +206,7 @@ OPTIONS: ARGS: " ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let randomize = matches.contains("--randomize"); @@ -240,7 +243,7 @@ OPTIONS: ARGS: Project to analyse" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); @@ -275,10 +278,10 @@ SUBCOMMANDS: parse symbols" ); - return Ok(Command::HelpPrinted); + return Ok(Err(HelpPrinted)); } }; - Ok(command) + Ok(Ok(command)) } } -- cgit v1.2.3 From 06e37b273ea6f50d4d8cceccd85e613a5840b4c0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:10:15 +0100 Subject: Better name --- crates/ra_cli/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 5302f6ed2..f4eb0bd3c 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -17,7 +17,7 @@ type Result> = std::result::Result; fn main() -> Result<()> { env_logger::try_init()?; - let command = match Command::from_args()? { + let command = match Command::from_env_args()? { Ok(it) => it, Err(HelpPrinted) => return Ok(()), }; @@ -108,7 +108,7 @@ enum Command { struct HelpPrinted; impl Command { - fn from_args() -> Result> { + fn from_env_args() -> Result> { let mut matches = Arguments::from_env(); let subcommand = matches.subcommand()?.unwrap_or_default(); -- cgit v1.2.3 From 8e86d12771c0d77dce82cf80671d4cf9383057c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:15:19 +0100 Subject: Cleanup --- crates/ra_cli/src/main.rs | 88 ++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 31 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index f4eb0bd3c..66258c860 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -5,7 +5,7 @@ mod analysis_stats; mod analysis_bench; mod progress_report; -use std::{error::Error, fmt::Write, io::Read, path::PathBuf}; +use std::{error::Error, fmt::Write, io::Read, path::PathBuf, str::FromStr}; use pico_args::Arguments; use ra_ide::{file_structure, Analysis}; @@ -51,14 +51,37 @@ fn main() -> Result<()> { randomize, )?; } - Command::Bench { verbosity, path, op } => { - analysis_bench::run(verbosity, path.as_ref(), op)?; + Command::Bench { verbosity, path, what } => { + analysis_bench::run(verbosity, path.as_ref(), what)?; } } Ok(()) } +enum Command { + Parse { + no_dump: bool, + }, + Symbols, + Highlight { + rainbow: bool, + }, + Stats { + verbosity: Verbosity, + randomize: bool, + memory_usage: bool, + only: Option, + with_deps: bool, + path: PathBuf, + }, + Bench { + verbosity: Verbosity, + path: PathBuf, + what: BenchWhat, + }, +} + #[derive(Clone, Copy)] pub enum Verbosity { Spammy, @@ -82,27 +105,30 @@ impl Verbosity { } } -enum Command { - Parse { - no_dump: bool, - }, - Symbols, - Highlight { - rainbow: bool, - }, - Stats { - verbosity: Verbosity, - randomize: bool, - memory_usage: bool, - only: Option, - with_deps: bool, - path: PathBuf, - }, - Bench { - verbosity: Verbosity, - path: PathBuf, - op: analysis_bench::Op, - }, +enum BenchWhat { + Highlight { path: PathBuf }, + Complete(Position), + GotoDef(Position), +} + +pub(crate) struct Position { + path: PathBuf, + line: u32, + column: u32, +} + +impl FromStr for Position { + type Err = Box; + fn from_str(s: &str) -> Result { + let (path_line, column) = rsplit_at_char(s, ':')?; + let (path, line) = rsplit_at_char(path_line, ':')?; + Ok(Position { path: path.into(), line: line.parse()?, column: column.parse()? }) + } +} + +fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { + let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; + Ok((&s[..idx], &s[idx + 1..])) } struct HelpPrinted; @@ -248,17 +274,17 @@ ARGS: let path: PathBuf = matches.opt_value_from_str("--path")?.unwrap_or_default(); let highlight_path: Option = matches.opt_value_from_str("--highlight")?; - let complete_path: Option = matches.opt_value_from_str("--complete")?; - let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; - let op = match (highlight_path, complete_path, goto_def_path) { - (Some(path), None, None) => analysis_bench::Op::Highlight { path: path.into() }, - (None, Some(position), None) => analysis_bench::Op::Complete(position.parse()?), - (None, None, Some(position)) => analysis_bench::Op::GotoDef(position.parse()?), + let complete_path: Option = matches.opt_value_from_str("--complete")?; + let goto_def_path: Option = matches.opt_value_from_str("--goto-def")?; + let what = match (highlight_path, complete_path, goto_def_path) { + (Some(path), None, None) => BenchWhat::Highlight { path: path.into() }, + (None, Some(position), None) => BenchWhat::Complete(position), + (None, None, Some(position)) => BenchWhat::GotoDef(position), _ => panic!( "exactly one of `--highlight`, `--complete` or `--goto-def` must be set" ), }; - Command::Bench { verbosity, path, op } + Command::Bench { verbosity, path, what } } _ => { eprintln!( -- cgit v1.2.3 From 2d1b3da5fb69d932c65884a361ec10d81e8a51d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 18:19:25 +0100 Subject: Use anyhow --- crates/ra_cli/src/main.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'crates/ra_cli/src/main.rs') diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 66258c860..4cf062f47 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -5,14 +5,14 @@ mod analysis_stats; mod analysis_bench; mod progress_report; -use std::{error::Error, fmt::Write, io::Read, path::PathBuf, str::FromStr}; +use std::{fmt::Write, io::Read, path::PathBuf, str::FromStr}; use pico_args::Arguments; use ra_ide::{file_structure, Analysis}; use ra_prof::profile; use ra_syntax::{AstNode, SourceFile}; -type Result> = std::result::Result; +use anyhow::{bail, format_err, Result}; fn main() -> Result<()> { env_logger::try_init()?; @@ -118,7 +118,7 @@ pub(crate) struct Position { } impl FromStr for Position { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { let (path_line, column) = rsplit_at_char(s, ':')?; let (path, line) = rsplit_at_char(path_line, ':')?; @@ -127,7 +127,7 @@ impl FromStr for Position { } fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { - let idx = s.rfind(':').ok_or_else(|| format!("no `{}` in {}", c, s))?; + let idx = s.rfind(c).ok_or_else(|| format_err!("no `{}` in {}", c, s))?; Ok((&s[..idx], &s[idx + 1..])) } @@ -143,12 +143,12 @@ impl Command { matches.contains(["-v", "--verbose"]), matches.contains(["-q", "--quiet"]), ) { - (true, _, true) => Err("Invalid flags: -q conflicts with -vv")?, + (true, _, true) => bail!("Invalid flags: -q conflicts with -vv"), (true, _, false) => Verbosity::Spammy, (false, false, false) => Verbosity::Normal, (false, false, true) => Verbosity::Quiet, (false, true, false) => Verbosity::Verbose, - (false, true, true) => Err("Invalid flags: -q conflicts with -v")?, + (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), }; let command = match subcommand.as_str() { @@ -242,7 +242,7 @@ ARGS: let path = { let mut trailing = matches.free()?; if trailing.len() != 1 { - Err("Invalid flags")?; + bail!("Invalid flags"); } trailing.pop().unwrap().into() }; @@ -318,9 +318,9 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> { write!(&mut invalid_flags, "{}, ", flag)?; } let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); - Err(format!("Invalid flags: {}", invalid_flags).into()) + bail!("Invalid flags: {}", invalid_flags); } else { - Err(e.to_string().into()) + bail!(e); } } -- cgit v1.2.3