#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
use std::path::PathBuf;
use crate::{Config, Error, Layer};
#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
use crate::File;
pub struct Builder<C: Config> {
sources: Vec<Source<C>>,
}
impl<C: Config> Builder<C> {
pub(crate) fn new() -> Self {
Self { sources: vec![] }
}
#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
pub fn file(mut self, path: impl Into<PathBuf>) -> Self {
self.sources.push(Source::File(path.into()));
self
}
pub fn env(mut self) -> Self {
self.sources.push(Source::Env);
self
}
pub fn preloaded(mut self, layer: C::Layer) -> Self {
self.sources.push(Source::Preloaded(layer));
self
}
pub fn load(self) -> Result<C, Error> {
let mut partial = C::Layer::empty();
for source in self.sources {
let layer = match source {
#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
Source::File(path) => File::new(path)?.load()?,
Source::Env => C::Layer::from_env()?,
Source::Preloaded(p) => p,
};
partial = partial.with_fallback(layer);
}
C::from_layer(partial.with_fallback(C::Layer::default_values()))
}
}
enum Source<C: Config> {
#[cfg(any(feature = "toml", feature = "yaml", feature = "json5"))]
File(PathBuf),
Env,
Preloaded(C::Layer),
}