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 7, 2020
1 parent 2641349 commit e033765
Show file tree
Hide file tree
Showing 12 changed files with 914 additions and 647 deletions.
1,292 changes: 710 additions & 582 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 11 additions & 11 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"
once_cell = "1.4.1"
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
40 changes: 30 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,29 @@ mod tests {

#[test]
#[serial]
fn returns_err_if_server_response_is_not_200() -> Result<(), Error> {
#[should_panic(expected = "Failed to extract response.")]
fn response_with_failure_response_field() {
let server = test_server::new("127.0.0.1:0", || {
HttpResponse::Ok()
.body(r#"{"dad":39,"sad":63,"lenr":2,"response":"0123456789","responseCode":0}"#)
})
.unwrap();
env::set_var("K2_BASE_URL", server.url());
init_config_clear_map();

let (_, command, lenc, response, mut lenr, mut dad, mut sad, ctn, pn) = rand_params();
let _ = MAP.write().insert(ctn, pn);

let res = data(ctn, &mut dad, &mut sad, lenc, command, &mut lenr, response);

env::remove_var("K2_BASE_URL");

let _status = res.unwrap();
}

#[test]
#[serial]
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 +281,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 +298,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 +321,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
5 changes: 2 additions & 3 deletions src/ctapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub mod init;
pub mod status;

use antidote::RwLock;
use once_cell::sync::Lazy;
use std::collections::HashMap;

lazy_static::lazy_static! {
static ref MAP: RwLock<HashMap<u16, u16>> = RwLock::new(HashMap::new());
}
static MAP: Lazy<RwLock<HashMap<u16, u16>>> = Lazy::new(|| RwLock::new(HashMap::new()));
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
9 changes: 4 additions & 5 deletions 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 All @@ -28,12 +28,11 @@ use crate::ctapi::init::init;
use crate::ctapi::status::Status;
use crate::settings::Settings;
use antidote::RwLock;
use once_cell::sync::Lazy;
use std::panic;

lazy_static::lazy_static! {
pub(crate) static ref CONFIG: RwLock<Settings> =
RwLock::new(Settings::init().expect("Failed to init configuration!"));
}
static CONFIG: Lazy<RwLock<Settings>> =
Lazy::new(|| RwLock::new(Settings::init().expect("Failed to init configuration!")));

#[no_mangle]
pub extern "system" fn CT_init(ctn: u16, pn: u16) -> i8 {
Expand Down
Loading

0 comments on commit e033765

Please sign in to comment.