libscilo/config/mod.rs
1//! Parsing configuration files and representing them internally.
2
3use crate::version_control::project_root;
4use clap::crate_name;
5use dirs::{config_dir, home_dir};
6use std::path::PathBuf;
7
8pub(crate) mod error;
9mod file;
10pub(crate) mod instantiated_config;
11pub(crate) mod parse;
12
13/// The default file name for the configuration file.
14const CONFIG_NAME: &str = "scilo.toml";
15
16/// Find all configuration files available to be parsed.
17///
18/// Configuration files are detected in the following order:
19/// 1. `${PROJECT_ROOT}/scilo.toml`
20/// 2. `${PROJECT_ROOT}/.config/scilo/config.toml`
21/// 3. [`config_dir()`][dirs::config_dir]`/scilo/config.toml`
22///
23/// These configurations are returned in order of priority.
24/// The first element is the highest priority, so options that are defined in
25/// the first configuration file will overrule the same option in the last file.
26pub fn find_config_paths() -> Vec<PathBuf> {
27 let mut paths = vec![];
28 let repo_root = project_root();
29
30 // check current and extra directories
31 let extra_paths = vec![
32 // `$GIT_ROOT/scilo.toml`
33 repo_root.clone().join(CONFIG_NAME),
34 // `$GIT_ROOT/.config/scilo/config.toml`
35 repo_root
36 .join(".config")
37 .join(crate_name!())
38 .join("config.toml"),
39 // `$XDG_CONFIG_DIR/scilo/config.toml`
40 config_dir()
41 .unwrap_or(home_dir().unwrap().join(".config"))
42 .join(crate_name!())
43 .join("config.toml"),
44 ];
45
46 for p in extra_paths {
47 if p.exists() {
48 paths.push(p);
49 }
50 }
51
52 paths
53}