use std::borrow::Borrow;
use std::convert::Infallible;
use std::fmt;
use std::fs;
use std::io;
use std::ops::Range;
use std::path::Path;
use std::path::PathBuf;
use std::slice;
use std::str::FromStr;
use std::sync::Arc;
use itertools::Itertools as _;
use once_cell::sync::Lazy;
use serde::de::IntoDeserializer as _;
use serde::Deserialize;
use thiserror::Error;
use toml_edit::DocumentMut;
use toml_edit::ImDocument;
pub use crate::config_resolver::migrate;
pub use crate::config_resolver::resolve;
pub use crate::config_resolver::ConfigMigrateError;
pub use crate::config_resolver::ConfigMigrateLayerError;
pub use crate::config_resolver::ConfigMigrationRule;
pub use crate::config_resolver::ConfigResolutionContext;
use crate::file_util::IoResultExt as _;
use crate::file_util::PathError;
pub type ConfigItem = toml_edit::Item;
pub type ConfigTable = toml_edit::Table;
pub type ConfigTableLike<'a> = dyn toml_edit::TableLike + 'a;
pub type ConfigValue = toml_edit::Value;
#[derive(Debug, Error)]
pub enum ConfigLoadError {
#[error("Failed to read configuration file")]
Read(#[source] PathError),
#[error("Configuration cannot be parsed as TOML document")]
Parse {
#[source]
error: toml_edit::TomlError,
source_path: Option<PathBuf>,
},
}
#[derive(Debug, Error)]
#[error("Failed to write configuration file")]
pub struct ConfigFileSaveError(#[source] pub PathError);
#[derive(Debug, Error)]
pub enum ConfigGetError {
#[error("Value not found for {name}")]
NotFound {
name: String,
},
#[error("Invalid type or value for {name}")]
Type {
name: String,
#[source]
error: Box<dyn std::error::Error + Send + Sync>,
source_path: Option<PathBuf>,
},
}
#[derive(Debug, Error)]
pub enum ConfigUpdateError {
#[error("Would overwrite non-table value with parent table {name}")]
WouldOverwriteValue {
name: String,
},
#[error("Would overwrite entire table {name}")]
WouldOverwriteTable {
name: String,
},
#[error("Would delete entire table {name}")]
WouldDeleteTable {
name: String,
},
}
pub trait ConfigGetResultExt<T> {
fn optional(self) -> Result<Option<T>, ConfigGetError>;
}
impl<T> ConfigGetResultExt<T> for Result<T, ConfigGetError> {
fn optional(self) -> Result<Option<T>, ConfigGetError> {
match self {
Ok(value) => Ok(Some(value)),
Err(ConfigGetError::NotFound { .. }) => Ok(None),
Err(err) => Err(err),
}
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ConfigNamePathBuf(Vec<toml_edit::Key>);
impl ConfigNamePathBuf {
pub fn root() -> Self {
ConfigNamePathBuf(vec![])
}
pub fn is_root(&self) -> bool {
self.0.is_empty()
}
pub fn starts_with(&self, base: impl AsRef<[toml_edit::Key]>) -> bool {
self.0.starts_with(base.as_ref())
}
pub fn components(&self) -> slice::Iter<'_, toml_edit::Key> {
self.0.iter()
}
pub fn push(&mut self, key: impl Into<toml_edit::Key>) {
self.0.push(key.into());
}
}
impl From<&ConfigNamePathBuf> for ConfigNamePathBuf {
fn from(value: &ConfigNamePathBuf) -> Self {
value.clone()
}
}
impl<K: Into<toml_edit::Key>> FromIterator<K> for ConfigNamePathBuf {
fn from_iter<I: IntoIterator<Item = K>>(iter: I) -> Self {
let keys = iter.into_iter().map(|k| k.into()).collect();
ConfigNamePathBuf(keys)
}
}
impl FromStr for ConfigNamePathBuf {
type Err = toml_edit::TomlError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
toml_edit::Key::parse(s).map(ConfigNamePathBuf)
}
}
impl AsRef<[toml_edit::Key]> for ConfigNamePathBuf {
fn as_ref(&self) -> &[toml_edit::Key] {
&self.0
}
}
impl fmt::Display for ConfigNamePathBuf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut components = self.0.iter().fuse();
if let Some(key) = components.next() {
write!(f, "{key}")?;
}
components.try_for_each(|key| write!(f, ".{key}"))
}
}
pub trait ToConfigNamePath: Sized {
type Output: Borrow<ConfigNamePathBuf> + Into<ConfigNamePathBuf>;
fn into_name_path(self) -> Self::Output;
}
impl ToConfigNamePath for ConfigNamePathBuf {
type Output = Self;
fn into_name_path(self) -> Self::Output {
self
}
}
impl ToConfigNamePath for &ConfigNamePathBuf {
type Output = Self;
fn into_name_path(self) -> Self::Output {
self
}
}
impl ToConfigNamePath for &'static str {
type Output = ConfigNamePathBuf;
fn into_name_path(self) -> Self::Output {
self.parse()
.expect("valid TOML dotted key must be provided")
}
}
impl<const N: usize> ToConfigNamePath for [&str; N] {
type Output = ConfigNamePathBuf;
fn into_name_path(self) -> Self::Output {
self.into_iter().collect()
}
}
impl<const N: usize> ToConfigNamePath for &[&str; N] {
type Output = ConfigNamePathBuf;
fn into_name_path(self) -> Self::Output {
self.as_slice().into_name_path()
}
}
impl ToConfigNamePath for &[&str] {
type Output = ConfigNamePathBuf;
fn into_name_path(self) -> Self::Output {
self.iter().copied().collect()
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ConfigSource {
Default,
EnvBase,
User,
Repo,
EnvOverrides,
CommandArg,
}
#[derive(Clone, Debug)]
pub struct ConfigLayer {
pub source: ConfigSource,
pub path: Option<PathBuf>,
pub data: DocumentMut,
}
impl ConfigLayer {
pub fn empty(source: ConfigSource) -> Self {
Self::with_data(source, DocumentMut::new())
}
pub fn with_data(source: ConfigSource, data: DocumentMut) -> Self {
ConfigLayer {
source,
path: None,
data,
}
}
pub fn parse(source: ConfigSource, text: &str) -> Result<Self, ConfigLoadError> {
let data = ImDocument::parse(text).map_err(|error| ConfigLoadError::Parse {
error,
source_path: None,
})?;
Ok(Self::with_data(source, data.into_mut()))
}
pub fn load_from_file(source: ConfigSource, path: PathBuf) -> Result<Self, ConfigLoadError> {
let text = fs::read_to_string(&path)
.context(&path)
.map_err(ConfigLoadError::Read)?;
let data = ImDocument::parse(text).map_err(|error| ConfigLoadError::Parse {
error,
source_path: Some(path.clone()),
})?;
Ok(ConfigLayer {
source,
path: Some(path),
data: data.into_mut(),
})
}
fn load_from_dir(source: ConfigSource, path: &Path) -> Result<Vec<Self>, ConfigLoadError> {
let mut file_paths: Vec<_> = path
.read_dir()
.and_then(|dir_entries| {
dir_entries
.map(|entry| Ok(entry?.path()))
.filter_ok(|path| path.is_file())
.try_collect()
})
.context(path)
.map_err(ConfigLoadError::Read)?;
file_paths.sort_unstable();
file_paths
.into_iter()
.map(|path| Self::load_from_file(source, path))
.try_collect()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn look_up_table(
&self,
name: impl ToConfigNamePath,
) -> Result<Option<&ConfigTableLike>, &ConfigItem> {
match self.look_up_item(name) {
Ok(Some(item)) => match item.as_table_like() {
Some(table) => Ok(Some(table)),
None => Err(item),
},
Ok(None) => Ok(None),
Err(item) => Err(item),
}
}
pub fn look_up_item(
&self,
name: impl ToConfigNamePath,
) -> Result<Option<&ConfigItem>, &ConfigItem> {
look_up_item(self.data.as_item(), name.into_name_path().borrow())
}
pub fn set_value(
&mut self,
name: impl ToConfigNamePath,
new_value: impl Into<ConfigValue>,
) -> Result<Option<ConfigValue>, ConfigUpdateError> {
let would_overwrite_table = |name| ConfigUpdateError::WouldOverwriteValue { name };
let name = name.into_name_path();
let name = name.borrow();
let (leaf_key, table_keys) = name
.0
.split_last()
.ok_or_else(|| would_overwrite_table(name.to_string()))?;
let parent_table = ensure_table(self.data.as_table_mut(), table_keys)
.map_err(|keys| would_overwrite_table(keys.join(".")))?;
match parent_table.entry_format(leaf_key) {
toml_edit::Entry::Occupied(mut entry) => {
if !entry.get().is_value() {
return Err(ConfigUpdateError::WouldOverwriteTable {
name: name.to_string(),
});
}
let old_item = entry.insert(toml_edit::value(new_value));
Ok(Some(old_item.into_value().unwrap()))
}
toml_edit::Entry::Vacant(entry) => {
entry.insert(toml_edit::value(new_value));
let mut new_key = parent_table.key_mut(leaf_key).unwrap();
new_key.leaf_decor_mut().clear();
Ok(None)
}
}
}
pub fn delete_value(
&mut self,
name: impl ToConfigNamePath,
) -> Result<Option<ConfigValue>, ConfigUpdateError> {
let would_delete_table = |name| ConfigUpdateError::WouldDeleteTable { name };
let name = name.into_name_path();
let name = name.borrow();
let mut keys = name.components();
let leaf_key = keys
.next_back()
.ok_or_else(|| would_delete_table(name.to_string()))?;
let Some(parent_table) = keys.try_fold(
self.data.as_table_mut() as &mut ConfigTableLike,
|table, key| table.get_mut(key)?.as_table_like_mut(),
) else {
return Ok(None);
};
match parent_table.entry(leaf_key) {
toml_edit::Entry::Occupied(entry) => {
if !entry.get().is_value() {
return Err(would_delete_table(name.to_string()));
}
let old_item = entry.remove();
Ok(Some(old_item.into_value().unwrap()))
}
toml_edit::Entry::Vacant(_) => Ok(None),
}
}
pub fn ensure_table(
&mut self,
name: impl ToConfigNamePath,
) -> Result<&mut ConfigTableLike, ConfigUpdateError> {
let would_overwrite_table = |name| ConfigUpdateError::WouldOverwriteValue { name };
let name = name.into_name_path();
let name = name.borrow();
ensure_table(self.data.as_table_mut(), &name.0)
.map_err(|keys| would_overwrite_table(keys.join(".")))
}
}
fn look_up_item<'a>(
root_item: &'a ConfigItem,
name: &ConfigNamePathBuf,
) -> Result<Option<&'a ConfigItem>, &'a ConfigItem> {
let mut cur_item = root_item;
for key in name.components() {
let Some(table) = cur_item.as_table_like() else {
return Err(cur_item);
};
cur_item = match table.get(key) {
Some(item) => item,
None => return Ok(None),
};
}
Ok(Some(cur_item))
}
fn ensure_table<'a, 'b>(
root_table: &'a mut ConfigTableLike<'a>,
keys: &'b [toml_edit::Key],
) -> Result<&'a mut ConfigTableLike<'a>, &'b [toml_edit::Key]> {
keys.iter()
.enumerate()
.try_fold(root_table, |table, (i, key)| {
let sub_item = table.entry_format(key).or_insert_with(new_implicit_table);
sub_item.as_table_like_mut().ok_or(&keys[..=i])
})
}
fn new_implicit_table() -> ConfigItem {
let mut table = ConfigTable::new();
table.set_implicit(true);
ConfigItem::Table(table)
}
#[derive(Debug)]
pub struct ConfigFile {
layer: Arc<ConfigLayer>,
}
impl ConfigFile {
pub fn load_or_empty(
source: ConfigSource,
path: impl Into<PathBuf>,
) -> Result<Self, ConfigLoadError> {
let layer = match ConfigLayer::load_from_file(source, path.into()) {
Ok(layer) => Arc::new(layer),
Err(ConfigLoadError::Read(PathError { path, error }))
if error.kind() == io::ErrorKind::NotFound =>
{
Arc::new(ConfigLayer {
source,
path: Some(path),
data: DocumentMut::new(),
})
}
Err(err) => return Err(err),
};
Ok(ConfigFile { layer })
}
pub fn from_layer(layer: Arc<ConfigLayer>) -> Result<Self, Arc<ConfigLayer>> {
if layer.path.is_some() {
Ok(ConfigFile { layer })
} else {
Err(layer)
}
}
pub fn save(&self) -> Result<(), ConfigFileSaveError> {
fs::write(self.path(), self.layer.data.to_string())
.context(self.path())
.map_err(ConfigFileSaveError)
}
pub fn path(&self) -> &Path {
self.layer.path.as_ref().expect("path must be known")
}
pub fn layer(&self) -> &Arc<ConfigLayer> {
&self.layer
}
pub fn set_value(
&mut self,
name: impl ToConfigNamePath,
new_value: impl Into<ConfigValue>,
) -> Result<Option<ConfigValue>, ConfigUpdateError> {
Arc::make_mut(&mut self.layer).set_value(name, new_value)
}
pub fn delete_value(
&mut self,
name: impl ToConfigNamePath,
) -> Result<Option<ConfigValue>, ConfigUpdateError> {
Arc::make_mut(&mut self.layer).delete_value(name)
}
}
#[derive(Clone, Debug)]
pub struct StackedConfig {
layers: Vec<Arc<ConfigLayer>>,
}
impl StackedConfig {
pub fn empty() -> Self {
StackedConfig { layers: vec![] }
}
pub fn with_defaults() -> Self {
StackedConfig {
layers: DEFAULT_CONFIG_LAYERS.to_vec(),
}
}
pub fn load_file(
&mut self,
source: ConfigSource,
path: impl Into<PathBuf>,
) -> Result<(), ConfigLoadError> {
let layer = ConfigLayer::load_from_file(source, path.into())?;
self.add_layer(layer);
Ok(())
}
pub fn load_dir(
&mut self,
source: ConfigSource,
path: impl AsRef<Path>,
) -> Result<(), ConfigLoadError> {
let layers = ConfigLayer::load_from_dir(source, path.as_ref())?;
self.extend_layers(layers);
Ok(())
}
pub fn add_layer(&mut self, layer: impl Into<Arc<ConfigLayer>>) {
let layer = layer.into();
let index = self.insert_point(layer.source);
self.layers.insert(index, layer);
}
pub fn extend_layers<I>(&mut self, layers: I)
where
I: IntoIterator,
I::Item: Into<Arc<ConfigLayer>>,
{
let layers = layers.into_iter().map(Into::into);
for (source, chunk) in &layers.chunk_by(|layer| layer.source) {
let index = self.insert_point(source);
self.layers.splice(index..index, chunk);
}
}
pub fn remove_layers(&mut self, source: ConfigSource) {
self.layers.drain(self.layer_range(source));
}
fn layer_range(&self, source: ConfigSource) -> Range<usize> {
let start = self
.layers
.iter()
.take_while(|layer| layer.source < source)
.count();
let count = self.layers[start..]
.iter()
.take_while(|layer| layer.source == source)
.count();
start..(start + count)
}
fn insert_point(&self, source: ConfigSource) -> usize {
let skip = self
.layers
.iter()
.rev()
.take_while(|layer| layer.source > source)
.count();
self.layers.len() - skip
}
pub fn layers(&self) -> &[Arc<ConfigLayer>] {
&self.layers
}
pub fn layers_mut(&mut self) -> &mut [Arc<ConfigLayer>] {
&mut self.layers
}
pub fn layers_for(&self, source: ConfigSource) -> &[Arc<ConfigLayer>] {
&self.layers[self.layer_range(source)]
}
pub fn get<'de, T: Deserialize<'de>>(
&self,
name: impl ToConfigNamePath,
) -> Result<T, ConfigGetError> {
self.get_value_with(name, |value| T::deserialize(value.into_deserializer()))
}
pub fn get_value(&self, name: impl ToConfigNamePath) -> Result<ConfigValue, ConfigGetError> {
self.get_value_with::<_, Infallible>(name, Ok)
}
pub fn get_value_with<T, E: Into<Box<dyn std::error::Error + Send + Sync>>>(
&self,
name: impl ToConfigNamePath,
convert: impl FnOnce(ConfigValue) -> Result<T, E>,
) -> Result<T, ConfigGetError> {
self.get_item_with(name, |item| {
let value = item
.into_value()
.expect("Item::None should not exist in loaded tables");
convert(value)
})
}
pub fn get_table(&self, name: impl ToConfigNamePath) -> Result<ConfigTable, ConfigGetError> {
self.get_item_with(name, |item| {
item.into_table()
.map_err(|item| format!("Expected a table, but is {}", item.type_name()))
})
}
fn get_item_with<T, E: Into<Box<dyn std::error::Error + Send + Sync>>>(
&self,
name: impl ToConfigNamePath,
convert: impl FnOnce(ConfigItem) -> Result<T, E>,
) -> Result<T, ConfigGetError> {
let name = name.into_name_path();
let name = name.borrow();
let (item, layer_index) =
get_merged_item(&self.layers, name).ok_or_else(|| ConfigGetError::NotFound {
name: name.to_string(),
})?;
convert(item).map_err(|err| ConfigGetError::Type {
name: name.to_string(),
error: err.into(),
source_path: self.layers[layer_index].path.clone(),
})
}
pub fn table_keys(&self, name: impl ToConfigNamePath) -> impl Iterator<Item = &str> {
let name = name.into_name_path();
let name = name.borrow();
let to_merge = get_tables_to_merge(&self.layers, name);
to_merge
.into_iter()
.rev()
.flat_map(|table| table.iter().map(|(k, _)| k))
.unique()
}
}
fn get_merged_item(
layers: &[Arc<ConfigLayer>],
name: &ConfigNamePathBuf,
) -> Option<(ConfigItem, usize)> {
let mut to_merge = Vec::new();
for (index, layer) in layers.iter().enumerate().rev() {
let item = match layer.look_up_item(name) {
Ok(Some(item)) => item,
Ok(None) => continue, Err(_) => break, };
if item.is_table_like() {
to_merge.push((item, index));
} else if to_merge.is_empty() {
return Some((item.clone(), index)); } else {
break; }
}
let (item, mut top_index) = to_merge.pop()?;
let mut merged = item.clone();
for (item, index) in to_merge.into_iter().rev() {
merge_items(&mut merged, item);
top_index = index;
}
Some((merged, top_index))
}
fn get_tables_to_merge<'a>(
layers: &'a [Arc<ConfigLayer>],
name: &ConfigNamePathBuf,
) -> Vec<&'a ConfigTableLike<'a>> {
let mut to_merge = Vec::new();
for layer in layers.iter().rev() {
match layer.look_up_table(name) {
Ok(Some(table)) => to_merge.push(table),
Ok(None) => {} Err(_) => break, }
}
to_merge
}
fn merge_items(lower_item: &mut ConfigItem, upper_item: &ConfigItem) {
let (Some(lower_table), Some(upper_table)) =
(lower_item.as_table_like_mut(), upper_item.as_table_like())
else {
*lower_item = upper_item.clone();
return;
};
for (key, upper) in upper_table.iter() {
match lower_table.entry(key) {
toml_edit::Entry::Occupied(entry) => {
merge_items(entry.into_mut(), upper);
}
toml_edit::Entry::Vacant(entry) => {
entry.insert(upper.clone());
}
};
}
}
static DEFAULT_CONFIG_LAYERS: Lazy<[Arc<ConfigLayer>; 1]> = Lazy::new(|| {
let parse = |text: &str| Arc::new(ConfigLayer::parse(ConfigSource::Default, text).unwrap());
[parse(include_str!("config/misc.toml"))]
});
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use indoc::indoc;
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn test_config_layer_set_value() {
let mut layer = ConfigLayer::empty(ConfigSource::User);
assert_matches!(
layer.set_value(ConfigNamePathBuf::root(), 0),
Err(ConfigUpdateError::WouldOverwriteValue { name }) if name.is_empty()
);
layer.set_value("foo", 1).unwrap();
layer.set_value("bar.baz.blah", "2").unwrap();
layer
.set_value("bar.qux", ConfigValue::from_iter([("inline", "table")]))
.unwrap();
layer
.set_value("bar.to-update", ConfigValue::from_iter([("some", true)]))
.unwrap();
insta::assert_snapshot!(layer.data, @r#"
foo = 1
[bar]
qux = { inline = "table" }
to-update = { some = true }
[bar.baz]
blah = "2"
"#);
layer
.set_value("foo", ConfigValue::from_iter(["new", "foo"]))
.unwrap();
layer.set_value("bar.qux", "new bar.qux").unwrap();
layer
.set_value(
"bar.to-update.new",
ConfigValue::from_iter([("table", "value")]),
)
.unwrap();
assert_matches!(
layer.set_value("bar", 0),
Err(ConfigUpdateError::WouldOverwriteTable { name }) if name == "bar"
);
assert_matches!(
layer.set_value("bar.baz.blah.blah", 0),
Err(ConfigUpdateError::WouldOverwriteValue { name }) if name == "bar.baz.blah"
);
insta::assert_snapshot!(layer.data, @r#"
foo = ["new", "foo"]
[bar]
qux = "new bar.qux"
to-update = { some = true, new = { table = "value" } }
[bar.baz]
blah = "2"
"#);
}
#[test]
fn test_config_layer_set_value_formatting() {
let mut layer = ConfigLayer::empty(ConfigSource::User);
layer
.set_value(
"'foo' . bar . 'baz'",
ConfigValue::from_str("'value'").unwrap(),
)
.unwrap();
insta::assert_snapshot!(layer.data, @r"
['foo' . bar]
'baz' = 'value'
");
layer.set_value("foo.bar.baz", "new value").unwrap();
layer.set_value("foo.'bar'.blah", 0).unwrap();
insta::assert_snapshot!(layer.data, @r#"
['foo' . bar]
'baz' = "new value"
blah = 0
"#);
}
#[test]
fn test_config_layer_delete_value() {
let mut layer = ConfigLayer::empty(ConfigSource::User);
assert_matches!(
layer.delete_value(ConfigNamePathBuf::root()),
Err(ConfigUpdateError::WouldDeleteTable { name }) if name.is_empty()
);
layer.set_value("foo", 1).unwrap();
layer.set_value("bar.baz.blah", "2").unwrap();
layer
.set_value("bar.qux", ConfigValue::from_iter([("inline", "table")]))
.unwrap();
layer
.set_value("bar.to-update", ConfigValue::from_iter([("some", true)]))
.unwrap();
insta::assert_snapshot!(layer.data, @r#"
foo = 1
[bar]
qux = { inline = "table" }
to-update = { some = true }
[bar.baz]
blah = "2"
"#);
let old_value = layer.delete_value("foo").unwrap();
assert_eq!(old_value.and_then(|v| v.as_integer()), Some(1));
let old_value = layer.delete_value("bar.qux").unwrap();
assert!(old_value.is_some_and(|v| v.is_inline_table()));
let old_value = layer.delete_value("bar.to-update.some").unwrap();
assert_eq!(old_value.and_then(|v| v.as_bool()), Some(true));
assert_matches!(
layer.delete_value("bar"),
Err(ConfigUpdateError::WouldDeleteTable { name }) if name == "bar"
);
assert_matches!(layer.delete_value("bar.baz.blah.blah"), Ok(None));
insta::assert_snapshot!(layer.data, @r#"
[bar]
to-update = {}
[bar.baz]
blah = "2"
"#);
}
#[test]
fn test_stacked_config_layer_order() {
let empty_data = || DocumentMut::new();
let layer_sources = |config: &StackedConfig| {
config
.layers()
.iter()
.map(|layer| layer.source)
.collect_vec()
};
let mut config = StackedConfig::empty();
config.add_layer(ConfigLayer::with_data(ConfigSource::Repo, empty_data()));
config.add_layer(ConfigLayer::with_data(ConfigSource::User, empty_data()));
config.add_layer(ConfigLayer::with_data(ConfigSource::Default, empty_data()));
assert_eq!(
layer_sources(&config),
vec![
ConfigSource::Default,
ConfigSource::User,
ConfigSource::Repo,
]
);
config.add_layer(ConfigLayer::with_data(
ConfigSource::CommandArg,
empty_data(),
));
config.add_layer(ConfigLayer::with_data(ConfigSource::EnvBase, empty_data()));
config.add_layer(ConfigLayer::with_data(ConfigSource::User, empty_data()));
assert_eq!(
layer_sources(&config),
vec![
ConfigSource::Default,
ConfigSource::EnvBase,
ConfigSource::User,
ConfigSource::User,
ConfigSource::Repo,
ConfigSource::CommandArg,
]
);
config.remove_layers(ConfigSource::CommandArg);
config.remove_layers(ConfigSource::Default);
config.remove_layers(ConfigSource::User);
assert_eq!(
layer_sources(&config),
vec![ConfigSource::EnvBase, ConfigSource::Repo]
);
config.remove_layers(ConfigSource::Default);
config.remove_layers(ConfigSource::EnvOverrides);
assert_eq!(
layer_sources(&config),
vec![ConfigSource::EnvBase, ConfigSource::Repo]
);
config.extend_layers([
ConfigLayer::with_data(ConfigSource::Repo, empty_data()),
ConfigLayer::with_data(ConfigSource::Repo, empty_data()),
ConfigLayer::with_data(ConfigSource::User, empty_data()),
]);
assert_eq!(
layer_sources(&config),
vec![
ConfigSource::EnvBase,
ConfigSource::User,
ConfigSource::Repo,
ConfigSource::Repo,
ConfigSource::Repo,
]
);
config.remove_layers(ConfigSource::EnvBase);
config.remove_layers(ConfigSource::User);
config.remove_layers(ConfigSource::Repo);
assert_eq!(layer_sources(&config), vec![]);
}
fn new_user_layer(text: &str) -> ConfigLayer {
ConfigLayer::parse(ConfigSource::User, text).unwrap()
}
#[test]
fn test_stacked_config_get_simple_value() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.b.c = 'a.b.c #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a.d = ['a.d #1']
"}));
assert_eq!(config.get::<String>("a.b.c").unwrap(), "a.b.c #0");
assert_eq!(
config.get::<Vec<String>>("a.d").unwrap(),
vec!["a.d #1".to_owned()]
);
assert_matches!(
config.get::<String>("a.b.missing"),
Err(ConfigGetError::NotFound { name }) if name == "a.b.missing"
);
assert_matches!(
config.get::<String>("a.b.c.d"),
Err(ConfigGetError::NotFound { name }) if name == "a.b.c.d"
);
assert_matches!(
config.get::<String>("a.b"),
Err(ConfigGetError::Type { name, .. }) if name == "a.b"
);
}
#[test]
fn test_stacked_config_get_table_as_value() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.b = { c = 'a.b.c #0' }
"}));
config.add_layer(new_user_layer(indoc! {"
a.d = ['a.d #1']
"}));
insta::assert_snapshot!(
config.get_value("a").unwrap(),
@"{ b = { c = 'a.b.c #0' }, d = ['a.d #1'] }");
}
#[test]
fn test_stacked_config_get_inline_table() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.b = { c = 'a.b.c #0' }
"}));
config.add_layer(new_user_layer(indoc! {"
a.b = { d = 'a.b.d #1' }
"}));
insta::assert_snapshot!(
config.get_value("a.b").unwrap(),
@" { c = 'a.b.c #0' , d = 'a.b.d #1' }");
}
#[test]
fn test_stacked_config_get_inline_non_inline_table() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.b = { c = 'a.b.c #0' }
"}));
config.add_layer(new_user_layer(indoc! {"
a.b.d = 'a.b.d #1'
"}));
insta::assert_snapshot!(
config.get_value("a.b").unwrap(),
@" { c = 'a.b.c #0' , d = 'a.b.d #1'}");
insta::assert_snapshot!(
config.get_table("a").unwrap(),
@"b = { c = 'a.b.c #0' , d = 'a.b.d #1'}");
}
#[test]
fn test_stacked_config_get_value_shadowing_table() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.b.c = 'a.b.c #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a.b = 'a.b #1'
"}));
assert_eq!(config.get::<String>("a.b").unwrap(), "a.b #1");
assert_matches!(
config.get::<String>("a.b.c"),
Err(ConfigGetError::NotFound { name }) if name == "a.b.c"
);
}
#[test]
fn test_stacked_config_get_table_shadowing_table() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.b = 'a.b #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a.b.c = 'a.b.c #1'
"}));
insta::assert_snapshot!(config.get_table("a.b").unwrap(), @"c = 'a.b.c #1'");
}
#[test]
fn test_stacked_config_get_merged_table() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.a.a = 'a.a.a #0'
a.a.b = 'a.a.b #0'
a.b = 'a.b #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a.a.b = 'a.a.b #1'
a.a.c = 'a.a.c #1'
a.c = 'a.c #1'
"}));
insta::assert_snapshot!(config.get_table("a").unwrap(), @r"
a.a = 'a.a.a #0'
a.b = 'a.a.b #1'
a.c = 'a.a.c #1'
b = 'a.b #0'
c = 'a.c #1'
");
assert_eq!(config.table_keys("a").collect_vec(), vec!["a", "b", "c"]);
assert_eq!(config.table_keys("a.a").collect_vec(), vec!["a", "b", "c"]);
assert_eq!(config.table_keys("a.b").collect_vec(), vec![""; 0]);
assert_eq!(config.table_keys("a.missing").collect_vec(), vec![""; 0]);
}
#[test]
fn test_stacked_config_get_merged_table_shadowed_top() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.a.a = 'a.a.a #0'
a.b = 'a.b #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a = 'a #1'
"}));
config.add_layer(new_user_layer(indoc! {"
a.a.b = 'a.a.b #2'
"}));
insta::assert_snapshot!(config.get_table("a").unwrap(), @"a.b = 'a.a.b #2'");
assert_eq!(config.table_keys("a").collect_vec(), vec!["a"]);
assert_eq!(config.table_keys("a.a").collect_vec(), vec!["b"]);
}
#[test]
fn test_stacked_config_get_merged_table_shadowed_child() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.a.a = 'a.a.a #0'
a.b = 'a.b #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a.a = 'a.a #1'
"}));
config.add_layer(new_user_layer(indoc! {"
a.a.b = 'a.a.b #2'
"}));
insta::assert_snapshot!(config.get_table("a").unwrap(), @r"
a.b = 'a.a.b #2'
b = 'a.b #0'
");
assert_eq!(config.table_keys("a").collect_vec(), vec!["a", "b"]);
assert_eq!(config.table_keys("a.a").collect_vec(), vec!["b"]);
}
#[test]
fn test_stacked_config_get_merged_table_shadowed_parent() {
let mut config = StackedConfig::empty();
config.add_layer(new_user_layer(indoc! {"
a.a.a = 'a.a.a #0'
"}));
config.add_layer(new_user_layer(indoc! {"
a = 'a #1'
"}));
config.add_layer(new_user_layer(indoc! {"
a.a.b = 'a.a.b #2'
"}));
insta::assert_snapshot!(config.get_table("a.a").unwrap(), @"b = 'a.a.b #2'");
assert_eq!(config.table_keys("a.a").collect_vec(), vec!["b"]);
}
}