fixed subsections not working and revamped error handling

This commit is contained in:
Valerie Wolfe 2022-09-17 22:48:48 -04:00
parent e1f0270d6f
commit e2d42759df
4 changed files with 45 additions and 33 deletions

2
Cargo.lock generated
View file

@ -13,7 +13,7 @@ dependencies = [
[[package]] [[package]]
name = "config" name = "config"
version = "0.1.6" version = "0.2.0"
dependencies = [ dependencies = [
"regex", "regex",
"toml", "toml",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "config" name = "config"
version = "0.1.6" version = "0.2.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -3,9 +3,14 @@ use std::{
process::exit process::exit
}; };
pub fn no_args() {
println!("config: no arguments suppied");
exit(1);
}
pub fn missing_config(path: &PathBuf) { pub fn missing_config(path: &PathBuf) {
println!("config: no config file found at {:?}", path); println!("config: no config file found at {:?}", path);
exit(1); exit(2);
} }
pub fn no_section(section: &String) { pub fn no_section(section: &String) {

View file

@ -4,15 +4,16 @@ use std::{
fs::read_to_string, fs::read_to_string,
path::PathBuf, path::PathBuf,
process::{ process::{
exit,
Command, Command,
Stdio Stdio
} }
}; };
use regex::Regex; use regex::Regex;
use toml::Value; use toml::{
Value,
value::Map
};
mod error; mod error;
@ -39,10 +40,9 @@ fn main() -> Result<(), Box<dyn Error>>{
let mut dir = false; let mut dir = false;
let mut list = false; let mut list = false;
let mut path = false; let mut path = false;
{
if args.len() == 0 { if args.len() == 0 {
help_text(); error::no_args();
exit(2);
} }
match args[0].as_str() { match args[0].as_str() {
"-d" | "-d" |
@ -58,7 +58,8 @@ fn main() -> Result<(), Box<dyn Error>>{
} }
_ => {} _ => {}
} }
}
let start_index = if dir || list || path { 1 } else { 0 };
if list { if list {
let mut section = config; let mut section = config;
@ -81,17 +82,23 @@ fn main() -> Result<(), Box<dyn Error>>{
return Ok(()); return Ok(());
} }
let entry = &args[args.len() - 1]; let mut section: &Map<String, Value> = config;
let i_section = config.get(entry); let mut fullname: String = "".to_string();
for i in start_index..args.len() {
let entry = &args[i];
fullname = if fullname.is_empty() { args[i].clone() } else { fullname + "." + &args[i] };
let i_section = section.get(entry);
if i_section.is_none() { if i_section.is_none() {
error::no_section(entry); error::no_section(&fullname);
} }
let section = i_section.unwrap().as_table().unwrap(); section = i_section.unwrap().as_table().unwrap();
}
//let section = i_section.unwrap().as_table().unwrap();
let prop_path = section.get("path"); let prop_path = section.get("path");
if dir { if dir {
if prop_path.is_none() { if prop_path.is_none() {
error::no_property(entry, "path"); error::no_property(&fullname, "path");
} }
let raw_path = prop_path.unwrap().as_str().unwrap(); let raw_path = prop_path.unwrap().as_str().unwrap();
let expanded = home_regex.replace(raw_path, &home_dir); let expanded = home_regex.replace(raw_path, &home_dir);
@ -108,7 +115,7 @@ fn main() -> Result<(), Box<dyn Error>>{
} }
if path { if path {
if prop_path.is_none() { if prop_path.is_none() {
error::no_property(entry, "path"); error::no_property(&fullname, "path");
} }
let i_file_path = prop_path.unwrap().as_str().unwrap(); let i_file_path = prop_path.unwrap().as_str().unwrap();
let file_path = home_regex.replace(i_file_path, home_dir); let file_path = home_regex.replace(i_file_path, home_dir);
@ -120,7 +127,7 @@ fn main() -> Result<(), Box<dyn Error>>{
let raw_command: String; let raw_command: String;
if prop_command.is_none() { if prop_command.is_none() {
if editor.is_none() { if editor.is_none() {
error::no_property(entry, "command"); error::no_property(&fullname, "command");
} }
raw_command = editor.unwrap(); raw_command = editor.unwrap();
} else { } else {