Switch from anyhow to eyre

This commit is contained in:
cyqsimon
2024-12-16 11:18:37 +08:00
parent 7c9093ef99
commit 1dde635c4e
7 changed files with 35 additions and 20 deletions

21
Cargo.lock generated
View File

@@ -112,9 +112,6 @@ name = "anyhow"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775"
dependencies = [
"backtrace",
]
[[package]]
name = "arbitrary"
@@ -161,7 +158,6 @@ dependencies = [
name = "bandwhich"
version = "0.23.1"
dependencies = [
"anyhow",
"async-trait",
"chrono",
"clap",
@@ -170,6 +166,7 @@ dependencies = [
"clap_mangen",
"crossterm",
"derive_more",
"eyre",
"http_req",
"insta",
"ipnetwork",
@@ -703,6 +700,16 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "eyre"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "fastrand"
version = "2.1.1"
@@ -990,6 +997,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "indexmap"
version = "2.6.0"

View File

@@ -26,12 +26,13 @@ default = []
ui_test = []
[dependencies]
anyhow = { version = "1.0.93", features = ["backtrace"] }
async-trait = "0.1.83"
chrono = "0.4"
clap-verbosity-flag = "3.0.1"
clap = { version = "4.5.21", features = ["derive"] }
crossterm = "0.28.1"
derive_more = { version = "1.0.0", features = ["debug"] }
eyre = "0.6.12"
ipnetwork = "0.20.0"
itertools = "0.13.0"
log = "0.4.22"
@@ -46,7 +47,7 @@ tokio = { version = "1.41", features = ["rt", "sync"] }
trust-dns-resolver = "0.23.2"
unicode-width = "0.2.0"
strum = { version = "0.26.3", features = ["derive"] }
derive_more = { version = "1.0.0", features = ["debug"] }
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
procfs = "0.17.0"
@@ -66,12 +67,12 @@ regex = "1.11.1"
rstest = "0.23.0"
[build-dependencies]
anyhow = "1.0.93"
clap = { version = "4.5.21", features = ["derive"] }
clap-verbosity-flag = "3.0.1"
clap_complete = "4.5.38"
clap_mangen = "0.2.24"
derive_more = { version = "1.0.0", features = ["debug"] }
eyre = "0.6.12"
strum = { version = "0.26.3", features = ["derive"] }
[target.'cfg(target_os = "windows")'.build-dependencies]

View File

@@ -1,9 +1,9 @@
use std::{env, fs::File};
use anyhow::anyhow;
use clap::CommandFactory;
use clap_complete::Shell;
use clap_mangen::Man;
use eyre::eyre;
fn main() {
build_completion_manpage().unwrap();
@@ -14,13 +14,13 @@ fn main() {
include!("src/cli.rs");
fn build_completion_manpage() -> anyhow::Result<()> {
fn build_completion_manpage() -> eyre::Result<()> {
let mut cmd = Opt::command();
// build into `BANDWHICH_GEN_DIR` with a fallback to `OUT_DIR`
let gen_dir: PathBuf = env::var_os("BANDWHICH_GEN_DIR")
.or_else(|| env::var_os("OUT_DIR"))
.ok_or(anyhow!("OUT_DIR is unset"))?
.ok_or(eyre!("OUT_DIR is unset"))?
.into();
// completion
@@ -37,7 +37,7 @@ fn build_completion_manpage() -> anyhow::Result<()> {
}
#[cfg(target_os = "windows")]
fn download_windows_npcap_sdk() -> anyhow::Result<()> {
fn download_windows_npcap_sdk() -> eyre::Result<()> {
use std::{
fs,
io::{self, Write},
@@ -102,7 +102,7 @@ fn download_windows_npcap_sdk() -> anyhow::Result<()> {
"cargo:rustc-link-search=native={}",
lib_dir
.to_str()
.ok_or(anyhow!("{lib_dir:?} is not valid UTF-8"))?
.ok_or(eyre!("{lib_dir:?} is not valid UTF-8"))?
);
Ok(())

View File

@@ -24,6 +24,7 @@ use crossterm::{
terminal,
};
use display::{elapsed_time, RawTerminalBackend, Ui};
use eyre::bail;
use network::{
dns::{self, IpTable},
LocalSocket, Sniffer, Utilization,
@@ -37,7 +38,7 @@ use crate::os::ProcessInfo;
const DISPLAY_DELTA: Duration = Duration::from_millis(1000);
fn main() -> anyhow::Result<()> {
fn main() -> eyre::Result<()> {
let opts = Opt::parse();
// init logging
@@ -59,7 +60,7 @@ fn main() -> anyhow::Result<()> {
start(terminal_backend, os_input, opts);
} else {
let Ok(()) = terminal::enable_raw_mode() else {
anyhow::bail!(
bail!(
"Failed to get stdout: if you are trying to pipe 'bandwhich' you should use the --raw flag"
)
};

View File

@@ -24,7 +24,7 @@ pub struct Client {
}
impl Client {
pub fn new<R>(resolver: R, runtime: Runtime) -> anyhow::Result<Self>
pub fn new<R>(resolver: R, runtime: Runtime) -> eyre::Result<Self>
where
R: Lookup + Send + Sync + 'static,
{

View File

@@ -15,7 +15,7 @@ pub trait Lookup {
pub struct Resolver(TokioAsyncResolver);
impl Resolver {
pub async fn new(dns_server: Option<Ipv4Addr>) -> anyhow::Result<Self> {
pub async fn new(dns_server: Option<Ipv4Addr>) -> eyre::Result<Self> {
let resolver = match dns_server {
Some(dns_server_address) => {
let mut config = ResolverConfig::new();

View File

@@ -4,8 +4,8 @@ use std::{
time,
};
use anyhow::{anyhow, bail};
use crossterm::event::{read, Event};
use eyre::{bail, eyre};
use itertools::Itertools;
use log::{debug, warn};
use pnet::datalink::{self, Channel::Ethernet, Config, DataLinkReceiver, NetworkInterface};
@@ -99,11 +99,11 @@ pub fn get_input(
interface_name: Option<&str>,
resolve: bool,
dns_server: Option<Ipv4Addr>,
) -> anyhow::Result<OsInputOutput> {
) -> eyre::Result<OsInputOutput> {
// get the user's requested interface, if any
// IDEA: allow requesting multiple interfaces
let requested_interfaces = interface_name
.map(|name| get_interface(name).ok_or_else(|| anyhow!("Cannot find interface {name}")))
.map(|name| get_interface(name).ok_or_else(|| eyre!("Cannot find interface {name}")))
.transpose()?
.map(|interface| vec![interface]);
@@ -200,7 +200,7 @@ pub fn get_input(
let resolver = runtime
.block_on(dns::Resolver::new(dns_server))
.map_err(|err| {
anyhow!("Could not initialize the DNS resolver. Are you offline?\n\nReason: {err}")
eyre!("Could not initialize the DNS resolver. Are you offline?\n\nReason: {err}")
})?;
let dns_client = dns::Client::new(resolver, runtime)?;
Some(dns_client)