Skip to content

Commit

Permalink
remove failure dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeyR committed May 14, 2019
1 parent b8d1ca7 commit cfed2b8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
7 changes: 0 additions & 7 deletions dotenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ lazy_static = "1.0.0"
regex = "1.0"
clap = { version = "2", optional = true }

[dependencies.failure]
version = "0.1.1"
features = ["derive"]
default-features = false

[dev-dependencies]
tempfile = "3.0.0"

[features]
backtrace = ["failure/backtrace"]
cli = ["clap"]
default = ["backtrace"]
103 changes: 95 additions & 8 deletions dotenv/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::io;
use failure::Fail;
use std::fmt;
use std::error;

#[derive(Debug, Fail)]
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
#[fail(display = "Error parsing line: '{}'", _0)]
LineParse(String),
#[fail(display = "{}", _0)]
Io(#[cfg_attr(backtrace, cause)] ::std::io::Error),
#[fail(display = "{}", _0)]
EnvVar(#[cfg_attr(backtrace, cause)] ::std::env::VarError),
Io(io::Error),
EnvVar(std::env::VarError),
#[doc(hidden)]
__Nonexhaustive
}

impl Error {
Expand All @@ -20,4 +22,89 @@ impl Error {
}
}

pub type Result<T> = ::std::result::Result<T, Error>;
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
Error::EnvVar(err) => Some(err),
_ => None,
}
}
}

impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(err) => write!(fmt, "{}", err),
Error::EnvVar(err) => write!(fmt, "{}", err),
Error::LineParse(line) => write!(fmt, "Error parsing line: '{}'", line),
_ => unreachable!(),
}
}
}

#[cfg(test)]
mod test {
use std::error::Error as StdError;

use super::*;

#[test]
fn test_io_error_source() {
let err = Error::Io(std::io::ErrorKind::PermissionDenied.into());
let io_err = err.source().unwrap().downcast_ref::<std::io::Error>().unwrap();
assert_eq!(std::io::ErrorKind::PermissionDenied, io_err.kind());
}

#[test]
fn test_envvar_error_source() {
let err = Error::EnvVar(std::env::VarError::NotPresent);
let var_err = err.source().unwrap().downcast_ref::<std::env::VarError>().unwrap();
assert_eq!(&std::env::VarError::NotPresent, var_err);
}

#[test]
fn test_lineparse_error_source() {
let err = Error::LineParse("test line".to_string());
assert!(err.source().is_none());
}

#[test]
fn test_error_not_found_true() {
let err = Error::Io(std::io::ErrorKind::NotFound.into());
assert!(err.not_found());
}

#[test]
fn test_error_not_found_false() {
let err = Error::Io(std::io::ErrorKind::PermissionDenied.into());
assert!(!err.not_found());
}

#[test]
fn test_io_error_display() {
let err = Error::Io(std::io::ErrorKind::PermissionDenied.into());
let io_err: std::io::Error = std::io::ErrorKind::PermissionDenied.into();

let err_desc = format!("{}", err);
let io_err_desc = format!("{}", io_err);
assert_eq!(io_err_desc, err_desc);
}

#[test]
fn test_envvar_error_display() {
let err = Error::EnvVar(std::env::VarError::NotPresent);
let var_err = std::env::VarError::NotPresent;

let err_desc = format!("{}", err);
let var_err_desc = format!("{}", var_err);
assert_eq!(var_err_desc, err_desc);
}

#[test]
fn test_lineparse_error_display() {
let err = Error::LineParse("test line".to_string());
let err_desc = format!("{}", err);
assert_eq!("Error parsing line: 'test line'", err_desc);
}
}

0 comments on commit cfed2b8

Please sign in to comment.