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>
This commit is contained in:
alexis-opolka 2025-03-12 00:46:13 +01:00
parent 47c9bbaa62
commit 3e6597a23e
2 changed files with 44 additions and 5 deletions

View file

@ -222,4 +222,9 @@ impl Config {
None
}
}
// Returns the current configuration source
pub fn get_source(&self) -> String {
self.yaml.source.clone()
}
}

View file

@ -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<Self> {
pub fn get(env: &EnvConfig) -> Result<YamlConfig> {
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(),
}
}
}