refac: made error simple
... and remove failure package
This commit is contained in:
parent
356cf13ec6
commit
353937520f
6 changed files with 35 additions and 25 deletions
|
@ -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::<String>(&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::<u32>(&env_name);
|
||||
assert_eq!(env_val, Err(FailedToParse { env_name }))
|
||||
assert_eq!(env_val, Err(FailedToParse(env_name)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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 },
|
||||
}
|
23
itconfig/src/error.rs
Normal file
23
itconfig/src/error.rs
Normal file
|
@ -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 {}
|
|
@ -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<T>(env_name: &str, env_str: EnvString) -> Result<T, EnvErr
|
|||
where
|
||||
T: FromEnvString,
|
||||
{
|
||||
env_str.parse::<T>().map_err(|_| EnvError::FailedToParse {
|
||||
env_name: env_name.to_string(),
|
||||
})
|
||||
env_str
|
||||
.parse::<T>()
|
||||
.map_err(|_| EnvError::FailedToParse(env_name.to_string()))
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
|
|
|
@ -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")]
|
||||
|
|
Reference in a new issue