1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub mod env;

use crate::app::AppBuilder;
use crate::error::{AppError, Result};
use anyhow::Context;
use env::Env;
use serde_toml_merge::merge_tables;
pub use spring_macros::Configurable;
use std::fs;
use toml::Table;

pub trait Configurable {
    /// Prefix used to read toml configuration.
    /// If you need to load external configuration, you need to rewrite this method
    fn config_prefix(&self) -> &str;
}

/// load toml config
pub(crate) fn load_config(app: &AppBuilder, env: Env) -> Result<Table> {
    let main_path = app.config_path.as_path();
    let config_file_content = fs::read_to_string(main_path);
    let main_toml_str = match config_file_content {
        Err(e) => {
            log::warn!("Failed to read configuration file {:?}: {}", main_path, e);
            return Ok(Table::new());
        }
        Ok(content) => content,
    };

    let main_table = toml::from_str::<Table>(main_toml_str.as_str())
        .with_context(|| format!("Failed to parse the toml file at path {:?}", main_path))?;

    let config_table = match env.get_config_path(main_path) {
        Ok(env_path) => {
            let env_path = env_path.as_path();
            if !env_path.exists() {
                return Ok(main_table);
            }

            let env_toml_str = fs::read_to_string(env_path)
                .with_context(|| format!("Failed to read configuration file {:?}", env_path))?;
            let env_table = toml::from_str::<Table>(env_toml_str.as_str())
                .with_context(|| format!("Failed to parse the toml file at path {:?}", env_path))?;
            merge_tables(main_table, env_table)
                .map_err(|e| AppError::TomlMergeError(format!("merge toml error: {}", e)))
                .with_context(|| {
                    format!("Failed to merge files {:?} and {:?}", main_path, env_path)
                })?
        }
        Err(_) => {
            log::debug!("{:?} config not found", env);
            main_table
        }
    };

    Ok(config_table)
}