From 353937520f5a7a9fe338c68b4a419a7495a833f4 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 23 Feb 2021 23:36:33 +0300 Subject: [PATCH] refac: made error simple ... and remove failure package --- itconfig-tests/tests/get_env.rs | 4 ++-- itconfig/Cargo.toml | 3 +-- itconfig/src/enverr.rs | 9 --------- itconfig/src/error.rs | 23 +++++++++++++++++++++++ itconfig/src/getenv.rs | 10 ++++------ itconfig/src/lib.rs | 11 +++++------ 6 files changed, 35 insertions(+), 25 deletions(-) delete mode 100644 itconfig/src/enverr.rs create mode 100644 itconfig/src/error.rs diff --git a/itconfig-tests/tests/get_env.rs b/itconfig-tests/tests/get_env.rs index b63b5e9..bf99979 100644 --- a/itconfig-tests/tests/get_env.rs +++ b/itconfig-tests/tests/get_env.rs @@ -20,7 +20,7 @@ fn get_env_with_invalid_value() { fn get_result_of_missing_env() { let env_name = String::from("TEST_CASE_3"); let env_val = get_env::(&env_name); - assert_eq!(env_val, Err(MissingVariable { env_name })) + assert_eq!(env_val, Err(MissingVariable(env_name))) } #[test] @@ -28,7 +28,7 @@ fn get_result_of_env_with_invalid_value() { let env_name = String::from("TEST_CASE_4"); env::set_var(&env_name, "30r"); let env_val = get_env::(&env_name); - assert_eq!(env_val, Err(FailedToParse { env_name })) + assert_eq!(env_val, Err(FailedToParse(env_name))) } #[test] diff --git a/itconfig/Cargo.toml b/itconfig/Cargo.toml index 59d4e81..8efde64 100644 --- a/itconfig/Cargo.toml +++ b/itconfig/Cargo.toml @@ -15,8 +15,7 @@ readme = "README.md" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -failure = { version = "0.1.7", features = ["derive"]} -serde_json = { version = "1.0.44", optional = true } +serde_json = { version = "1", optional = true } itconfig-macro = { version = "1.0", path = "../itconfig-macro", optional = true } [dev-dependencies] diff --git a/itconfig/src/enverr.rs b/itconfig/src/enverr.rs deleted file mode 100644 index 9072b3e..0000000 --- a/itconfig/src/enverr.rs +++ /dev/null @@ -1,9 +0,0 @@ -use failure::Fail; - -#[derive(Debug, Fail, PartialEq)] -pub enum EnvError { - #[fail(display = r#"Environment variable "{}" is missing"#, env_name)] - MissingVariable { env_name: String }, - #[fail(display = r#"Failed to parse environment variable "{}""#, env_name)] - FailedToParse { env_name: String }, -} diff --git a/itconfig/src/error.rs b/itconfig/src/error.rs new file mode 100644 index 0000000..2b0c92a --- /dev/null +++ b/itconfig/src/error.rs @@ -0,0 +1,23 @@ +use std::error; +use std::fmt; + +#[derive(Debug, PartialEq)] +pub enum EnvError { + MissingVariable(String), + FailedToParse(String), +} + +impl fmt::Display for EnvError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self { + EnvError::MissingVariable(env_name) => { + write!(f, r#"Environment variable "{}" is missing"#, env_name) + } + EnvError::FailedToParse(env_name) => { + write!(f, r#"Failed to parse environment variable "{}""#, env_name) + } + } + } +} + +impl error::Error for EnvError {} diff --git a/itconfig/src/getenv.rs b/itconfig/src/getenv.rs index d640251..a3240e5 100644 --- a/itconfig/src/getenv.rs +++ b/itconfig/src/getenv.rs @@ -40,9 +40,7 @@ where T: FromEnvString, { get_env_or(env_name, |_| { - Err(EnvError::MissingVariable { - env_name: env_name.to_string(), - }) + Err(EnvError::MissingVariable(env_name.to_string())) }) } @@ -130,9 +128,9 @@ fn parse_env_variable(env_name: &str, env_str: EnvString) -> Result().map_err(|_| EnvError::FailedToParse { - env_name: env_name.to_string(), - }) + env_str + .parse::() + .map_err(|_| EnvError::FailedToParse(env_name.to_string())) } #[doc(hidden)] diff --git a/itconfig/src/lib.rs b/itconfig/src/lib.rs index 135b7e6..bee8ba7 100644 --- a/itconfig/src/lib.rs +++ b/itconfig/src/lib.rs @@ -144,22 +144,21 @@ unused_imports, unused_qualifications )] +#![deny(clippy::all)] +#![allow(clippy::needless_doctest_main)] ///////////////////////////////////////////////////////////////////////////// -#[macro_use] -extern crate failure; - -mod enverr; pub mod envstr; +mod error; mod getenv; -pub use self::enverr::*; +pub use self::error::*; pub use self::getenv::*; pub mod prelude { - pub use crate::enverr::*; pub use crate::envstr::*; + pub use crate::error::*; } #[cfg(feature = "macro")]