use crate::{Configuration, ConfigurationProvider, LoadError};
use std::fmt::{Debug, Formatter, Result as FormatResult};
use std::{borrow::Borrow, ops::Deref};
#[derive(PartialEq, Clone)]
pub enum ReloadError {
Provider(Vec<(String, LoadError)>),
Borrowed(Option<usize>),
}
impl Debug for ReloadError {
fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult {
match self {
Self::Provider(errors) => {
if errors.len() == 1 {
write!(f, "{} ({})", errors[0].1.message(), &errors[0].0)?;
} else {
f.write_str("One or more load errors occurred:")?;
for (i, (provider, error)) in errors.iter().enumerate() {
write!(f, "\n [{}]: {} ({})", (i+1), error.message(), provider)?;
}
}
}
Self::Borrowed(count) => {
write!(f, "Reload failed because the are")?;
if let Some(value) = count {
write!(f, "{} ", value)?;
}
write!(f, " outstanding borrow references.")?;
}
}
Ok(())
}
}
pub type ReloadResult = std::result::Result<(), ReloadError>;
pub trait ConfigurationRoot:
Configuration
+ AsRef<dyn Configuration>
+ Borrow<dyn Configuration>
+ Deref<Target = dyn Configuration>
+ Debug
{
fn reload(&mut self) -> ReloadResult;
fn providers(&self) -> Box<dyn ConfigurationProviderIterator + '_>;
fn as_config(&self) -> Box<dyn Configuration>;
}
pub trait ConfigurationProviderIterator<'a>:
Iterator<Item = Box<dyn ConfigurationProvider + 'a>>
+ ExactSizeIterator<Item = Box<dyn ConfigurationProvider + 'a>>
+ DoubleEndedIterator<Item = Box<dyn ConfigurationProvider + 'a>>
{
}