|
@ -79,13 +79,15 @@ fn get_default_config() -> Config { |
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
#[derive(Debug)]
|
|
|
pub struct ConfigError {
|
|
|
pub struct ConfigError {
|
|
|
description: String |
|
|
|
|
|
|
|
|
description: String,
|
|
|
|
|
|
original_error: Option<Box<Error>>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl ConfigError {
|
|
|
impl ConfigError {
|
|
|
fn new(description: &str) -> ConfigError {
|
|
|
|
|
|
|
|
|
fn new(description: &str, original_error: Option<Box<Error>>) -> ConfigError {
|
|
|
ConfigError {
|
|
|
ConfigError {
|
|
|
description: String::from(description)
|
|
|
|
|
|
|
|
|
description: String::from(description),
|
|
|
|
|
|
original_error
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@ -94,15 +96,19 @@ impl Error for ConfigError {} |
|
|
|
|
|
|
|
|
impl fmt::Display for ConfigError {
|
|
|
impl fmt::Display for ConfigError {
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
write!(f, "Configuration Error: {}", self.description)
|
|
|
|
|
|
|
|
|
match &self.original_error {
|
|
|
|
|
|
Some(original_error) => {
|
|
|
|
|
|
write!(f, "{}: \"{}\"", self.description, original_error)
|
|
|
|
|
|
},
|
|
|
|
|
|
None => write!(f, "{}", self.description)
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn read_config(yaml_str: &str) -> Result<Config, ConfigError> {
|
|
|
fn read_config(yaml_str: &str) -> Result<Config, ConfigError> {
|
|
|
match serde_yaml::from_str(yaml_str) {
|
|
|
match serde_yaml::from_str(yaml_str) {
|
|
|
Ok(v) => Result::Ok(v),
|
|
|
Ok(v) => Result::Ok(v),
|
|
|
// File wasn't valid Config/YAML
|
|
|
|
|
|
Err(_e) => Result::Err(ConfigError::new("Configuration file was invalid"))
|
|
|
|
|
|
|
|
|
Err(e) => Result::Err(ConfigError::new("Invalid Configuration", Option::Some(Box::from(e))))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@ -115,15 +121,12 @@ pub fn load_config(path: &str) -> Result<Config, ConfigError> { |
|
|
if c > 0 {
|
|
|
if c > 0 {
|
|
|
read_config(&contents)
|
|
|
read_config(&contents)
|
|
|
} else {
|
|
|
} else {
|
|
|
// File was empty
|
|
|
|
|
|
Result::Err(ConfigError::new("Configuration file was empty"))
|
|
|
|
|
|
|
|
|
Result::Err(ConfigError::new("Empty Configuration File", Option::None))
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
// File couldn't be read
|
|
|
|
|
|
Err(_e) => Result::Err(ConfigError::new("Configuration file could not be read"))
|
|
|
|
|
|
|
|
|
Err(e) => Result::Err(ConfigError::new("Cannot Read Configuration File", Option::Some(Box::from(e))))
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
// File Doesn't exist
|
|
|
|
|
|
Err(_e) => Result::Err(ConfigError::new(&format!("Configuration file doesn't exist \"{}\"", path)))
|
|
|
|
|
|
|
|
|
Err(e) => Result::Err(ConfigError::new(&format!("Configuration File Doesn't Exist \"{}\"", path), Option::Some(Box::from(e))))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|