From 03e02b9882d620e89ab6f6641b8002e7a2984da9 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 25 Mar 2023 00:40:00 -0400 Subject: [PATCH] code cleanup --- cargo | 0 src/main.rs | 90 ++++++++++++++++++++++++----------------------------- 2 files changed, 40 insertions(+), 50 deletions(-) create mode 100644 cargo diff --git a/cargo b/cargo new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 3ef065c..921577a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,9 @@ use std::{ env, fs::File, - io::{ - Read, - stdin, - }, + io::Read, iter::Peekable, + os::fd::{ FromRawFd }, path::Path, str::FromStr, }; @@ -48,21 +46,15 @@ fn parse_toml( match value { Value::Table(table) => { - match tabkey.peek() { - Some(_) => { + if tabkey.peek().is_some() { root = toml::Value::Table(table.to_owned()); continue; - }, - None => {}, // out.push_str(table.as_str()), }; }, _ => { - match tabkey.peek() { - Some(_) => { - return Err((format!("{}: Not a table.", item), EX_DATAERR)); - }, - None => {}, - }; + if tabkey.peek().is_some() { + return Err((format!("{}: Not a table.", item), EX_DATAERR)); + } }, }; @@ -111,48 +103,43 @@ fn main() { if argv.len() <= 1 { eprintln!("{}", usage_info); - exit(64); // sysexits(3) EX_USAGE + exit(EX_USAGE); } - - let input = match argv.get(2) { - Some(val) => val, - None => { - eprintln!("{}", usage_info); - exit(EX_USAGE); - }, - }; + + let input: &str; + + if let Some(arg) = argv.get(2) { + input = arg; + } else { input = &""; } + let mut content = Vec::new(); - let file = Path::new(&input); - - if input == &"-" { - match stdin().lock().read_to_end(&mut content) { - Ok(_) => {}, - Err(_) => { - eprintln!("{}: Could not read from standard input.", argv[0]); - exit(EX_OSERR); - }, - }; - } else { - match File::open(file).unwrap().read_to_end(&mut content) { - Ok(_) => {}, - Err(_) => { - eprintln!("{}: {:?}: No such file or directory.", argv[0], file); + match input { + "-" | "" => unsafe { File::from_raw_fd(0) }, + _ => { + File::open(Path::new(&input)).unwrap_or_else(|_| { + eprintln!( + "{}: {}: No such file or directory.", + argv[0], + &input + ); exit(EX_UNAVAILABLE); - }, - }; - } + }) + }, + }.read_to_end(&mut content) + .unwrap_or_else(|_| { + eprintln!("{}: Could not read input.", argv[0]); + exit(EX_OSERR); + }); + let mut tabkey: Vec<&str> = argv[1].split(".").collect(); let mut indexvec = Vec::new(); let mut index: Option = None; - match tabkey.iter().skip(1).peekable().peek() { - Some(_) => { + if tabkey.iter().skip(1).peekable().peek().is_some() { indexvec = tabkey[1].split(&['[', ']'][..]).collect(); tabkey[1] = indexvec.remove(0); - }, - None => {}, }; if ! indexvec.is_empty() { @@ -166,13 +153,16 @@ fn main() { }; } - let root = match String::from_utf8(content).unwrap().parse::() { - Ok(toml) => toml, - Err(_) => { + let root = String::from_utf8(content) + .unwrap_or_else(|_| { + eprintln!("{}: Input is not valid UTF-8.", argv[0]); + exit(EX_DATAERR); + }) + .parse::() + .unwrap_or_else(|_| { eprintln!("{}: Unable to parse TOML.", argv[0]); exit(EX_DATAERR); - }, - }; + }); let valiter = tabkey.iter().peekable(); println!(