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

Code before:

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.
        Ok(YamlConfig::default())
    }
}

impl Default for ColorWidth {
    fn default() -> Self {
        Self {
            color: Color(TerminalColor::Blue),
            width_percentage: 26,

Review comment (bug):
```suggestion
            cfg.source = format!("{}={}", env_var::CONFIG, p.to_string());
```
`NAVI_CONFIG_YAML` is typo of `NAVI_CONFIG` and it is better to use `env_var::CONFIG` that is used in `env.config_path`.
It is better to avoid using `{:?}` for anything other than debugging.
Unnecessary `to_string()`.

Code after:

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.
        Ok(YamlConfig::default())
    }
}

impl Default for ColorWidth {
    fn default() -> Self {
        Self {
            color: Color(TerminalColor::Blue),
            width_percentage: 26,