From 3255f24c3a3c9414114f8186ace6816845e93d8b Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Thu, 16 Oct 2025 06:47:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(config):=20add=20ConfigRoot=20?= =?UTF-8?q?enum=20for=20flexible=20path=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - introduce ConfigRoot enum to represent different configuration file locations - implement Display trait for ConfigRoot to provide string representation - add parse method to determine ConfigRoot type based on prefix (h, r, c) --- src/client_config/config_root.rs | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/client_config/config_root.rs diff --git a/src/client_config/config_root.rs b/src/client_config/config_root.rs new file mode 100644 index 0000000..cb44274 --- /dev/null +++ b/src/client_config/config_root.rs @@ -0,0 +1,43 @@ +use std::{env, fmt::Display}; + +#[derive(Debug, Default)] +pub enum ConfigRoot { + #[default] + None, + Crate(String), + Home(String), + Root(String), +} + +impl Display for ConfigRoot { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ConfigRoot::None => write!(f, ""), + ConfigRoot::Crate(path) => write!(f, "{path}"), + ConfigRoot::Home(path) => { + let pb = path.trim_start_matches("/"); + write!(f, "{}/{}", env::home_dir().unwrap().display(), pb) + } + ConfigRoot::Root(path) => { + let pb = path.trim_start_matches("/"); + write!(f, "/{pb}") + } + } + } +} + +impl ConfigRoot { + pub fn parse(s: &str) -> Self { + if !s.is_empty() { + match s.chars().nth(0) { + Some('h') => ConfigRoot::Home(s.to_string()), + Some('r') => ConfigRoot::Root(s.to_string()), + Some('c') => ConfigRoot::Crate(s.to_string()), + Some(_) => ConfigRoot::Crate(s.to_string()), + None => ConfigRoot::None, + } + } else { + ConfigRoot::None + } + } +}