Skip to content

Commit

Permalink
Merge pull request #450 from imsnif/eyre
Browse files Browse the repository at this point in the history
Switch from `anyhow` to `eyre`
  • Loading branch information
cyqsimon authored Dec 16, 2024
2 parents 206c4c8 + e77d7b4 commit 962a354
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* Add build optimizations for release binary #434 - @pando85
* Minor cleanup and optimisations #435 - @cyqsimon
* Bump `pnet` & `packet-builder` #444 - @cyqsimon
* Switch from anyhow to eyre #450 - @cyqsimon

## [0.23.1] - 2024-10-09

Expand Down
21 changes: 17 additions & 4 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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
Expand All @@ -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},
Expand Down Expand Up @@ -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(())
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crossterm::{
terminal,
};
use display::{elapsed_time, RawTerminalBackend, Ui};
use eyre::bail;
use network::{
dns::{self, IpTable},
LocalSocket, Sniffer, Utilization,
Expand All @@ -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
Expand All @@ -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"
)
};
Expand Down
2 changes: 1 addition & 1 deletion src/network/dns/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
2 changes: 1 addition & 1 deletion src/network/dns/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/os/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 962a354

Please sign in to comment.