use std::path::{Path, PathBuf};
use super::schema::ConfigFile;
use crate::error::{Error, Result};
pub const CONFIG_FILE_NAME: &str = "voom.toml";
#[derive(Debug)]
pub struct Layer {
pub source: PathBuf,
pub file: ConfigFile,
pub user_level: bool,
}
#[derive(Debug, Default)]
pub struct Sources {
pub layers: Vec<Layer>,
pub explicit: bool,
}
#[must_use]
pub fn user_config_path() -> Option<PathBuf> {
let base = std::env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from)
.or_else(|| dirs::home_dir().map(|home| home.join(".config")))?;
Some(base.join("voom").join("config.toml"))
}
pub fn read(path: &Path) -> Result<Layer> {
let text = std::fs::read_to_string(path).map_err(|source| Error::Config {
path: path.to_path_buf(),
reason: source.to_string(),
})?;
let file: ConfigFile = toml::from_str(&text).map_err(|source| Error::Config {
path: path.to_path_buf(),
reason: source.to_string(),
})?;
Ok(Layer {
source: path.to_path_buf(),
file,
user_level: false,
})
}
pub fn discover(root: &Path, explicit: Option<&Path>) -> Result<Sources> {
if let Some(path) = explicit {
let mut layer = read(path)?;
layer.user_level = true;
return Ok(Sources {
layers: vec![layer],
explicit: true,
});
}
let mut layers = Vec::new();
if let Some(path) = user_config_path()
&& path.is_file()
{
let mut layer = read(&path)?;
layer.user_level = true;
layers.push(layer);
}
let _ = root;
Ok(Sources {
layers,
explicit: false,
})
}
pub fn read_in(dir: &Path) -> Result<Option<Layer>> {
let path = dir.join(CONFIG_FILE_NAME);
if !path.is_file() {
return Ok(None);
}
read(&path).map(Some)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::tree;
#[test]
fn should_read_a_valid_file() {
let fixture = tree(&["voom.toml"]);
std::fs::write(fixture.path().join("voom.toml"), "exclude = [\"**/fixtures/**\"]").unwrap();
let layer = read(&fixture.path().join("voom.toml")).expect("it parses");
assert_eq!(layer.file.exclude, vec!["**/fixtures/**"]);
assert_eq!(layer.source, fixture.path().join("voom.toml"));
}
#[test]
fn should_name_the_file_in_a_parse_error() {
let fixture = tree(&["voom.toml"]);
std::fs::write(fixture.path().join("voom.toml"), "exclude = [").unwrap();
let error = read(&fixture.path().join("voom.toml")).unwrap_err();
assert!(error.to_string().contains("voom.toml"), "{error}");
}
#[test]
fn should_treat_a_missing_file_as_no_configuration() {
let fixture = tree(&["Cargo.toml"]);
let sources = discover(fixture.path(), None).expect("discovery succeeds");
assert!(
sources
.layers
.iter()
.all(|layer| layer.source != fixture.path().join("voom.toml"))
);
assert!(read_in(fixture.path()).expect("no file is not an error").is_none());
}
#[test]
fn should_leave_the_root_voom_toml_to_the_resolver() {
let fixture = tree(&["voom.toml"]);
std::fs::write(fixture.path().join("voom.toml"), "[keep]\nmin_age = \"7d\"").unwrap();
let sources = discover(fixture.path(), None).expect("discovery succeeds");
assert!(
sources
.layers
.iter()
.all(|layer| layer.source != fixture.path().join("voom.toml"))
);
let layer = read_in(fixture.path()).expect("it parses").expect("a layer");
assert_eq!(layer.file.keep.min_age.as_deref(), Some("7d"));
}
#[test]
fn should_replace_the_hierarchy_when_a_config_is_named_explicitly() {
let fixture = tree(&["voom.toml", "explicit.toml"]);
std::fs::write(fixture.path().join("voom.toml"), "[keep]\nmin_age = \"7d\"").unwrap();
std::fs::write(fixture.path().join("explicit.toml"), "[keep]\nmin_age = \"1d\"").unwrap();
let explicit = fixture.path().join("explicit.toml");
let sources = discover(fixture.path(), Some(&explicit)).expect("discovery succeeds");
assert_eq!(sources.layers.len(), 1);
assert_eq!(sources.layers[0].source, explicit);
}
#[test]
fn should_fail_when_an_explicit_config_does_not_exist() {
let fixture = tree(&["Cargo.toml"]);
let missing = fixture.path().join("nope.toml");
assert!(discover(fixture.path(), Some(&missing)).is_err());
}
}