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/Cargo.toml | 1 + crates/ra_cli/src/analysis_bench.rs | 3 ++- crates/ra_cli/src/load_cargo.rs | 7 +++---- crates/ra_cli/src/main.rs | 18 +++++++++--------- 4 files changed, 15 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ra_cli/Cargo.toml b/crates/ra_cli/Cargo.toml index 03494a809..ce88a76b1 100644 --- a/crates/ra_cli/Cargo.toml +++ b/crates/ra_cli/Cargo.toml @@ -13,6 +13,7 @@ log = "0.4.5" pico-args = "0.3.0" rand = { version = "0.7.0", features = ["small_rng"] } rustc-hash = "1.0" +anyhow = "1.0" hir = { path = "../ra_hir", package = "ra_hir" } hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs index 3dc8dc691..91fc55fe2 100644 --- a/crates/ra_cli/src/analysis_bench.rs +++ b/crates/ra_cli/src/analysis_bench.rs @@ -2,6 +2,7 @@ use std::{path::Path, sync::Arc, time::Instant}; +use anyhow::format_err; use ra_db::{ salsa::{Database, Durability}, FileId, SourceDatabaseExt, @@ -39,7 +40,7 @@ pub(crate) fn run(verbosity: Verbosity, path: &Path, what: BenchWhat) -> Result< } None }) - .ok_or_else(|| format!("Can't find {:?}", path))? + .ok_or_else(|| format_err!("Can't find {}", path.display()))? }; match &what { diff --git a/crates/ra_cli/src/load_cargo.rs b/crates/ra_cli/src/load_cargo.rs index 2d6433f18..b9a4e6aba 100644 --- a/crates/ra_cli/src/load_cargo.rs +++ b/crates/ra_cli/src/load_cargo.rs @@ -1,8 +1,6 @@ //! FIXME: write short doc here -use std::{collections::HashSet, error::Error, path::Path}; - -use rustc_hash::FxHashMap; +use std::{collections::HashSet, path::Path}; use crossbeam_channel::{unbounded, Receiver}; use ra_db::{CrateGraph, FileId, SourceRootId}; @@ -10,8 +8,9 @@ use ra_ide::{AnalysisChange, AnalysisHost, FeatureFlags}; use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace}; use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; use ra_vfs_glob::RustPackageFilterBuilder; +use rustc_hash::FxHashMap; -type Result = std::result::Result>; +use anyhow::Result; fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId { FileId(f.0) 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