use super::typed::{ValueOf, lists};
use super::value::Value;
use super::{Group, Key, Overridable, Settable, SingleKey};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Entry {
pub(crate) group: &'static str,
pub(crate) key: &'static str,
pub(crate) text: String,
}
pub(crate) fn put(entries: &mut Vec<Entry>, group: &'static str, key: &'static str, text: String) {
match entries
.iter_mut()
.find(|entry| entry.group == group && entry.key == key)
{
Some(entry) => entry.text = text,
None => entries.push(Entry { group, key, text }),
}
}
pub(crate) fn find<'a>(entries: &'a [Entry], group: &str, key: &str) -> Option<&'a str> {
entries
.iter()
.find(|entry| entry.group == group && entry.key == key)
.map(|entry| entry.text.as_str())
}
pub(crate) fn put_settable<K>(entries: &mut Vec<Entry>, value: ValueOf<K>)
where
K: Settable + Key<<K as SingleKey>::Group>,
{
const {
assert!(
lists(<<K as SingleKey>::Group as Group>::SETTABLE, K::NAME),
"the key is not in the list `SETTABLE` of its group"
)
};
put(entries, K::Group::NAME, K::NAME, value.format());
}
#[derive(Debug, Clone, Default)]
pub(crate) struct Overrides {
forced: Vec<Entry>,
fallback: Vec<Entry>,
}
impl Overrides {
pub(crate) fn force<K>(&mut self, value: ValueOf<K>)
where
K: Overridable + Key<<K as SingleKey>::Group>,
{
check_overridable::<K>();
put(&mut self.forced, K::Group::NAME, K::NAME, value.format());
}
pub(crate) fn fallback<K>(&mut self, value: ValueOf<K>)
where
K: Overridable + Key<<K as SingleKey>::Group>,
{
check_overridable::<K>();
put(&mut self.fallback, K::Group::NAME, K::NAME, value.format());
}
pub(crate) fn forced(&self, group: &str, key: &str) -> Option<&str> {
find(&self.forced, group, key)
}
pub(crate) fn fallback_of(&self, group: &str, key: &str) -> Option<&str> {
find(&self.fallback, group, key)
}
}
fn check_overridable<K>()
where
K: Overridable + Key<<K as SingleKey>::Group>,
{
const {
assert!(
lists(<<K as SingleKey>::Group as Group>::OVERRIDABLE, K::NAME),
"the key is not in the list `OVERRIDABLE` of its group"
)
};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::repo;
#[test]
fn a_repeated_key_replaces_its_value_in_place() {
let mut entries = Vec::new();
put(&mut entries, "core", "a", "1".into());
put(&mut entries, "x", "a", "2".into());
put(&mut entries, "core", "b", "3".into());
put(&mut entries, "core", "a", "4".into());
let keys: Vec<_> = entries
.iter()
.map(|e| (e.group, e.key, e.text.as_str()))
.collect();
assert_eq!(
keys,
[("core", "a", "4"), ("x", "a", "2"), ("core", "b", "3")]
);
assert_eq!(find(&entries, "x", "a"), Some("2"));
assert_eq!(find(&entries, "x", "b"), None);
}
#[test]
fn the_overrides_store_the_formatted_text() {
let mut overrides = Overrides::default();
overrides.force::<repo::Fsync>(false);
overrides.fallback::<repo::DefaultRepoFinders>(vec!["a b".into(), "c".into()]);
assert_eq!(overrides.forced("core", "fsync"), Some("false"));
assert_eq!(overrides.fallback_of("core", "fsync"), None);
assert_eq!(
overrides.fallback_of("core", "default-repo-finders"),
Some("a b;c;")
);
}
}