Repository: denisidoro/navi
File: src/config/yaml.rs (Rust)

Code before:
pub struct YamlConfig {
    pub style: Style,
    pub finder: Finder,
    pub cheats: Cheats,
    pub search: Search,
    pub shell: Shell,
    pub client: Client,
    pub source: String, // <= The source of the current configuration
}

impl YamlConfig {
    fn from_str(text: &str) -> Result<Self> {
        serde_yaml::from_str(text).map_err(|e| e.into())
    }

    fn from_path(path: &Path) -> Result<Self> {
        let file = fs::open(path)?;
        let reader = BufReader::new(file);
        serde_yaml::from_reader(reader).map_err(|e| e.into())
    }

    pub fn get(env: &EnvConfig) -> Result<YamlConfig> {
        if let Some(yaml) = env.config_yaml.as_ref() {
            // We're getting the configuration from the environment variable `NAVI_CONFIG_YAML`
            let mut cfg = Self::from_str(yaml)?;
            cfg.source = format!("NAVI_CONFIG_YAML={}", yaml);

            return Ok(cfg);
        }
        if let Some(path_str) = env.config_path.as_ref() {
            // We're getting the configuration from a file given in the environment variable 'NAVI_CONFIG'

            let p = PathBuf::from(path_str);
            let mut cfg = YamlConfig::from_path(&p)?;
            cfg.source = format!("NAVI_CONFIG_YAML={:?}", p).to_string();

            return Ok(cfg);
        }
        if let Ok(p) = default_config_pathbuf() {
            // We're getting the configuration from the default path

            if p.exists() {
                let mut cfg = YamlConfig::from_path(&p)?;
                cfg.source = p.to_string();

                return Ok(cfg);
            }
        }

        // AS no configuration has been found, we set the YAML configuration
        // to be its default (built-in) value.

Review comment (suggestion):
```suggestion
            cfg.source = format!("{}={}", env_var::CONFIG_YAML, yaml);
```
It is better to use `env_var::CONFIG_YAML` that is used for `env.config_yaml`.
This requires `use crate::env_var;`

Code after:
pub struct YamlConfig {
    pub style: Style,
    pub finder: Finder,
    pub cheats: Cheats,
    pub search: Search,
    pub shell: Shell,
    pub client: Client,
    pub source: String, // <= The source of the current configuration
}

impl YamlConfig {
    fn from_str(text: &str) -> Result<Self> {
        serde_yaml::from_str(text).map_err(|e| e.into())
    }

    fn from_path(path: &Path) -> Result<Self> {
        let file = fs::open(path)?;
        let reader = BufReader::new(file);
        serde_yaml::from_reader(reader).map_err(|e| e.into())
    }

    pub fn get(env: &EnvConfig) -> Result<YamlConfig> {
        if let Some(yaml) = env.config_yaml.as_ref() {
            // We're getting the configuration from the environment variable `NAVI_CONFIG_YAML`
            let mut cfg = Self::from_str(yaml)?;
            cfg.source = "ENV_NAVI_CONFIG_YAML".to_string();

            return Ok(cfg);
        }
        if let Some(path_str) = env.config_path.as_ref() {
            // We're getting the configuration from a file given in the environment variable 'NAVI_CONFIG'

            let p = PathBuf::from(path_str);
            let mut cfg = YamlConfig::from_path(&p)?;
            cfg.source = "ENV_NAVI_CONFIG".to_string();

            return Ok(cfg);
        }
        if let Ok(p) = default_config_pathbuf() {
            // We're getting the configuration from the default path

            if p.exists() {
                let mut cfg = YamlConfig::from_path(&p)?;
                cfg.source = "DEFAULT_CONFIG_FILE".to_string();

                return Ok(cfg);
            }
        }

        // As no configuration has been found, we set the YAML configuration
        // to be its default (built-in) value.