diff options
author | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2023-08-25 19:02:37 +0800 |
---|---|---|
committer | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2023-08-25 19:02:37 +0800 |
commit | ffbf0cd8aa73ed3db3e9dc01f154eacb5fe1130c (patch) | |
tree | 7e20ef11e08466f09044d6423319779d1d70919b /src | |
parent | c780c06807fbed4388eb5636c30a4cc11eeb814c (diff) |
Migrate from `failure` to `anyhow`+`thiserror`
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/network/dns/client.rs | 2 | ||||
-rw-r--r-- | src/network/dns/resolver.rs | 2 | ||||
-rw-r--r-- | src/os/errors.rs | 45 | ||||
-rw-r--r-- | src/os/shared.rs | 24 |
5 files changed, 20 insertions, 57 deletions
diff --git a/src/main.rs b/src/main.rs index ee197aa..8d88ec1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,7 +77,7 @@ fn main() { } } -fn try_main() -> Result<(), failure::Error> { +fn try_main() -> anyhow::Result<()> { use os::get_input; let opts = Opt::from_args(); let os_input = get_input(&opts.interface, !opts.no_resolve, &opts.dns_server)?; @@ -94,7 +94,7 @@ fn try_main() -> Result<(), failure::Error> { let terminal_backend = CrosstermBackend::new(stdout); start(terminal_backend, os_input, opts); } - Err(_) => failure::bail!( + Err(_) => anyhow::bail!( "Failed to get stdout: if you are trying to pipe 'bandwhich' you should use the --raw flag" ), } diff --git a/src/network/dns/client.rs b/src/network/dns/client.rs index ef32180..ec47d50 100644 --- a/src/network/dns/client.rs +++ b/src/network/dns/client.rs @@ -22,7 +22,7 @@ pub struct Client { } impl Client { - pub fn new<R>(resolver: R, runtime: Runtime) -> Result<Self, failure::Error> + pub fn new<R>(resolver: R, runtime: Runtime) -> anyhow::Result<Self> where R: Lookup + Send + Sync + 'static, { diff --git a/src/network/dns/resolver.rs b/src/network/dns/resolver.rs index 7327654..4c43975 100644 --- a/src/network/dns/resolver.rs +++ b/src/network/dns/resolver.rs @@ -13,7 +13,7 @@ pub trait Lookup { pub struct Resolver(TokioAsyncResolver); impl Resolver { - pub async fn new(dns_server: &Option<Ipv4Addr>) -> Result<Self, failure::Error> { + pub async fn new(dns_server: &Option<Ipv4Addr>) -> anyhow::Result<Self> { let resolver = match dns_server { Some(dns_server_address) => { let mut config = ResolverConfig::new(); diff --git a/src/os/errors.rs b/src/os/errors.rs index bb3d6db..7a49238 100644 --- a/src/os/errors.rs +++ b/src/os/errors.rs @@ -1,44 +1,7 @@ -use std::fmt; - -use failure::{Backtrace, Context, Fail}; - -#[derive(Debug)] -pub struct GetInterfaceError { - inner: Context<GetInterfaceErrorKind>, -} - -impl From<GetInterfaceErrorKind> for GetInterfaceError { - fn from(kind: GetInterfaceErrorKind) -> GetInterfaceError { - GetInterfaceError { - inner: Context::new(kind), - } - } -} - -impl From<Context<GetInterfaceErrorKind>> for GetInterfaceError { - fn from(inner: Context<GetInterfaceErrorKind>) -> GetInterfaceError { - GetInterfaceError { inner } - } -} -#[derive(Clone, Eq, PartialEq, Debug, Fail)] -pub enum GetInterfaceErrorKind { - #[fail(display = "{}", _0)] +#[derive(Clone, Eq, PartialEq, Debug, thiserror::Error)] +pub enum GetInterfaceError { + #[error("Permission error: {0}")] PermissionError(String), - #[fail(display = "{}", _0)] + #[error("Other error: {0}")] OtherError(String), } -impl Fail for GetInterfaceError { - fn cause(&self) -> Option<&dyn Fail> { - self.inner.cause() - } - - fn backtrace(&self) -> Option<&Backtrace> { - self.inner.backtrace() - } -} - -impl fmt::Display for GetInterfaceError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(&self.inner, f) - } -} diff --git a/src/os/shared.rs b/src/os/shared.rs index f28e49e..f8d997d 100644 --- a/src/os/shared.rs +++ b/src/os/shared.rs @@ -9,7 +9,7 @@ use ::tokio::runtime::Runtime; use ::std::net::Ipv4Addr; use ::std::time; -use crate::os::errors::GetInterfaceErrorKind; +use crate::os::errors::GetInterfaceError; #[cfg(target_os = "linux")] use crate::os::linux::get_open_sockets; @@ -34,7 +34,7 @@ impl Iterator for TerminalEvents { pub(crate) fn get_datalink_channel( interface: &NetworkInterface, -) -> Result<Box<dyn DataLinkReceiver>, GetInterfaceErrorKind> { +) -> Result<Box<dyn DataLinkReceiver>, GetInterfaceError> { let config = Config { read_timeout: Some(time::Duration::new(1, 0)), read_buffer_size: 65536, @@ -43,15 +43,15 @@ pub(crate) fn get_datalink_channel( match datalink::channel(interface, config) { Ok(Ethernet(_tx, rx)) => Ok(rx), - Ok(_) => Err(GetInterfaceErrorKind::OtherError(format!( + Ok(_) => Err(GetInterfaceError::OtherError(format!( "{}: Unsupported interface type", interface.name ))), Err(e) => match e.kind() { - ErrorKind::PermissionDenied => Err(GetInterfaceErrorKind::PermissionError( + ErrorKind::PermissionDenied => Err(GetInterfaceError::PermissionError( interface.name.to_owned(), )), - _ => Err(GetInterfaceErrorKind::OtherError(format!( + _ => Err(GetInterfaceError::OtherError(format!( "{}: {}", &interface.name, e ))), @@ -85,7 +85,7 @@ where I: Iterator< Item = ( &'a NetworkInterface, - Result<Box<dyn DataLinkReceiver>, GetInterfaceErrorKind>, + Result<Box<dyn DataLinkReceiver>, GetInterfaceError>, ), >, { @@ -97,7 +97,7 @@ where |acc, (_, elem)| { if let Some(iface_error) = elem.err() { match iface_error { - GetInterfaceErrorKind::PermissionError(interface_name) => { + GetInterfaceError::PermissionError(interface_name) => { if let Some(prev_interface) = acc.permission { return UserErrors { permission: Some(format!("{}, {}", prev_interface, interface_name)), @@ -151,12 +151,12 @@ pub fn get_input( interface_name: &Option<String>, resolve: bool, dns_server: &Option<Ipv4Addr>, -) -> Result<OsInputOutput, failure::Error> { +) -> anyhow::Result<OsInputOutput> { let network_interfaces = if let Some(name) = interface_name { match get_interface(name) { Some(interface) => vec![interface], None => { - failure::bail!("Cannot find interface {}", name); + anyhow::bail!("Cannot find interface {}", name); // the homebrew formula relies on this wording, please be careful when changing } } @@ -195,10 +195,10 @@ pub fn get_input( if available_network_frames.is_empty() { let all_errors = collect_errors(network_frames.clone()); if !all_errors.is_empty() { - failure::bail!(all_errors); + anyhow::bail!(all_errors); } - failure::bail!("Failed to find any network interface to listen on."); + anyhow::bail!("Failed to find any network interface to listen on."); } let keyboard_events = Box::new(TerminalEvents); @@ -207,7 +207,7 @@ pub fn get_input( let runtime = Runtime::new()?; let resolver = match runtime.block_on(dns::Resolver::new(dns_server)) { Ok(resolver) => resolver, - Err(err) => failure::bail!( + Err(err) => anyhow::bail!( "Could not initialize the DNS resolver. Are you offline?\n\nReason: {:?}", err ), |