Skip to content

Commit

Permalink
Use anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Feb 17, 2020
1 parent 8e86d12 commit 2d1b3da
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/ra_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 2 additions & 1 deletion crates/ra_cli/src/analysis_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::{path::Path, sync::Arc, time::Instant};

use anyhow::format_err;
use ra_db::{
salsa::{Database, Durability},
FileId, SourceDatabaseExt,
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions crates/ra_cli/src/load_cargo.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
//! 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};
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)
Expand Down
18 changes: 9 additions & 9 deletions crates/ra_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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, ':')?;
Expand All @@ -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..]))
}

Expand All @@ -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() {
Expand Down Expand Up @@ -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()
};
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 2d1b3da

Please sign in to comment.