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]
name = "open"
version = "2.0.0"
version = "2.0.1"
authors = ["Valerie Wolfe <sleeplessval@gmail.com>"]
edition = "2018"

View file

@ -9,6 +9,8 @@ use toml::{
map::Map
};
use crate::error;
pub struct Config {
pub local: Option<Map<String, Value>>,
pub global: Option<Map<String, Value>>,
@ -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<String>;
if global.is_some() {
global_path = Some(path_global.to_str().unwrap().to_string());
} else {
global_path = None
}
let local_path: Option<String>;
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<String> =
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<Value> {
let mut output: Option<Value> = 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
}
}

View file

@ -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);
}

View file

@ -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;
}