Skip to content

Commit

Permalink
Use anyhow instead of the deprecated failure crate (#201)
Browse files Browse the repository at this point in the history
* Switch to use anyhow crate instead of failure crate

Include Cargo.lock updates

* Rebase and increment patch version

* Merge conflict

* Add missing version updates after rebase
  • Loading branch information
romoh committed Jan 14, 2021
1 parent 055c703 commit c706128
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 166 deletions.
93 changes: 9 additions & 84 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions agent/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "agent"
version = "0.1.8"
version = "0.1.9"
authors = ["Kate Goldenring <kate.goldenring@microsoft.com>", "<bfjelds@microsoft.com>"]
edition = "2018"

Expand All @@ -13,7 +13,7 @@ blake2 = "0.8.0"
chrono = "0.4.10"
cfg-if = "0.1"
env_logger = "0.6.1"
failure = "0.1.5"
anyhow = "1.0.38"
futures = { version = "0.3.1", package = "futures" }
futures-core = "0.3"
futures-util = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion agent/src/protocols/debug_echo/discovery_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::super::{DiscoveryHandler, DiscoveryResult};
use akri_shared::akri::configuration::DebugEchoDiscoveryHandlerConfig;
use anyhow::Error;
use async_trait::async_trait;
use failure::Error;
use std::{collections::HashMap, fs};

/// File acting as an environment variable for testing discovery.
Expand Down
6 changes: 3 additions & 3 deletions agent/src/protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use akri_shared::{
akri::configuration::ProtocolHandler,
os::env_var::{ActualEnvVarQuery, EnvVarQuery},
};
use anyhow::Error;
use async_trait::async_trait;
use blake2::digest::{Input, VariableOutput};
use blake2::VarBlake2b;
use failure::Error;
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -49,7 +49,7 @@ impl DiscoveryResult {
/// pub struct SampleDiscoveryHandler {}
/// #[async_trait]
/// impl DiscoveryHandler for SampleDiscoveryHandler {
/// async fn discover(&self) -> Result<Vec<DiscoveryResult>, failure::Error> {
/// async fn discover(&self) -> Result<Vec<DiscoveryResult>, anyhow::Error> {
/// Ok(Vec::new())
/// }
/// fn are_shared(&self) -> Result<bool, Error> {
Expand Down Expand Up @@ -91,7 +91,7 @@ fn inner_get_discovery_handler(
ProtocolHandler::opcua(opcua) => Ok(Box::new(opcua::OpcuaDiscoveryHandler::new(&opcua))),
ProtocolHandler::debugEcho(dbg) => match query.get_env_var("ENABLE_DEBUG_ECHO") {
Ok(_) => Ok(Box::new(debug_echo::DebugEchoDiscoveryHandler::new(dbg))),
_ => Err(failure::format_err!("No protocol configured")),
_ => Err(anyhow::format_err!("No protocol configured")),
},
config => {
panic!("No handler found for configuration {:?}", config);
Expand Down
6 changes: 3 additions & 3 deletions agent/src/protocols/onvif/discovery_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use akri_shared::onvif::device_info::{
OnvifQuery, OnvifQueryImpl, ONVIF_DEVICE_IP_ADDRESS_LABEL_ID,
ONVIF_DEVICE_MAC_ADDRESS_LABEL_ID, ONVIF_DEVICE_SERVICE_URL_LABEL_ID,
};
use anyhow::Error;
use async_trait::async_trait;
use failure::Error;
use std::{collections::HashMap, time::Duration};

/// `OnvifDiscoveryHandler` discovers the onvif instances as described by the filters `discover_handler_config.ip_addresses`,
Expand Down Expand Up @@ -53,7 +53,7 @@ impl OnvifDiscoveryHandler {
&self,
device_service_uris: Vec<String>,
onvif_query: &impl OnvifQuery,
) -> Result<Vec<DiscoveryResult>, failure::Error> {
) -> Result<Vec<DiscoveryResult>, anyhow::Error> {
let mut result = Vec::new();
for device_service_url in device_service_uris.iter() {
trace!("apply_filters - device service url {}", &device_service_url);
Expand Down Expand Up @@ -128,7 +128,7 @@ impl OnvifDiscoveryHandler {

#[async_trait]
impl DiscoveryHandler for OnvifDiscoveryHandler {
async fn discover(&self) -> Result<Vec<DiscoveryResult>, failure::Error> {
async fn discover(&self) -> Result<Vec<DiscoveryResult>, anyhow::Error> {
let onvif_query = OnvifQueryImpl {};

info!("discover - filters:{:?}", &self.discovery_handler_config,);
Expand Down
2 changes: 1 addition & 1 deletion agent/src/protocols/onvif/discovery_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub mod util {
}
}

pub async fn simple_onvif_discover(timeout: Duration) -> Result<Vec<String>, failure::Error> {
pub async fn simple_onvif_discover(timeout: Duration) -> Result<Vec<String>, anyhow::Error> {
let (mut discovery_timeout_tx, mut discovery_timeout_rx) = mpsc::channel(2);
let (mut discovery_cancel_tx, mut discovery_cancel_rx) = mpsc::channel(2);
let shared_devices = Arc::new(Mutex::new(Vec::new()));
Expand Down
2 changes: 1 addition & 1 deletion agent/src/protocols/opcua/discovery_handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::super::{DiscoveryHandler, DiscoveryResult};
use super::{discovery_impl::do_standard_discovery, OPCUA_DISCOVERY_URL_LABEL};
use akri_shared::akri::configuration::{OpcuaDiscoveryHandlerConfig, OpcuaDiscoveryMethod};
use anyhow::Error;
use async_trait::async_trait;
use failure::Error;

/// `OpcuaDiscoveryHandler` discovers the OPC UA server instances as described by the `discovery_handler_config.opcua_discovery_method`
/// and the filter `discover_handler_config.application_names`. The instances it discovers are always shared.
Expand Down
12 changes: 6 additions & 6 deletions agent/src/protocols/opcua/discovery_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ fn get_discovery_urls(
/// The Rust OPC UA implementation of FindServers does not use a timeout when connecting with a Server over TCP
/// So, an unsuccessful attempt can take over 2 minutes.
/// Therefore, this tests the connection using a timeout before calling FindServers on the DiscoveryURL.
fn test_tcp_connection(url: &str, tcp_stream: &impl TcpStream) -> Result<(), failure::Error> {
fn test_tcp_connection(url: &str, tcp_stream: &impl TcpStream) -> Result<(), anyhow::Error> {
let socket_addr = get_socket_addr(url)?;
match tcp_stream.connect_timeout(
&socket_addr,
Duration::from_secs(TCP_CONNECTION_TEST_TIMEOUT_SECS),
) {
Ok(_stream) => Ok(()),
Err(e) => Err(failure::format_err!("{:?}", e)),
Err(e) => Err(anyhow::format_err!("{:?}", e)),
}
}

Expand Down Expand Up @@ -158,18 +158,18 @@ fn get_discovery_url_from_application_description(
}

/// This returns a socket address for the OPC UA DiscoveryURL else an error if not properly formatted
fn get_socket_addr(url: &str) -> Result<SocketAddr, failure::Error> {
let url = Url::parse(&url).map_err(|_| failure::format_err!("could not parse url"))?;
fn get_socket_addr(url: &str) -> Result<SocketAddr, anyhow::Error> {
let url = Url::parse(&url).map_err(|_| anyhow::format_err!("could not parse url"))?;
if url.scheme() != OPC_TCP_SCHEME {
return Err(failure::format_err!(
return Err(anyhow::format_err!(
"format of OPC UA url {} is not valid",
url
));
}
let host = url.host_str().unwrap();
let port = url
.port()
.ok_or_else(|| failure::format_err!("provided discoveryURL is missing port"))?;
.ok_or_else(|| anyhow::format_err!("provided discoveryURL is missing port"))?;

// Convert host and port to socket address
let addr_str = format!("{}:{}", host, port);
Expand Down
2 changes: 1 addition & 1 deletion agent/src/protocols/udev/discovery_handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::super::{DiscoveryHandler, DiscoveryResult};
use super::{discovery_impl, udev_enumerator, UDEV_DEVNODE_LABEL_ID};
use akri_shared::akri::configuration::UdevDiscoveryHandlerConfig;
use anyhow::Error;
use async_trait::async_trait;
use failure::Error;
use std::collections::HashSet;

/// `UdevDiscoveryHandler` discovers udev instances by parsing the udev rules in `discovery_handler_config.udev_rules`.
Expand Down
8 changes: 4 additions & 4 deletions agent/src/protocols/udev/discovery_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct UdevFilter<'a> {
pub fn do_parse_and_find(
enumerator: impl Enumerator,
udev_rule_string: &str,
) -> Result<Vec<String>, failure::Error> {
) -> Result<Vec<String>, anyhow::Error> {
let udev_filters = parse_udev_rule(udev_rule_string)?;
let devpaths = find_devices(enumerator, udev_filters)?;
trace!(
Expand All @@ -44,7 +44,7 @@ pub fn do_parse_and_find(
/// Udev discovery is only interested in match operations ("==", "!="), so all action ("=" , "+=" , "-=" , ":=") operations
/// will be ignored.
/// Udev discovery is only interested in match fields, so all action fields, such as TEST, are ignored
fn parse_udev_rule(udev_rule_string: &str) -> Result<Vec<UdevFilter>, failure::Error> {
fn parse_udev_rule(udev_rule_string: &str) -> Result<Vec<UdevFilter>, anyhow::Error> {
info!(
"parse_udev_rule - enter for udev rule string {}",
udev_rule_string
Expand All @@ -69,7 +69,7 @@ fn parse_udev_rule(udev_rule_string: &str) -> Result<Vec<UdevFilter>, failure::E
let field_pair = inner_rules.next().unwrap();
let inner_field = field_pair.into_inner().next().unwrap();
if inner_field.as_rule() == Rule::unsupported_field {
return Err(failure::format_err!(
return Err(anyhow::format_err!(
"parse_udev_rule - unsupported field {}",
inner_field.into_inner().next().unwrap().as_str()
));
Expand All @@ -91,7 +91,7 @@ fn parse_udev_rule(udev_rule_string: &str) -> Result<Vec<UdevFilter>, failure::E
value: value.to_string(),
});
} else {
return Err(failure::format_err!("parse_udev_rule - unsupported action operation for rule with field [{}], operation [{:?}], and value[{}]",
return Err(anyhow::format_err!("parse_udev_rule - unsupported action operation for rule with field [{}], operation [{:?}], and value[{}]",
inner_field.into_inner().as_str(), operation, value));
}
}
Expand Down

0 comments on commit c706128

Please sign in to comment.