You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.1 KiB
33 lines
1.1 KiB
use std::fs::File;
|
|
use std::io::prelude::*;
|
|
|
|
use serde_yaml;
|
|
|
|
use crate::config::error::ConfigError;
|
|
use crate::config::model::Config;
|
|
|
|
fn read_config(yaml_str: &str) -> Result<Config, ConfigError> {
|
|
match serde_yaml::from_str(yaml_str) {
|
|
Ok(v) => Result::Ok(v),
|
|
Err(e) => Result::Err(ConfigError::new("Invalid Configuration", Option::Some(Box::from(e))))
|
|
}
|
|
}
|
|
|
|
pub fn read(path: &str) -> Result<Config, ConfigError> {
|
|
match File::open(path) {
|
|
Ok(mut file) => {
|
|
let mut contents = String::new();
|
|
match file.read_to_string(&mut contents) {
|
|
Ok(c) => {
|
|
if c > 0 {
|
|
read_config(&contents)
|
|
} else {
|
|
Result::Err(ConfigError::new("Empty Configuration File", Option::None))
|
|
}
|
|
}
|
|
Err(e) => Result::Err(ConfigError::new("Cannot Read Configuration File", Option::Some(Box::from(e))))
|
|
}
|
|
}
|
|
Err(_e) => Result::Err(ConfigError::new(&format!("Configuration File Doesn't Exist \"{}\"", path), Option::None))
|
|
}
|
|
}
|