fud_core/
config.rs

1use figment::{
2    providers::{Format, Serialized, Toml},
3    Figment,
4};
5use serde::{Deserialize, Serialize};
6use std::{env, path::Path};
7
8#[derive(Debug, Serialize, Deserialize)]
9pub struct GlobalConfig {
10    /// The `ninja` command to execute in `run` mode.
11    pub ninja: String,
12
13    /// Never delete the temporary directory used to execute ninja in `run` mode.
14    pub keep_build_dir: bool,
15
16    /// Enable verbose output.
17    pub verbose: bool,
18}
19
20impl Default for GlobalConfig {
21    fn default() -> Self {
22        Self {
23            ninja: "ninja".to_string(),
24            keep_build_dir: false,
25            verbose: false,
26        }
27    }
28}
29
30/// Location of the configuration file
31pub(crate) fn config_path(name: &str) -> std::path::PathBuf {
32    // The configuration is usually at `~/.config/driver_name.toml`.
33    let config_base = env::var("XDG_CONFIG_HOME").unwrap_or_else(|_| {
34        let home = env::var("HOME").expect("$HOME not set");
35        home + "/.config"
36    });
37    let config_path = Path::new(&config_base).join(name).with_extension("toml");
38    log::info!("Loading config from {}", config_path.display());
39    config_path
40}
41
42/// Load configuration data from the standard config file location.
43pub(crate) fn load_config(name: &str) -> Figment {
44    let config_path = config_path(name);
45
46    // Use our defaults, overridden by the TOML config file.
47    Figment::from(Serialized::defaults(GlobalConfig::default()))
48        .merge(Toml::file(config_path))
49}