use crate::error::ConfigError;
use crate::validate::Validate;
use serde::de::DeserializeOwned;
use serde_json;
use toml;
pub trait Config: Validate + Sized {
fn from_toml_str(s: &str) -> Result<Self, ConfigError>
where
Self: DeserializeOwned,
{
toml::from_str(s).map_err(|e| {
ConfigError::new("Failed to parse TOML")
.with_kind("TOML parse error")
.with_source(e)
})
}
fn from_json_str(s: &str) -> Result<Self, ConfigError>
where
Self: DeserializeOwned,
{
serde_json::from_str(s).map_err(|e| {
ConfigError::new("Failed to parse JSON")
.with_kind("JSON parse error")
.with_source(e)
})
}
fn validate_and_build(self) -> Result<Self, ConfigError> {
self.validate()?;
Ok(self)
}
}
impl<T> Config for T where T: Validate + DeserializeOwned {}