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]]
name = "config"
version = "0.1.6"
version = "0.2.0"
dependencies = [
"regex",
"toml",

View file

@ -1,6 +1,6 @@
[package]
name = "config"
version = "0.1.6"
version = "0.2.0"
edition = "2021"
# 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
};
pub fn no_args() {
println!("config: no arguments suppied");
exit(1);
}
pub fn missing_config(path: &PathBuf) {
println!("config: no config file found at {:?}", path);
exit(1);
exit(2);
}
pub fn no_section(section: &String) {

View file

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