Skip to content
This repository has been archived by the owner on Dec 19, 2021. It is now read-only.

Commit

Permalink
Use anyhow instead of failure
Browse files Browse the repository at this point in the history
- update dependencies
  • Loading branch information
ChriFo committed Sep 6, 2020
1 parent 2641349 commit 2f6b092
Show file tree
Hide file tree
Showing 9 changed files with 752 additions and 634 deletions.
1,290 changes: 709 additions & 581 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ crate-type = ["cdylib"]

[dependencies]
antidote = "1.0.0"
chrono = "0.4.13"
data-encoding = "2.2.1"
failure = "0.1.8"
anyhow = "1.0.32"
chrono = "0.4.15"
data-encoding = "2.3.0"
fern = "0.6.0"
lazy_static = "1.4.0"
log = "0.4.8"
serde = "1.0.114"
serde_derive = "1.0.114"
serde_json = "1.0.56"
ureq = { version = "1.3.0", features = ["json"] }
log = "0.4.11"
serde = "1.0.115"
serde_derive = "1.0.115"
serde_json = "1.0.57"
ureq = { version = "1.4.0", features = ["json"] }
url = "2.1.1"

[dependencies.config]
Expand All @@ -37,10 +37,10 @@ const-cstr = "0.3.0"
dlopen = "0.1.8"
futures-timer = "3.0.2"
rand = "0.7.3"
serial_test = "0.4.0"
serial_test = "0.5.0"
spectral = "0.6.0"
tempfile = "3.1.0"
test-server = { git = "https://github.com/ChriFo/test-server-rs", tag = "0.8.0" }
test-server = { git = "https://github.com/ChriFo/test-server-rs", tag = "0.9.0" }

[target.'cfg(windows)'.build-dependencies]
winres = "0.1.11"
Expand Down
16 changes: 7 additions & 9 deletions src/ctapi/close.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::ctapi::MAP;
use crate::{http, Status, CONFIG};
use failure::Error;

