use {
mollusk_svm::result::Compare,
serde::{Deserialize, Serialize},
};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigFile {
pub checks: Vec<Compare>,
}
impl ConfigFile {
fn load_json(path: &str) -> Result<Self, String> {
let file = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
serde_json::from_str(&file).map_err(|e| e.to_string())
}
fn load_yaml(path: &str) -> Result<Self, String> {
let file = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
serde_yaml::from_str(&file).map_err(|e| e.to_string())
}
pub fn try_load(path: &str) -> Result<ConfigFile, Box<dyn std::error::Error>> {
let ext = std::path::Path::new(path)
.extension()
.unwrap()
.to_str()
.unwrap();
match ext {
"json" => Self::load_json(path).map_err(|e| e.into()),
"yaml" => Self::load_yaml(path).map_err(|e| e.into()),
_ => Err(format!("Unsupported config file format: {}", ext).into()),
}
}
}