use serde::de::DeserializeOwned;
#[derive(Debug, Clone)]
pub struct Config<T: Clone>(pub T);
impl<T: DeserializeOwned + Clone> Config<T> {
pub fn from_env() -> Result<Self, ConfigError> {
let config: T = envy::from_env::<T>().map_err(|e| ConfigError(e.to_string()))?;
Ok(Config(config))
}
pub fn from_env_prefixed(prefix: &str) -> Result<Self, ConfigError> {
let config: T = envy::prefixed(prefix)
.from_env::<T>()
.map_err(|e| ConfigError(e.to_string()))?;
Ok(Config(config))
}
pub fn new(config: T) -> Self {
Config(config)
}
pub fn inner(&self) -> &T {
&self.0
}
pub fn into_inner(self) -> T {
self.0
}
}
#[derive(Debug, Clone)]
pub struct ConfigError(pub String);
impl std::fmt::Display for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "configuration error: {}", self.0)
}
}
impl std::error::Error for ConfigError {}