pub fn close(mut ctn: u16) -> Result<Status, Error> {
pub fn close(mut ctn: u16) -> anyhow::Result<Status> {
if let Some(ctn_from_cfg) = CONFIG.read().ctn {
debug!("Use ctn '{}' from configuration", ctn_from_cfg);
ctn = ctn_from_cfg;
Expand Down Expand Up @@ -45,7 +44,6 @@ mod tests {
use super::close;
use crate::ctapi::MAP;
use crate::{Settings, Status, CONFIG};
use failure::Error;
use std::collections::HashMap;
use std::env;
use test_server::{self, HttpResponse};
Expand Down Expand Up @@ -75,7 +73,7 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_in_request_path() -> Result<(), Error> {
fn use_ctn_and_pn_in_request_path() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -96,7 +94,7 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_from_config() -> Result<(), Error> {
fn use_ctn_and_pn_from_config() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
let ctn = rand::random::<u16>();
Expand All @@ -123,7 +121,7 @@ mod tests {

#[test]
#[serial]
fn returns_err_htsi_if_server_response_is_not_200() -> Result<(), Error> {
fn returns_err_htsi_if_server_response_is_not_200() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -142,7 +140,7 @@ mod tests {

#[test]
#[serial]
fn returns_err_htsi_if_server_response_not_contains_status() -> Result<(), Error> {
fn returns_err_htsi_if_server_response_not_contains_status() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("hello world"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -161,7 +159,7 @@ mod tests {

#[test]
#[serial]
fn returns_response_status_from_server() -> Result<(), Error> {
fn returns_response_status_from_server() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("-11"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -180,7 +178,7 @@ mod tests {

#[test]
#[serial]
fn return_ok_and_close_ctn_if_server_returns_ok() -> Result<(), Error> {
fn return_ok_and_close_ctn_if_server_returns_ok() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("0"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand Down
18 changes: 8 additions & 10 deletions src/ctapi/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::ctapi::MAP;
use crate::{http, Status, CONFIG};
use data_encoding::{BASE64, HEXLOWER};
use failure::Error;
use std::slice;

#[allow(non_snake_case)]
Expand All @@ -24,7 +23,7 @@ pub fn data(
command: *const u8,
lenr: *mut u16,
response: *mut u8,
) -> Result<Status, Error> {
) -> anyhow::Result<Status> {
if let Some(ctn_from_cfg) = CONFIG.read().ctn {
debug!("Use ctn '{}' from configuration", ctn_from_cfg);
ctn = ctn_from_cfg;
Expand Down Expand Up @@ -106,7 +105,6 @@ mod tests {
use crate::ctapi::MAP;
use crate::{Settings, Status, CONFIG};
use data_encoding::BASE64;
use failure::Error;
use serde_json::{self, Value};
use std::collections::HashMap;
use std::env;
Expand Down Expand Up @@ -159,7 +157,7 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_in_request_path() -> Result<(), Error> {
fn use_ctn_and_pn_in_request_path() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -179,7 +177,7 @@ mod tests {

#[test]
#[serial]
fn post_body_contains_parameter() -> Result<(), Error> {
fn post_body_contains_parameter() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand Down Expand Up @@ -217,7 +215,7 @@ mod tests {

#[test]
#[serial]
fn response_is_mapped_to_parameter() -> Result<(), Error> {
fn response_is_mapped_to_parameter() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || {
HttpResponse::Ok()
.body(r#"{"dad":39,"sad":63,"lenr":2,"response":"kAA=","responseCode":0}"#)
Expand All @@ -244,7 +242,7 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_is_not_200() -> Result<(), Error> {
fn returns_err_if_server_response_is_not_200() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -261,7 +259,7 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_not_contains_response_struct_as_json() -> Result<(), Error> {
fn returns_err_if_server_response_not_contains_response_struct_as_json() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("hello world"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -278,7 +276,7 @@ mod tests {

#[test]
#[serial]
fn returns_response_status_from_valid_json_response_struct() -> Result<(), Error> {
fn returns_response_status_from_valid_json_response_struct() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || {
HttpResponse::Ok()
.body(r#"{"dad":1,"sad":1,"lenr":1,"response":"a=","responseCode":-11}"#)
Expand All @@ -301,7 +299,7 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_from_config() -> Result<(), Error> {
fn use_ctn_and_pn_from_config() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());

Expand Down
16 changes: 7 additions & 9 deletions src/ctapi/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::ctapi::MAP;
use crate::{http, Status, CONFIG};
use failure::Error;

pub fn init(mut ctn: u16, mut pn: u16) -> Result<Status, Error> {
pub fn init(mut ctn: u16, mut pn: u16) -> anyhow::Result<Status> {
if let (Some(ctn_from_cfg), Some(pn_from_cfg)) = (CONFIG.read().ctn, CONFIG.read().pn) {
debug!(
"Use ctn '{}' and pn '{}' from configuration.",
Expand Down Expand Up @@ -45,7 +44,6 @@ mod tests {
use super::init;
use crate::ctapi::MAP;
use crate::{Settings, Status, CONFIG};
use failure::Error;
use std::collections::HashMap;
use std::env;
use test_server::{self, HttpResponse};
Expand Down Expand Up @@ -79,7 +77,7 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_in_request_path() -> Result<(), Error> {
fn use_ctn_and_pn_in_request_path() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -99,7 +97,7 @@ mod tests {

#[test]
#[serial]
fn use_ctn_and_pn_from_config() -> Result<(), Error> {
fn use_ctn_and_pn_from_config() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
let ctn = rand::random::<u16>();
Expand All @@ -125,7 +123,7 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_is_not_200() -> Result<(), Error> {
fn returns_err_if_server_response_is_not_200() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -143,7 +141,7 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_not_contains_status() -> Result<(), Error> {
fn returns_err_if_server_response_not_contains_status() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("hello world"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -161,7 +159,7 @@ mod tests {

#[test]
#[serial]
fn returns_response_status_from_server() -> Result<(), Error> {
fn returns_response_status_from_server() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("-11"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand All @@ -178,7 +176,7 @@ mod tests {

#[test]
#[serial]
fn returns_ok_and_init_ctn_if_server_returns_ok() -> Result<(), Error> {
fn returns_ok_and_init_ctn_if_server_returns_ok() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || HttpResponse::Ok().body("0"))?;
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();
Expand Down
14 changes: 6 additions & 8 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::CONFIG;
use failure::Error;
use serde_json::Value;

pub fn request(path: &str, request_body: Option<Value>) -> Result<String, Error> {
pub fn request(path: &str, request_body: Option<Value>) -> anyhow::Result<String> {
let url = format!("{}{}", CONFIG.read().base_url, path);
debug!("Request URL: {}", url);
let mut request = ureq::post(&url);
Expand All @@ -27,7 +26,7 @@ pub fn request(path: &str, request_body: Option<Value>) -> Result<String, Error>
};

if response.ok() {
response.into_string().map_err(Error::from)
response.into_string().map_err(anyhow::Error::from)
} else {
Err(format_err!(
"Request failed with status code {}",
Expand All @@ -41,13 +40,12 @@ mod tests {

use super::request;
use crate::{Settings, CONFIG};
use failure::Error;
use std::{env, time::Duration};
use test_server::{self, helper, HttpResponse};

#[test]
#[serial]
fn request_with_body_is_content_type_json() -> Result<(), Error> {
fn request_with_body_is_content_type_json() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config();
Expand All @@ -67,7 +65,7 @@ mod tests {

#[test]
#[serial]
fn send_request_body_if_given() -> Result<(), Error> {
fn send_request_body_if_given() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config();
Expand All @@ -86,7 +84,7 @@ mod tests {

#[test]
#[serial]
fn if_no_json_is_given_send_empty_request_body() -> Result<(), Error> {
fn if_no_json_is_given_send_empty_request_body() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", HttpResponse::BadRequest)?;
env::set_var("K2_BASE_URL", server.url());
init_config();
Expand All @@ -103,7 +101,7 @@ mod tests {

#[test]
#[serial]
fn http_is_using_timeout_from_config() -> Result<(), Error> {
fn http_is_using_timeout_from_config() -> anyhow::Result<()> {
let server = test_server::new("127.0.0.1:0", || async {
futures_timer::Delay::new(Duration::from_secs(5)).await;
HttpResponse::Ok().body("foobar")
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![warn(unused_imports)]

#[macro_use]
extern crate failure;
extern crate anyhow;
#[macro_use]
extern crate log;
#[macro_use]
Expand Down
7 changes: 3 additions & 4 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use config::{Config, Environment, File};
use failure::Error;
use std::path::{Path, MAIN_SEPARATOR};
use url::Url;

Expand All @@ -20,7 +19,7 @@ pub struct Settings {
}

impl Settings {
pub fn init() -> Result<Self, Error> {
pub fn init() -> anyhow::Result<Self> {
let mut settings = Config::new();

// set defaults
Expand Down Expand Up @@ -50,7 +49,7 @@ impl Settings {
// force trailing slash for log_path
if let Ok(Some(mut path)) = settings.get::<Option<String>>("log_path") {
if !Path::new(&path).exists() {
return Err(format_err!("log_path does not exists"));
bail!("log_path does not exists");
}

if !path.trim().ends_with(MAIN_SEPARATOR) {
Expand All @@ -70,7 +69,7 @@ impl Settings {
let _ = settings.set("pn", None::<String>);
}

settings.try_into().map_err(failure::Error::from)
settings.try_into().map_err(anyhow::Error::from)
}
}

Expand Down
3 changes: 1 addition & 2 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
extern crate const_cstr;

use dlopen::raw::Library;
use failure::Error;
use std::u16::MAX;
use std::{env, str};
use test_server::{HttpRequest, HttpResponse};
Expand All @@ -15,7 +14,7 @@ const LIB_PATH: &str = "./target/debug/libctehxk2.so";
const LIB_PATH: &str = "./target/debug/libctehxk2.dylib";

#[test]
fn use_ct_api_functions() -> Result<(), Error> {
fn use_ct_api_functions() -> anyhow::Result<()> {
let lib = Library::open(LIB_PATH)?;

let init: unsafe extern "system" fn(u16, u16) -> i8 =
Expand Down

0 comments on commit 2f6b092

Please sign in to comment.