#[cfg(any(test, feature = "wire", feature = "records"))]
mod data;
mod declaration;
mod env;
mod explain;
mod files;
mod handle;
mod hub;
mod layers;
mod marks;
mod resolve;
mod rules;
use std::fmt;
use serde::Serialize;
use serde::de::DeserializeOwned;
pub use env::Env;
pub use explain::{Explained, Line};
pub use files::File;
pub use handle::{Config, IfVersion, Snapshot, Watching};
pub(crate) use hub::Hub;
use layers::Layer;
pub use marks::{Live, Required, Secret};
use resolve::Validate;
use crate::{Result, Store};
#[must_use = "a config opens with open()"]
pub struct ConfigBuilder<T> {
store: Store,
name: String,
declaration: T,
layers: Vec<Layer>,
validate: Option<Validate<T>>,
strict: bool,
}
impl Store {
pub fn config<T>(&self, name: &str, declaration: T) -> ConfigBuilder<T>
where
T: Serialize + DeserializeOwned + Clone + Send + Sync + 'static,
{
ConfigBuilder {
store: self.clone(),
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)
}
}
#[cfg(any(test, feature = "wire", feature = "records"))]
pub(crate) struct Sent<'a> {
pub(crate) name: &'a str,
pub(crate) declaration: &'a serde_json::Value,
pub(crate) layers: &'a serde_json::Value,
pub(crate) strict: bool,
pub(crate) remember: bool,
}
#[cfg(any(test, feature = "wire", feature = "records"))]
impl Store {
pub(crate) fn config_sent(&self, sent: &Sent<'_>) -> Result<Config<serde_json::Value>> {
let within = |why: String| crate::Error::invalid(why).within(format!("config {}", sent.name));
let declaration = data::declaration(sent.declaration).map_err(within)?;
let layers = data::layers(sent.layers).map_err(within)?;
handle::open_with(handle::Opening {
store: self.clone(),
name: sent.name.to_owned(),
declaration,
layers,
validate: None,
strict: sent.strict,
view: resolve::View::Data,
remember: sent.remember,
})
}
}
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(test)]
#[path = "config_tests.rs"]
mod tests;