Use anyhow

This commit is contained in:
Aleksey Kladov 2020-02-17 18:19:25 +01:00
parent 8e86d12771
commit 2d1b3da5fb
5 changed files with 16 additions and 14 deletions

1
Cargo.lock generated
View file

@ -945,6 +945,7 @@ dependencies = [
name = "ra_cli"
version = "0.1.0"
dependencies = [
"anyhow",
"crossbeam-channel",
"env_logger",
"itertools",

View file

@ -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" }

View file

@ -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 {

View file

@ -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<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
use anyhow::Result;
fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
FileId(f.0)

View file

@ -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<T, E = Box<dyn Error + Send + Sync>> = std::result::Result<T, E>;
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<dyn std::error::Error + Send + Sync>;
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
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);
}
}