use std::fmt;
use crate::descriptor::ConfigValueDescriptor;
#[cfg_attr(feature = "nightly", doc(notable_trait))]
pub trait Layer {
type Output;
type Error;
fn try_get(&self) -> Result<Self::Output, Self::Error>;
fn get(&self) -> Self::Output
where
Self: ConfigValueDescriptor,
Self::Error: fmt::Display,
{
self.try_get().unwrap_or_else(|e| {
let val_config = <Self as ConfigValueDescriptor>::get_descriptor(self);
panic!(
"couldn't get env var `{}` (expected type `{}`): {e}",
val_config.var_name,
std::any::type_name::<Self::Output>(),
);
})
}
}
impl<T: Layer> Layer for &T {
type Output = <T as Layer>::Output;
type Error = <T as Layer>::Error;
fn try_get(&self) -> Result<Self::Output, Self::Error> {
<T as Layer>::try_get(self)
}
}