mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-26 01:31:57 -07:00
core: switch to anyhow
This commit adds `anyhow` as a dependency and switches over to it from Box<dyn Error>. It actually looks like I've kept all of my errors rather shallow, such that we don't get a huge benefit from anyhow at present. But now that anyhow is in use, I expect to use its "context" feature more going forward.
This commit is contained in:
@@ -40,16 +40,14 @@ mod subject;
|
||||
#[global_allocator]
|
||||
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
||||
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
fn main() {
|
||||
if let Err(err) = Args::parse().and_then(try_main) {
|
||||
eprintln_locked!("{}", err);
|
||||
eprintln_locked!("{:#}", err);
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
fn try_main(args: Args) -> Result<()> {
|
||||
fn try_main(args: Args) -> anyhow::Result<()> {
|
||||
use args::Command::*;
|
||||
|
||||
let matched = match args.command() {
|
||||
@@ -73,7 +71,7 @@ fn try_main(args: Args) -> Result<()> {
|
||||
/// The top-level entry point for single-threaded search. This recursively
|
||||
/// steps through the file list (current directory by default) and searches
|
||||
/// each file sequentially.
|
||||
fn search(args: &Args) -> Result<bool> {
|
||||
fn search(args: &Args) -> anyhow::Result<bool> {
|
||||
/// The meat of the routine is here. This lets us call the same iteration
|
||||
/// code over each file regardless of whether we stream over the files
|
||||
/// as they're produced by the underlying directory traversal or whether
|
||||
@@ -82,7 +80,7 @@ fn search(args: &Args) -> Result<bool> {
|
||||
args: &Args,
|
||||
subjects: impl Iterator<Item = Subject>,
|
||||
started_at: std::time::Instant,
|
||||
) -> Result<bool> {
|
||||
) -> anyhow::Result<bool> {
|
||||
let quit_after_match = args.quit_after_match()?;
|
||||
let mut stats = args.stats()?;
|
||||
let mut searcher = args.search_worker(args.stdout())?;
|
||||
@@ -138,9 +136,8 @@ fn search(args: &Args) -> Result<bool> {
|
||||
///
|
||||
/// Requesting a sorted output from ripgrep (such as with `--sort path`) will
|
||||
/// automatically disable parallelism and hence sorting is not handled here.
|
||||
fn search_parallel(args: &Args) -> Result<bool> {
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
fn search_parallel(args: &Args) -> anyhow::Result<bool> {
|
||||
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
|
||||
|
||||
let quit_after_match = args.quit_after_match()?;
|
||||
let started_at = Instant::now();
|
||||
@@ -227,7 +224,7 @@ fn eprint_nothing_searched() {
|
||||
/// The top-level entry point for listing files without searching them. This
|
||||
/// recursively steps through the file list (current directory by default) and
|
||||
/// prints each path sequentially using a single thread.
|
||||
fn files(args: &Args) -> Result<bool> {
|
||||
fn files(args: &Args) -> anyhow::Result<bool> {
|
||||
/// The meat of the routine is here. This lets us call the same iteration
|
||||
/// code over each file regardless of whether we stream over the files
|
||||
/// as they're produced by the underlying directory traversal or whether
|
||||
@@ -235,7 +232,7 @@ fn files(args: &Args) -> Result<bool> {
|
||||
fn iter(
|
||||
args: &Args,
|
||||
subjects: impl Iterator<Item = Subject>,
|
||||
) -> Result<bool> {
|
||||
) -> anyhow::Result<bool> {
|
||||
let quit_after_match = args.quit_after_match()?;
|
||||
let mut matched = false;
|
||||
let mut path_printer = args.path_printer(args.stdout())?;
|
||||
@@ -276,7 +273,7 @@ fn files(args: &Args) -> Result<bool> {
|
||||
///
|
||||
/// Requesting a sorted output from ripgrep (such as with `--sort path`) will
|
||||
/// automatically disable parallelism and hence sorting is not handled here.
|
||||
fn files_parallel(args: &Args) -> Result<bool> {
|
||||
fn files_parallel(args: &Args) -> anyhow::Result<bool> {
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::sync::mpsc;
|
||||
@@ -328,7 +325,7 @@ fn files_parallel(args: &Args) -> Result<bool> {
|
||||
}
|
||||
|
||||
/// The top-level entry point for --type-list.
|
||||
fn types(args: &Args) -> Result<bool> {
|
||||
fn types(args: &Args) -> anyhow::Result<bool> {
|
||||
let mut count = 0;
|
||||
let mut stdout = args.stdout();
|
||||
for def in args.type_defs()? {
|
||||
@@ -350,9 +347,9 @@ fn types(args: &Args) -> Result<bool> {
|
||||
}
|
||||
|
||||
/// The top-level entry point for --pcre2-version.
|
||||
fn pcre2_version(args: &Args) -> Result<bool> {
|
||||
fn pcre2_version(args: &Args) -> anyhow::Result<bool> {
|
||||
#[cfg(feature = "pcre2")]
|
||||
fn imp(args: &Args) -> Result<bool> {
|
||||
fn imp(args: &Args) -> anyhow::Result<bool> {
|
||||
use grep::pcre2;
|
||||
|
||||
let mut stdout = args.stdout();
|
||||
@@ -367,7 +364,7 @@ fn pcre2_version(args: &Args) -> Result<bool> {
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "pcre2"))]
|
||||
fn imp(args: &Args) -> Result<bool> {
|
||||
fn imp(args: &Args) -> anyhow::Result<bool> {
|
||||
let mut stdout = args.stdout();
|
||||
writeln!(stdout, "PCRE2 is not available in this build of ripgrep.")?;
|
||||
Ok(false)
|
||||
|
Reference in New Issue
Block a user