From 0010e073662507deb27d4bd2f8ce4431b0f8e8e3 Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 30 Jul 2024 09:18:39 -0400 Subject: [PATCH] big config struct cleanup --- Cargo.toml | 2 +- src/config.rs | 104 +++++++++++++++++++++----------------------------- src/error.rs | 10 ++--- src/main.rs | 1 - 4 files changed, 49 insertions(+), 68 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3807c9e..8101d49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open" -version = "2.0.0" +version = "2.0.1" authors = ["Valerie Wolfe "] edition = "2018" diff --git a/src/config.rs b/src/config.rs index 35e7cb7..18b90fc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,6 +9,8 @@ use toml::{ map::Map }; +use crate::error; + pub struct Config { pub local: Option>, pub global: Option>, @@ -18,81 +20,61 @@ pub struct Config { impl Config { pub fn new() -> Config { - // Instantiate global config - let i_dir_global = String::from(var("HOME").ok().unwrap()); - let dir_global = Path::new(i_dir_global.as_str()); - let i_path_global = dir_global.join(".config/open.toml"); - let path_global = i_path_global.as_path(); - let mut global = None; - if path_global.exists() { - let raw_conf = read_to_string(path_global).unwrap(); - let toml_conf: Value = toml::from_str(raw_conf.as_str()).unwrap(); - let toml = toml_conf.as_table().unwrap(); - global = Some(toml.to_owned()); - } + // initialize global config, if it exists + let str_path_global = var("HOME").unwrap() + "/.config/open.toml"; + let path_global = Path::new(&str_path_global); + let global = + if path_global.exists() { + if let Ok(raw) = read_to_string(path_global) { + if let Ok(Value::Table(toml)) = toml::from_str(&raw) { Some(toml) } + else { None } + } else { None } + } else { None }; - // Instantiate local config, if it exists. - let i_dir_local = current_dir().unwrap(); - let mut dir_local = i_dir_local.as_path(); - let mut i_path_local = dir_local.join(".open"); - let mut path_local = i_path_local.as_path(); - let root = Path::new("/"); + // propagate up for local config, if it exists + let cwd = current_dir().unwrap(); + let mut path_local = Path::new(&cwd); let mut local = None; - loop { - if dir_local == root { - break; + while let Some(parent) = path_local.parent() { + let file_local = path_local.join(".open"); + if file_local.exists() { + if let Ok(raw) = read_to_string(file_local) { + if let Ok(Value::Table(toml)) = toml::from_str(&raw) { + local = Some(toml); + break; + } + } } - if path_local.exists() { - let raw_conf = read_to_string(path_local).unwrap(); - let toml_conf: Value = toml::from_str(raw_conf.as_str()).unwrap(); - let toml = toml_conf.as_table().unwrap(); - local = Some(toml.to_owned()); - break; - } - dir_local = dir_local.parent().unwrap(); - i_path_local = dir_local.join(".open"); - path_local = i_path_local.as_path(); + + path_local = parent; } - if global.is_none() && local.is_none() { - panic!("No configuration found."); - } + if global.is_none() && local.is_none() { error::no_configs(); } // prepare path vars - let global_path: Option; - if global.is_some() { - global_path = Some(path_global.to_str().unwrap().to_string()); - } else { - global_path = None - } - let local_path: Option; - if local.is_some() { - local_path = Some(dir_local.join(".open").to_str().unwrap().to_string()); - } else { - local_path = None; - } - let output = Config { + let global_path: Option = + if global.is_some() { Some(path_global.to_string_lossy().into()) } + else { None }; + let local_path = + if local.is_some() { Some(path_local.join(".open").to_string_lossy().into()) } + else { None }; + + Config { global, local, local_path, global_path - }; - return output; + } } pub fn get(&self, key: &str) -> Option { - let mut output: Option = None; - if self.local.is_some() { - let result = self.local.as_ref().unwrap().get(key); - if result.is_some() { - output = Some(result.unwrap().to_owned()); - } + if let Some(local) = &self.local { + if let Some(result) = local.get(key) { return Some(result.to_owned()); } } - if output.is_none() && self.global.is_some() { - let result = self.global.as_ref().unwrap().get(key); - if result.is_some() { - output = Some(result.unwrap().to_owned()); - } + + if let Some(global) = &self.global { + if let Some(result) = global.get(key) { return Some(result.to_owned()); } } - return output; + + None } } diff --git a/src/error.rs b/src/error.rs index 3243bc5..0511c6f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,27 +3,27 @@ use std::{ process::exit }; -pub fn no_configs() { +pub fn no_configs() -> ! { println!("open: no configurations found"); exit(1); } -pub fn many_args() { +pub fn many_args() -> ! { println!("open: too many arguments supplied"); exit(2); } -pub fn editor_unset() { +pub fn editor_unset() -> ! { println!("open: $EDITOR is not set"); exit(3); } -pub fn not_found(path: &Path) { +pub fn not_found(path: &Path) -> ! { println!("open: {path:?} does not exist"); exit(4); } -pub fn no_section(path: &Path) { +pub fn no_section(path: &Path) -> ! { println!("open: no appropriate sections for {path:?}"); exit(5); } diff --git a/src/main.rs b/src/main.rs index cfbec89..e3e8678 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,6 @@ fn main() { if args.contains(["-p", "--path"]) { if let Some(local) = config.local_path { println!("{local}"); } else if let Some(global) = config.global_path { println!("{global}"); } - else { error::no_configs(); } return; }