From 3e6597a23ef5160bdc024f8def6eb7f18d7628b5 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Wed, 12 Mar 2025 00:46:13 +0100 Subject: [PATCH] Added an entrypoint in the configuration to understand where it was loaded from Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- src/config/mod.rs | 5 +++++ src/config/yaml.rs | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 40633fe..50fbcb3 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -222,4 +222,9 @@ impl Config { None } } + + // Returns the current configuration source + pub fn get_source(&self) -> String { + self.yaml.source.clone() + } } diff --git a/src/config/yaml.rs b/src/config/yaml.rs index 796d3f6..5548231 100644 --- a/src/config/yaml.rs +++ b/src/config/yaml.rs @@ -5,6 +5,7 @@ use crate::finder::FinderChoice; use crate::prelude::*; use crossterm::style::Color as TerminalColor; use serde::de; +use crate::common::fs::ToStringExt; #[derive(Deserialize, Debug)] pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor); @@ -86,7 +87,7 @@ pub struct Client { pub tealdeer: bool, } -#[derive(Deserialize, Default, Debug)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct YamlConfig { pub style: Style, @@ -95,6 +96,7 @@ pub struct YamlConfig { pub search: Search, pub shell: Shell, pub client: Client, + pub source: String, // <= The source of the current configuration } impl YamlConfig { @@ -108,19 +110,36 @@ impl YamlConfig { serde_yaml::from_reader(reader).map_err(|e| e.into()) } - pub fn get(env: &EnvConfig) -> Result { + pub fn get(env: &EnvConfig) -> Result { if let Some(yaml) = env.config_yaml.as_ref() { - return Self::from_str(yaml); + // 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); - return YamlConfig::from_path(&p); + 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() { - return YamlConfig::from_path(&p); + 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()) } } @@ -172,3 +191,18 @@ impl Default for Shell { } } } + + +impl Default for YamlConfig { + fn default() -> Self { + Self { + style: Default::default(), + finder: Default::default(), + cheats: Default::default(), + search: Default::default(), + shell: Default::default(), + client: Default::default(), + source: "built-in".to_string(), + } + } +}