diff --git a/src/main.rs b/src/main.rs index fb00f65..23b9cdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,27 +17,54 @@ use std::env; use std::fs::File; +use std::io; use std::io::Read; use std::path::Path; +use std::process; use toml::Value; fn main() { - let arguments: Vec = env::args().collect(); - let argv0 = &arguments[0]; - let input = &arguments[2]; + let mut arguments: Vec = env::args().collect(); + let argv0 = arguments.remove(0); + let input = &arguments[1]; let mut content = String::new(); - let file = Path::new(&input); + let mut file = Path::new(&input); if file.is_file() { - File::open(file).expect("").read_to_string(&mut content).unwrap(); + File::open(file).unwrap().read_to_string(&mut content).unwrap(); } else { content = input.to_string(); } - let keyval: Vec<&str> = arguments[1].split(".").collect(); + let keyval: Vec<&str> = arguments[0].split(".").collect(); let toml = content.parse::().unwrap(); - - match toml.get(keyval[1]) { - Some(value) => println!("{}", value), - None => println!("{}: {}: No such table or key.", argv0, arguments[1]), - }; + + let mut table = &toml; + if ! keyval[0].is_empty() { + match toml.get(keyval[0]) { + Some(table) => { + if keyval[1].is_empty() { + println!("{}", table.as_str().unwrap()); + } else { + match table.get(keyval[1]) { + Some(value) => { + let out = value + .as_str() + .unwrap() + .trim_start_matches('"') + .trim_end_matches('"'); + println!("{}", out); + }, + None => { + eprintln!("{}: {}: No such key.", argv0, keyval[1]); + process::exit(1); + }, + }; + } + }, + None => { + eprintln!("{}: {}: No such table.", argv0, keyval[0]); + process::exit(1); + }, + }; + } }