use std::ffi::OsString;
use std::{fs, io, sync::Arc};
use figment::Figment;
use void::ResultVoidExt as _;
use crate::err::ConfigError;
use crate::{CmdLine, ConfigurationTree};
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Default)]
pub struct ConfigurationSources {
files: Vec<(ConfigurationSource, MustRead)>,
options: Vec<String>,
mistrust: fs_mistrust::Mistrust,
}
#[derive(Clone, Debug, Copy, Eq, PartialEq)]
#[allow(clippy::exhaustive_enums)]
pub enum MustRead {
TolerateAbsence,
MustRead,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[allow(clippy::exhaustive_enums)]
pub enum ConfigurationSource {
File(PathBuf),
Dir(PathBuf),
Verbatim(Arc<String>),
}
impl ConfigurationSource {
pub fn from_path<P: Into<PathBuf>>(p: P) -> ConfigurationSource {
use ConfigurationSource as CS;
let p = p.into();
if is_syntactically_directory(&p) {
CS::Dir(p)
} else {
CS::File(p)
}
}
pub fn from_verbatim(text: String) -> ConfigurationSource {
Self::Verbatim(Arc::new(text))
}
pub fn as_path(&self) -> Option<&Path> {
use ConfigurationSource as CS;
match self {
CS::File(p) | CS::Dir(p) => Some(p),
CS::Verbatim(_) => None,
}
}
}
#[derive(Debug)]
pub struct FoundConfigFiles<'srcs> {
files: Vec<FoundConfigFile>,
sources: &'srcs ConfigurationSources,
}
#[derive(Debug, Clone)]
struct FoundConfigFile {
source: ConfigurationSource,
must_read: MustRead,
}
impl ConfigurationSources {
pub fn new_empty() -> Self {
Self::default()
}
pub fn from_cmdline<F, O>(
default_config_files: impl IntoIterator<Item = ConfigurationSource>,
config_files_options: impl IntoIterator<Item = F>,
cmdline_toml_override_options: impl IntoIterator<Item = O>,
) -> Self
where
F: Into<PathBuf>,
O: Into<String>,
{
ConfigurationSources::try_from_cmdline(
|| Ok(default_config_files),
config_files_options,
cmdline_toml_override_options,
)
.void_unwrap()
}
pub fn try_from_cmdline<F, O, DEF, E>(
default_config_files: impl FnOnce() -> Result<DEF, E>,
config_files_options: impl IntoIterator<Item = F>,
cmdline_toml_override_options: impl IntoIterator<Item = O>,
) -> Result<Self, E>
where
F: Into<PathBuf>,
O: Into<String>,
DEF: IntoIterator<Item = ConfigurationSource>,
{
let mut cfg_sources = ConfigurationSources::new_empty();
let mut any_files = false;
for f in config_files_options {
let f = f.into();
cfg_sources.push_source(ConfigurationSource::from_path(f), MustRead::MustRead);
any_files = true;
}
if !any_files {
for default in default_config_files()? {
cfg_sources.push_source(default, MustRead::TolerateAbsence);
}
}
for s in cmdline_toml_override_options {
cfg_sources.push_option(s);
}
Ok(cfg_sources)
}
pub fn push_source(&mut self, src: ConfigurationSource, must_read: MustRead) {
self.files.push((src, must_read));
}
pub fn push_option(&mut self, option: impl Into<String>) {
self.options.push(option.into());
}
pub fn set_mistrust(&mut self, mistrust: fs_mistrust::Mistrust) {
self.mistrust = mistrust;
}
pub fn mistrust(&self) -> &fs_mistrust::Mistrust {
&self.mistrust
}
pub fn load(&self) -> Result<ConfigurationTree, ConfigError> {
let files = self.scan()?;
files.load()
}
pub fn scan(&self) -> Result<FoundConfigFiles, ConfigError> {
let mut out = vec![];
for &(ref source, must_read) in &self.files {
let required = must_read == MustRead::MustRead;
let handle_io_error = |e: io::Error, p: &Path| {
if e.kind() == io::ErrorKind::NotFound && !required {
Result::<_, crate::ConfigError>::Ok(())
} else {
Err(crate::ConfigError::Io {
action: "reading",
path: p.to_owned(),
err: Arc::new(e),
})
}
};
use ConfigurationSource as CS;
match &source {
CS::Dir(dirname) => {
let dir = match fs::read_dir(dirname) {
Ok(y) => y,
Err(e) => {
handle_io_error(e, dirname.as_ref())?;
continue;
}
};
out.push(FoundConfigFile {
source: source.clone(),
must_read,
});
let mut entries = vec![];
for found in dir {
let found = match found {
Ok(y) => y,
Err(e) => {
handle_io_error(e, dirname.as_ref())?;
continue;
}
};
let leaf = found.file_name();
let leaf: &Path = leaf.as_ref();
match leaf.extension() {
Some(e) if e == "toml" => {}
_ => continue,
}
entries.push(found.path());
}
entries.sort();
out.extend(entries.into_iter().map(|path| FoundConfigFile {
source: CS::File(path),
must_read: MustRead::TolerateAbsence,
}));
}
CS::File(_) | CS::Verbatim(_) => {
out.push(FoundConfigFile {
source: source.clone(),
must_read,
});
}
}
}
Ok(FoundConfigFiles {
files: out,
sources: self,
})
}
}
impl FoundConfigFiles<'_> {
pub fn iter(&self) -> impl Iterator<Item = &ConfigurationSource> {
self.files.iter().map(|f| &f.source)
}
fn add_sources(self, mut builder: Figment) -> Result<Figment, ConfigError> {
use figment::providers::Format;
for FoundConfigFile { source, must_read } in self.files {
use ConfigurationSource as CS;
let required = must_read == MustRead::MustRead;
let file = match source {
CS::File(file) => file,
CS::Dir(_) => continue,
CS::Verbatim(text) => {
builder = builder.merge(figment::providers::Toml::string(&text));
continue;
}
};
match self
.sources
.mistrust
.verifier()
.permit_readable()
.check(&file)
{
Ok(()) => {}
Err(fs_mistrust::Error::NotFound(_)) if !required => {
continue;
}
Err(e) => return Err(ConfigError::FileAccess(e)),
}
let f = figment::providers::Toml::file_exact(file);
builder = builder.merge(f);
}
let mut cmdline = CmdLine::new();
for opt in &self.sources.options {
cmdline.push_toml_line(opt.clone());
}
builder = builder.merge(cmdline);
Ok(builder)
}
pub fn load(self) -> Result<ConfigurationTree, ConfigError> {
let mut builder = Figment::new();
builder = self.add_sources(builder)?;
Ok(ConfigurationTree(builder))
}
}
fn is_syntactically_directory(p: &Path) -> bool {
use std::path::Component as PC;
match p.components().next_back() {
None => false,
Some(PC::Prefix(_)) | Some(PC::RootDir) | Some(PC::CurDir) | Some(PC::ParentDir) => true,
Some(PC::Normal(_)) => {
let l = p.components().count();
let mut appended = OsString::from(p);
appended.push("a");
let l2 = PathBuf::from(appended).components().count();
l2 != l
}
}
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
use itertools::Itertools;
use tempfile::tempdir;
static EX_TOML: &str = "
[hello]
world = \"stuff\"
friends = 4242
";
fn sources_nodefaults<P: AsRef<Path>>(
files: &[(P, MustRead)],
opts: &[String],
) -> ConfigurationSources {
let mistrust = fs_mistrust::Mistrust::new_dangerously_trust_everyone();
let files = files
.iter()
.map(|(p, m)| (ConfigurationSource::from_path(p.as_ref()), *m))
.collect_vec();
let options = opts.iter().cloned().collect_vec();
ConfigurationSources {
files,
options,
mistrust,
}
}
fn load_nodefaults<P: AsRef<Path>>(
files: &[(P, MustRead)],
opts: &[String],
) -> Result<ConfigurationTree, crate::ConfigError> {
sources_nodefaults(files, opts).load()
}
#[test]
fn non_required_file() {
let td = tempdir().unwrap();
let dflt = td.path().join("a_file");
let files = vec![(dflt, MustRead::TolerateAbsence)];
load_nodefaults(&files, Default::default()).unwrap();
}
static EX2_TOML: &str = "
[hello]
world = \"nonsense\"
";
#[test]
fn both_required_and_not() {
let td = tempdir().unwrap();
let dflt = td.path().join("a_file");
let cf = td.path().join("other_file");
std::fs::write(&cf, EX2_TOML).unwrap();
let files = vec![(dflt, MustRead::TolerateAbsence), (cf, MustRead::MustRead)];
let c = load_nodefaults(&files, Default::default()).unwrap();
assert!(c.get_string("hello.friends").is_err());
assert_eq!(c.get_string("hello.world").unwrap(), "nonsense");
}
#[test]
fn dir_with_some() {
let td = tempdir().unwrap();
let cf = td.path().join("1.toml");
let d = td.path().join("extra.d/");
let df = d.join("2.toml");
let xd = td.path().join("nonexistent.d/");
std::fs::create_dir(&d).unwrap();
std::fs::write(&cf, EX_TOML).unwrap();
std::fs::write(df, EX2_TOML).unwrap();
std::fs::write(d.join("not-toml"), "SYNTAX ERROR").unwrap();
let files = vec![
(cf, MustRead::MustRead),
(d, MustRead::MustRead),
(xd.clone(), MustRead::TolerateAbsence),
];
let c = sources_nodefaults(&files, Default::default());
let found = c.scan().unwrap();
assert_eq!(
found
.iter()
.map(|p| p
.as_path()
.unwrap()
.strip_prefix(&td)
.unwrap()
.to_str()
.unwrap())
.collect_vec(),
&["1.toml", "extra.d", "extra.d/2.toml"]
);
let c = found.load().unwrap();
assert_eq!(c.get_string("hello.friends").unwrap(), "4242");
assert_eq!(c.get_string("hello.world").unwrap(), "nonsense");
let files = vec![(xd, MustRead::MustRead)];
let e = load_nodefaults(&files, Default::default())
.unwrap_err()
.to_string();
assert!(dbg!(e).contains("nonexistent.d"));
}
#[test]
fn load_two_files_with_cmdline() {
let td = tempdir().unwrap();
let cf1 = td.path().join("a_file");
let cf2 = td.path().join("other_file");
std::fs::write(&cf1, EX_TOML).unwrap();
std::fs::write(&cf2, EX2_TOML).unwrap();
let v = vec![(cf1, MustRead::TolerateAbsence), (cf2, MustRead::MustRead)];
let v2 = vec!["other.var=present".to_string()];
let c = load_nodefaults(&v, &v2).unwrap();
assert_eq!(c.get_string("hello.friends").unwrap(), "4242");
assert_eq!(c.get_string("hello.world").unwrap(), "nonsense");
assert_eq!(c.get_string("other.var").unwrap(), "present");
}
#[test]
fn from_cmdline() {
let sources = ConfigurationSources::from_cmdline(
[ConfigurationSource::from_path("/etc/loid.toml")],
["/family/yor.toml", "/family/anya.toml"],
["decade=1960", "snack=peanuts"],
);
let files: Vec<_> = sources
.files
.iter()
.map(|file| file.0.as_path().unwrap().to_str().unwrap())
.collect();
assert_eq!(files, vec!["/family/yor.toml", "/family/anya.toml"]);
assert_eq!(sources.files[0].1, MustRead::MustRead);
assert_eq!(
&sources.options,
&vec!["decade=1960".to_owned(), "snack=peanuts".to_owned()]
);
let sources = ConfigurationSources::from_cmdline(
[ConfigurationSource::from_path("/etc/loid.toml")],
Vec::<PathBuf>::new(),
["decade=1960", "snack=peanuts"],
);
assert_eq!(
&sources.files,
&vec![(
ConfigurationSource::from_path("/etc/loid.toml"),
MustRead::TolerateAbsence
)]
);
}
#[test]
fn dir_syntax() {
let chk = |tf, s: &str| assert_eq!(tf, is_syntactically_directory(s.as_ref()), "{:?}", s);
chk(false, "");
chk(false, "1");
chk(false, "1/2");
chk(false, "/1");
chk(false, "/1/2");
chk(true, "/");
chk(true, ".");
chk(true, "./");
chk(true, "..");
chk(true, "../");
chk(true, "/");
chk(true, "1/");
chk(true, "1/2/");
chk(true, "/1/");
chk(true, "/1/2/");
}
}