fixed subsections not working and revamped error handling
This commit is contained in:
parent
e1f0270d6f
commit
e2d42759df
4 changed files with 45 additions and 33 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -13,7 +13,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "config"
|
||||
version = "0.1.6"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
"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
|
||||
|
|
|
@ -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) {
|
||||
|
|
35
src/main.rs
35
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,10 +40,9 @@ 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);
|
||||
error::no_args();
|
||||
}
|
||||
match args[0].as_str() {
|
||||
"-d" |
|
||||
|
@ -58,7 +58,8 @@ fn main() -> Result<(), Box<dyn Error>>{
|
|||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
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(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");
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue