From e2d42759dfd4b692263883d23d1d71e2b6a2eb55 Mon Sep 17 00:00:00 2001 From: Valerie Date: Sat, 17 Sep 2022 22:48:48 -0400 Subject: [PATCH] fixed subsections not working and revamped error handling --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/error.rs | 7 +++++- src/main.rs | 67 +++++++++++++++++++++++++++++----------------------- 4 files changed, 45 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73b1d99..93ba5ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,7 @@ dependencies = [ [[package]] name = "config" -version = "0.1.6" +version = "0.2.0" dependencies = [ "regex", "toml", diff --git a/Cargo.toml b/Cargo.toml index 270deed..d7e6850 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/error.rs b/src/error.rs index 473552a..500d9de 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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) { diff --git a/src/main.rs b/src/main.rs index a36ecfc..6cf816f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>{ 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>{ 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 = 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>{ } 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>{ 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 {