Skip to content

Commit

Permalink
Switch to thiserror and anyhow
Browse files Browse the repository at this point in the history
Since failure is now deprecated.

See rust-lang-deprecated/failure#347
  • Loading branch information
gluxon committed May 3, 2020
1 parent 8dd9481 commit a8bad33
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 74 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ readme = "README.md"

[dependencies]
derive_builder = "0.7.1"
failure = "0.1.5"
libc = "0.2.66"
neli = "0.4.3"
thiserror = "1.0"

[dev-dependencies]
anyhow = "1.0"
base64 = "0.10.1"
colored = "1.7"
predicates = "1"
rand = "0.6.5"
rand = "0.6.5"
4 changes: 2 additions & 2 deletions examples/wg.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow;
use base64;
use colored::*;
use failure;
use std::env;
use wireguard_uapi;
use wireguard_uapi::get::{AllowedIp, Device, Peer};

fn main() -> Result<(), failure::Error> {
fn main() -> anyhow::Result<()> {
let mut args = env::args();
let _prog_name = args.next();
let ifname = args.next().expect("Please provide an interface name");
Expand Down
12 changes: 6 additions & 6 deletions src/err/connect_error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use failure::Fail;
use neli::err::NlError;
use thiserror::Error;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ConnectError {
#[fail(display = "{}", _0)]
NlError(#[fail(cause)] NlError),
#[error(transparent)]
NlError(NlError),

#[fail(display = "Unable to connect to the WireGuard DKMS. Is WireGuard installed?")]
ResolveFamilyError(#[fail(cause)] NlError),
#[error("Unable to connect to the WireGuard DKMS. Is WireGuard installed?")]
ResolveFamilyError(#[source] NlError),
}

impl From<NlError> for ConnectError {
Expand Down
22 changes: 11 additions & 11 deletions src/err/get_device_error.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use crate::err::ParseDeviceError;
use failure::Fail;
use neli::err::{NlError, SerError};
use thiserror::Error;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum GetDeviceError {
#[fail(display = "{}", _0)]
NlError(#[fail(cause)] NlError),
#[error(transparent)]
NlError(NlError),

#[fail(display = "{}", _0)]
NlSerError(#[fail(cause)] SerError),
#[error(transparent)]
NlSerError(SerError),

#[fail(display = "Interface names must be 1 to IFNAMSIZ-1 characters")]
#[error("Interface names must be 1 to IFNAMSIZ-1 characters")]
InvalidInterfaceName,

#[fail(
display = "Unable to get interface from WireGuard. Make sure it exists and you have permissions to access it."
#[error(
"Unable to get interface from WireGuard. Make sure it exists and you have permissions to access it."
)]
AccessError,

#[fail(display = "{}", _0)]
ParseDeviceError(#[fail(cause)] ParseDeviceError),
#[error(transparent)]
ParseDeviceError(ParseDeviceError),
}

impl From<NlError> for GetDeviceError {
Expand Down
18 changes: 9 additions & 9 deletions src/err/link_device_error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use failure::Fail;
use neli::err::{NlError, SerError};
use thiserror::Error;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum LinkDeviceError {
#[fail(display = "{}", _0)]
NlError(#[fail(cause)] NlError),
#[error(transparent)]
NlError(NlError),

#[fail(display = "{}", _0)]
NlSerError(#[fail(cause)] SerError),
#[error(transparent)]
NlSerError(SerError),

#[fail(display = "Interface names must be 1 to IFNAMSIZ-1 characters")]
#[error("Interface names must be 1 to IFNAMSIZ-1 characters")]
InvalidInterfaceName,

#[fail(
display = "Unable to get interface from WireGuard. Make sure it exists and you have permissions to access it."
#[error(
"Unable to get interface from WireGuard. Make sure it exists and you have permissions to access it."
)]
AccessError,
}
Expand Down
39 changes: 20 additions & 19 deletions src/err/parse_attribute_error.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
use failure::Fail;
use libc;
use std::num::TryFromIntError;
use std::string::FromUtf8Error;
use thiserror::Error;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ParseAttributeError {
#[fail(
display = "Error parsing Netlink attribute. Expected {} bytes, found {}.",
expected, found
#[error(
"Error parsing Netlink attribute. Expected {} bytes, found {}.",
expected,
found
)]
StaticLengthError { expected: usize, found: usize },

#[fail(display = "{}", _0)]
ParseSockAddrError(#[fail(cause)] ParseSockAddrError),
#[error("{0}")]
ParseSockAddrError(#[source] ParseSockAddrError),

#[fail(display = "{}", _0)]
ParseIpAddrError(#[fail(cause)] ParseIpAddrError),
#[error("{0}")]
ParseIpAddrError(#[source] ParseIpAddrError),

#[fail(display = "{}", _0)]
FromUtf8Error(#[fail(cause)] FromUtf8Error),
#[error("{0}")]
FromUtf8Error(#[source] FromUtf8Error),

#[fail(display = "{}", _0)]
TryFromIntError(#[fail(cause)] TryFromIntError),
#[error("{0}")]
TryFromIntError(#[source] TryFromIntError),

#[fail(display = "Expected a null-terminated string in Netlink response")]
#[error("Expected a null-terminated string in Netlink response")]
InvalidCStringError,
}

Expand All @@ -39,9 +40,9 @@ impl From<TryFromIntError> for ParseAttributeError {
}
}

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ParseSockAddrError {
#[fail(display = "Unrecognized address family")]
#[error("Unrecognized address family")]
UnrecognizedAddressFamilyError { id: libc::c_int },
}

Expand All @@ -51,10 +52,10 @@ impl From<ParseSockAddrError> for ParseAttributeError {
}
}

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ParseIpAddrError {
#[fail(
display = "Payload does not correspond to known ip address lengths. Found {}.",
#[error(
"Payload does not correspond to known ip address lengths. Found {}.",
found
)]
InvalidIpAddrLengthError { found: usize },
Expand Down
24 changes: 12 additions & 12 deletions src/err/parse_device_error.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use crate::err::ParseAttributeError;
use failure::Fail;
use neli::err::{DeError, NlError};
use thiserror::Error;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum ParseDeviceError {
#[fail(display = "{}", _0)]
NlError(#[fail(cause)] NlError),
#[error(transparent)]
NlError(NlError),

#[fail(display = "{}", _0)]
NlDeError(#[fail(cause)] DeError),
#[error(transparent)]
NlDeError(DeError),

#[fail(display = "{}", _0)]
#[error("{0}")]
String(String),

#[fail(display = "{}", _0)]
ParseAttributeError(#[fail(cause)] ParseAttributeError),
#[error(transparent)]
ParseAttributeError(ParseAttributeError),

#[fail(display = "Encountered unknown device attribute id {}", id)]
#[error("Encountered unknown device attribute id {}", id)]
UnknownDeviceAttributeError { id: u16 },

#[fail(display = "Encountered unknown peer attribute id {}", id)]
#[error("Encountered unknown peer attribute id {}", id)]
UnknownPeerAttributeError { id: u16 },

#[fail(display = "Encountered unknown allowed ip attribute id {}", id)]
#[error("Encountered unknown allowed ip attribute id {}", id)]
UnknownAllowedIpAttributeError { id: u16 },
}

Expand Down
12 changes: 6 additions & 6 deletions src/err/set_device_error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use failure::Fail;
use neli::err::{NlError, SerError};
use thiserror::Error;

#[derive(Fail, Debug)]
#[derive(Error, Debug)]
pub enum SetDeviceError {
#[fail(display = "{}", _0)]
NlError(#[fail(cause)] NlError),
#[error(transparent)]
NlError(NlError),

#[fail(display = "{}", _0)]
NlSerError(#[fail(cause)] SerError),
#[error(transparent)]
NlSerError(SerError),
}

impl From<NlError> for SetDeviceError {
Expand Down
6 changes: 3 additions & 3 deletions src/socket/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ pub fn parse_in6_addr(buf: &[u8]) -> Result<Ipv6Addr, ParseAttributeError> {
mod tests {
use super::*;
use crate::cmd::WgCmd;
use anyhow::Error;
use base64;
use failure;
use neli::err::DeError;
use neli::genl::Genlmsghdr;
use neli::Nl;
Expand All @@ -345,7 +345,7 @@ mod tests {
}

#[test]
fn parse_device_example_from_man_page() -> Result<(), failure::Error> {
fn parse_device_example_from_man_page() -> Result<(), Error> {
// This payload comes from the configuration example in "man wg", but with the third peer
// removed since it specifies an invalid endpoint.
let payload = vec![
Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
}

#[test]
fn parse_device_with_large_peer() -> Result<(), failure::Error> {
fn parse_device_with_large_peer() -> Result<(), Error> {
let first_payload = [
0, 1, 0, 0, 6, 0, 6, 0, 108, 202, 0, 0, 8, 0, 7, 0, 0, 0, 0, 0, 8, 0, 1, 0, 6, 0, 0, 0,
9, 0, 2, 0, 116, 101, 115, 116, 0, 0, 0, 0, 36, 0, 3, 0, 200, 9, 243, 229, 49, 126,
Expand Down
8 changes: 4 additions & 4 deletions tests/set_and_get.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow;
use base64;
use failure;
use libc;
use rand;
use std::net::{IpAddr, Ipv6Addr};
Expand Down Expand Up @@ -32,7 +32,7 @@ fn create_set_allowed_ips(allowed_ips: &[get::AllowedIp]) -> Vec<set::AllowedIp>
///
/// - Create it with: ip link add wgtest0 type wireguard
/// - Remove it with: ip link del wgtest0
fn simple() -> Result<(), failure::Error> {
fn simple() -> anyhow::Result<()> {
let mut test_device = get::Device {
ifindex: 0,
ifname: get_random_ifname(),
Expand Down Expand Up @@ -132,7 +132,7 @@ fn simple() -> Result<(), failure::Error> {
}

#[test]
fn set_ifname_has_proper_padding() -> Result<(), failure::Error> {
fn set_ifname_has_proper_padding() -> anyhow::Result<()> {
let ifname = get_random_ifname();
let listen_port = rand::random::<u16>();

Expand All @@ -156,7 +156,7 @@ fn set_ifname_has_proper_padding() -> Result<(), failure::Error> {
}

#[test]
fn large_peer() -> Result<(), failure::Error> {
fn large_peer() -> anyhow::Result<()> {
let mut test_device = get::Device {
ifindex: 6,
ifname: get_random_ifname(),
Expand Down

0 comments on commit a8bad33

Please sign in to comment.