use std::env::VarError;
use std::error;
use std::ffi::OsString;
use std::fmt;
use estring::EString;
#[derive(Debug)]
pub struct Error(pub(crate) String, pub(crate) Reason);
impl Error {
pub fn var_name(&self) -> &str {
&self.0
}
pub fn reason(&self) -> &Reason {
&self.1
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"The environment variable '{}' failed: {}",
self.0, self.1
)
}
}
#[derive(Debug, Clone)]
pub enum Reason {
NotPresent,
Parse(EString),
Invalid(OsString),
}
impl fmt::Display for Reason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Reason::NotPresent => f.write_str("The specified env variable was not present"),
Reason::Invalid(inner) => write!(
f,
"The specified env variable was found, but it did not valid: '{:?}'",
inner,
),
Reason::Parse(env_name) => {
write!(f, r#"Failed to parse environment variable "{}""#, env_name)
}
}
}
}
impl error::Error for Error {}
impl From<VarError> for Reason {
fn from(err: VarError) -> Self {
match err {
VarError::NotPresent => Reason::NotPresent,
VarError::NotUnicode(inner) => Reason::Invalid(inner),
}
}
}
impl From<estring::Error> for Reason {
fn from(err: estring::Error) -> Self {
Reason::Parse(err.0)
}
}