big config struct cleanup

This commit is contained in:
Valerie Wolfe 2024-07-30 09:18:39 -04:00
parent 3bea615b52
commit 0010e07366
4 changed files with 49 additions and 68 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "open" name = "open"
version = "2.0.0" version = "2.0.1"
authors = ["Valerie Wolfe <sleeplessval@gmail.com>"] authors = ["Valerie Wolfe <sleeplessval@gmail.com>"]
edition = "2018" edition = "2018"

View file

@ -9,6 +9,8 @@ use toml::{
map::Map map::Map
}; };
use crate::error;
pub struct Config { pub struct Config {
pub local: Option<Map<String, Value>>, pub local: Option<Map<String, Value>>,
pub global: Option<Map<String, Value>>, pub global: Option<Map<String, Value>>,
@ -18,81 +20,61 @@ pub struct Config {
impl Config { impl Config {
pub fn new() -> Config { pub fn new() -> Config {
// Instantiate global config // initialize global config, if it exists
let i_dir_global = String::from(var("HOME").ok().unwrap()); let str_path_global = var("HOME").unwrap() + "/.config/open.toml";
let dir_global = Path::new(i_dir_global.as_str()); let path_global = Path::new(&str_path_global);
let i_path_global = dir_global.join(".config/open.toml"); let global =
let path_global = i_path_global.as_path();
let mut global = None;
if path_global.exists() { if path_global.exists() {
let raw_conf = read_to_string(path_global).unwrap(); if let Ok(raw) = read_to_string(path_global) {
let toml_conf: Value = toml::from_str(raw_conf.as_str()).unwrap(); if let Ok(Value::Table(toml)) = toml::from_str(&raw) { Some(toml) }
let toml = toml_conf.as_table().unwrap(); else { None }
global = Some(toml.to_owned()); } else { None }
} } else { None };
// Instantiate local config, if it exists. // propagate up for local config, if it exists
let i_dir_local = current_dir().unwrap(); let cwd = current_dir().unwrap();
let mut dir_local = i_dir_local.as_path(); let mut path_local = Path::new(&cwd);
let mut i_path_local = dir_local.join(".open");
let mut path_local = i_path_local.as_path();
let root = Path::new("/");
let mut local = None; let mut local = None;
loop { while let Some(parent) = path_local.parent() {
if dir_local == root { 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; 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();
} }
if global.is_none() && local.is_none() { path_local = parent;
panic!("No configuration found.");
} }
if global.is_none() && local.is_none() { error::no_configs(); }
// prepare path vars // prepare path vars
let global_path: Option<String>; let global_path: Option<String> =
if global.is_some() { if global.is_some() { Some(path_global.to_string_lossy().into()) }
global_path = Some(path_global.to_str().unwrap().to_string()); else { None };
} else { let local_path =
global_path = None if local.is_some() { Some(path_local.join(".open").to_string_lossy().into()) }
} else { None };
let local_path: Option<String>;
if local.is_some() { Config {
local_path = Some(dir_local.join(".open").to_str().unwrap().to_string());
} else {
local_path = None;
}
let output = Config {
global, global,
local, local,
local_path, local_path,
global_path global_path
}; }
return output;
} }
pub fn get(&self, key: &str) -> Option<Value> { pub fn get(&self, key: &str) -> Option<Value> {
let mut output: Option<Value> = None; if let Some(local) = &self.local {
if self.local.is_some() { if let Some(result) = local.get(key) { return Some(result.to_owned()); }
let result = self.local.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()); }
} }
if output.is_none() && self.global.is_some() {
let result = self.global.as_ref().unwrap().get(key); None
if result.is_some() {
output = Some(result.unwrap().to_owned());
}
}
return output;
} }
} }

View file

@ -3,27 +3,27 @@ use std::{
process::exit process::exit
}; };
pub fn no_configs() { pub fn no_configs() -> ! {
println!("open: no configurations found"); println!("open: no configurations found");
exit(1); exit(1);
} }
pub fn many_args() { pub fn many_args() -> ! {
println!("open: too many arguments supplied"); println!("open: too many arguments supplied");
exit(2); exit(2);
} }
pub fn editor_unset() { pub fn editor_unset() -> ! {
println!("open: $EDITOR is not set"); println!("open: $EDITOR is not set");
exit(3); exit(3);
} }
pub fn not_found(path: &Path) { pub fn not_found(path: &Path) -> ! {
println!("open: {path:?} does not exist"); println!("open: {path:?} does not exist");
exit(4); exit(4);
} }
pub fn no_section(path: &Path) { pub fn no_section(path: &Path) -> ! {
println!("open: no appropriate sections for {path:?}"); println!("open: no appropriate sections for {path:?}");
exit(5); exit(5);
} }

View file

@ -37,7 +37,6 @@ fn main() {
if args.contains(["-p", "--path"]) { if args.contains(["-p", "--path"]) {
if let Some(local) = config.local_path { println!("{local}"); } if let Some(local) = config.local_path { println!("{local}"); }
else if let Some(global) = config.global_path { println!("{global}"); } else if let Some(global) = config.global_path { println!("{global}"); }
else { error::no_configs(); }
return; return;
} }