use crate::error::ConfigError;
use crate::validate::Validate;
use serde::de::DeserializeOwned;
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(e.to_string()).with_kind("TOML parse error"))
}
fn from_json_str(s: &str) -> Result<Self, ConfigError>
where
Self: DeserializeOwned,
{
serde_json::from_str(s)
.map_err(|e| ConfigError::new(e.to_string()).with_kind("JSON parse error"))
}
fn validate_and_build(self) -> Result<Self, ConfigError> {
self.validate()?;
Ok(self)
}
}
impl<T> Config for T where T: Validate + DeserializeOwned {}