summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--talpid-core/src/winnet.rs52
1 files changed, 28 insertions, 24 deletions
diff --git a/talpid-core/src/winnet.rs b/talpid-core/src/winnet.rs
index f6a932acfb..1211cd7545 100644
--- a/talpid-core/src/winnet.rs
+++ b/talpid-core/src/winnet.rs
@@ -3,19 +3,17 @@ use std::ptr;
use libc::{c_char, c_void, wchar_t};
use widestring::WideCString;
-error_chain! {
- errors{
- /// Failure to set metrics of network interfaces
- MetricApplication{
- description("Failed to set the metrics for a network interface")
- }
- InvalidInterfaceAlias{
- description("Supplied interface alias is invalid")
- }
- GetIpv6Status {
- description("Failed to read IPv6 status on the TAP network interface")
- }
- }
+#[derive(err_derive::Error, Debug)]
+pub enum Error {
+ /// Failure to set metrics of network interfaces
+ #[error(display = "Failed to set the metrics for a network interface")]
+ MetricApplication,
+
+ #[error(display = "Supplied interface alias is invalid")]
+ InvalidInterfaceAlias(#[error(cause)] widestring::NulError<u16>),
+
+ #[error(display = "Failed to read IPv6 status on the TAP network interface")]
+ GetIpv6Status,
}
pub type ErrorSink = extern "system" fn(msg: *const c_char, ctx: *mut c_void);
@@ -30,9 +28,9 @@ pub extern "system" fn error_sink(msg: *const c_char, _ctx: *mut c_void) {
}
/// Returns true if metrics were changed, false otherwise
-pub fn ensure_top_metric_for_interface(interface_alias: &str) -> Result<bool> {
+pub fn ensure_top_metric_for_interface(interface_alias: &str) -> Result<bool, Error> {
let interface_alias_ws =
- WideCString::from_str(interface_alias).chain_err(|| ErrorKind::InvalidInterfaceAlias)?;
+ WideCString::from_str(interface_alias).map_err(Error::InvalidInterfaceAlias)?;
let metric_result = unsafe {
WinRoute_EnsureTopMetric(
@@ -48,11 +46,14 @@ pub fn ensure_top_metric_for_interface(interface_alias: &str) -> Result<bool> {
// Metrics changed
1 => Ok(true),
// Failure
- 2 => Err(Error::from(ErrorKind::MetricApplication)),
+ 2 => Err(Error::MetricApplication),
// Unexpected value
- _ => {
- log::error!("Unexpected return code from WinRoute_EnsureTopMetric");
- Err(Error::from(ErrorKind::MetricApplication))
+ i => {
+ log::error!(
+ "Unexpected return code from WinRoute_EnsureTopMetric: {}",
+ i
+ );
+ Err(Error::MetricApplication)
}
}
}
@@ -68,7 +69,7 @@ extern "system" {
/// Checks if IPv6 is enabled for the TAP interface
-pub fn get_tap_interface_ipv6_status() -> Result<bool> {
+pub fn get_tap_interface_ipv6_status() -> Result<bool, Error> {
let tap_ipv6_status = unsafe { GetTapInterfaceIpv6Status(Some(error_sink), ptr::null_mut()) };
match tap_ipv6_status {
@@ -77,11 +78,14 @@ pub fn get_tap_interface_ipv6_status() -> Result<bool> {
// Disabled
1 => Ok(false),
// Failure
- 2 => Err(Error::from(ErrorKind::GetIpv6Status)),
+ 2 => Err(Error::GetIpv6Status),
// Unexpected value
- _ => {
- log::error!("Unexpected return code from GetTapInterfaceIpv6Status");
- Err(Error::from(ErrorKind::GetIpv6Status))
+ i => {
+ log::error!(
+ "Unexpected return code from GetTapInterfaceIpv6Status: {}",
+ i
+ );
+ Err(Error::GetIpv6Status)
}
}
}