mod data;
mod declaration;
mod env;
mod explain;
mod files;
mod handle;
mod hub;
mod layers;
mod marks;
#[cfg(not(feature = "config"))]
mod no_store;
mod resolve;
mod rules;
#[cfg(feature = "config")]
mod store;
use std::fmt;
use serde::Serialize;
use serde::de::DeserializeOwned;
pub use declaration::Declaration;
pub use env::Env;
pub use explain::{Explained, Given, Line, Origin};
pub use files::File;
pub use handle::{Config, IfVersion, Snapshot, Watching};
use layers::Layer;
pub use marks::{Entries, Live, Required, Secret};
use resolve::Validate;
#[cfg(not(feature = "config"))]
use no_store::{Keeper, Store};
#[cfg(feature = "config")]
use {crate::Store, store::Keeper};
#[cfg(feature = "config")]
pub(crate) use hub::Hub;
#[cfg(all(feature = "config", any(test, feature = "wire", feature = "records")))]
pub(crate) use store::Sent;
#[cfg(all(feature = "config", feature = "client"))]
pub(crate) use store::names;
use crate::Result;
#[must_use = "a config opens with open()"]
pub struct ConfigBuilder<T> {
store: Option<Store>,
name: String,
declaration: Declared<T>,
layers: Vec<Layer>,
validate: Option<Validate<T>>,
strict: bool,
}
#[diagnostic::on_unimplemented(
message = "a config is declared by a serde value that is Clone, Send and Sync, or by a config::Declaration",
label = "neither a Serialize + DeserializeOwned + Clone + Send + Sync value nor a Declaration"
)]
pub trait IntoDeclaration {
type Value: Serialize + DeserializeOwned + Clone + Send + Sync + 'static;
#[doc(hidden)]
fn into_declared(self) -> Declared<Self::Value>;
}
impl<T> IntoDeclaration for T
where
T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
{
type Value = T;
fn into_declared(self) -> Declared<T> {
Declared(Source::Typed(self))
}
}
impl IntoDeclaration for Declaration {
type Value = serde_json::Value;
fn into_declared(self) -> Declared<serde_json::Value> {
Declared(Source::Data(self))
}
}
#[doc(hidden)]
pub struct Declared<T>(Source<T>);
enum Source<T> {
Typed(T),
Data(Declaration),
}
impl<T> fmt::Debug for Declared<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Source::Typed(_) => f.write_str("Declared(a typed value)"),
Source::Data(declaration) => f.debug_tuple("Declared").field(declaration).finish(),
}
}
}
pub fn load<D: IntoDeclaration>(name: &str, declaration: D) -> ConfigBuilder<D::Value> {
let declaration = declaration.into_declared();
ConfigBuilder { store: None, name: name.to_owned(), declaration, layers: Vec::new(), validate: None, strict: false }
}
impl<T> ConfigBuilder<T>
where
T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
{
pub fn file(mut self, file: impl Into<File>) -> Self {
self.layers.push(Layer::File(file.into()));
self
}
pub fn env(mut self, env: impl Into<Env>) -> Self {
self.layers.push(Layer::Env(env.into()));
self
}
pub fn validate(
mut self,
validate: impl Fn(&T) -> std::result::Result<(), String> + Send + Sync + 'static,
) -> Self {
self.validate = Some(Box::new(validate));
self
}
pub fn strict(mut self) -> Self {
self.strict = true;
self
}
pub fn open(self) -> Result<Config<T>> {
handle::open(self)
}
}
impl<T> fmt::Debug for ConfigBuilder<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ConfigBuilder")
.field("name", &self.name)
.field("layers", &self.layers)
.field("strict", &self.strict)
.finish_non_exhaustive()
}
}
#[cfg(all(test, feature = "config"))]
#[path = "config_tests.rs"]
mod tests;