code cleanup

This commit is contained in:
Emma Tebibyte 2023-03-25 00:40:00 -04:00
parent 152348a17a
commit 03e02b9882
Signed by: emma
GPG Key ID: 6D661C738815E7DD
2 changed files with 40 additions and 50 deletions

0
cargo Normal file
View File

View File

@ -19,11 +19,9 @@
use std::{ use std::{
env, env,
fs::File, fs::File,
io::{ io::Read,
Read,
stdin,
},
iter::Peekable, iter::Peekable,
os::fd::{ FromRawFd },
path::Path, path::Path,
str::FromStr, str::FromStr,
}; };
@ -48,21 +46,15 @@ fn parse_toml(
match value { match value {
Value::Table(table) => { Value::Table(table) => {
match tabkey.peek() { if tabkey.peek().is_some() {
Some(_) => {
root = toml::Value::Table(table.to_owned()); root = toml::Value::Table(table.to_owned());
continue; continue;
},
None => {}, // out.push_str(table.as_str()),
}; };
}, },
_ => { _ => {
match tabkey.peek() { if tabkey.peek().is_some() {
Some(_) => { return Err((format!("{}: Not a table.", item), EX_DATAERR));
return Err((format!("{}: Not a table.", item), EX_DATAERR)); }
},
None => {},
};
}, },
}; };
@ -111,48 +103,43 @@ fn main() {
if argv.len() <= 1 { if argv.len() <= 1 {
eprintln!("{}", usage_info); eprintln!("{}", usage_info);
exit(64); // sysexits(3) EX_USAGE exit(EX_USAGE);
} }
let input = match argv.get(2) { let input: &str;
Some(val) => val,
None => { if let Some(arg) = argv.get(2) {
eprintln!("{}", usage_info); input = arg;
exit(EX_USAGE); } else { input = &""; }
},
};
let mut content = Vec::new(); let mut content = Vec::new();
let file = Path::new(&input); match input {
"-" | "" => unsafe { File::from_raw_fd(0) },
if input == &"-" { _ => {
match stdin().lock().read_to_end(&mut content) { File::open(Path::new(&input)).unwrap_or_else(|_| {
Ok(_) => {}, eprintln!(
Err(_) => { "{}: {}: No such file or directory.",
eprintln!("{}: Could not read from standard input.", argv[0]); argv[0],
exit(EX_OSERR); &input
}, );
};
} else {
match File::open(file).unwrap().read_to_end(&mut content) {
Ok(_) => {},
Err(_) => {
eprintln!("{}: {:?}: No such file or directory.", argv[0], file);
exit(EX_UNAVAILABLE); 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 tabkey: Vec<&str> = argv[1].split(".").collect();
let mut indexvec = Vec::new(); let mut indexvec = Vec::new();
let mut index: Option<usize> = None; let mut index: Option<usize> = None;
match tabkey.iter().skip(1).peekable().peek() { if tabkey.iter().skip(1).peekable().peek().is_some() {
Some(_) => {
indexvec = tabkey[1].split(&['[', ']'][..]).collect(); indexvec = tabkey[1].split(&['[', ']'][..]).collect();
tabkey[1] = indexvec.remove(0); tabkey[1] = indexvec.remove(0);
},
None => {},
}; };
if ! indexvec.is_empty() { if ! indexvec.is_empty() {
@ -166,13 +153,16 @@ fn main() {
}; };
} }
let root = match String::from_utf8(content).unwrap().parse::<Value>() { let root = String::from_utf8(content)
Ok(toml) => toml, .unwrap_or_else(|_| {
Err(_) => { eprintln!("{}: Input is not valid UTF-8.", argv[0]);
exit(EX_DATAERR);
})
.parse::<Value>()
.unwrap_or_else(|_| {
eprintln!("{}: Unable to parse TOML.", argv[0]); eprintln!("{}: Unable to parse TOML.", argv[0]);
exit(EX_DATAERR); exit(EX_DATAERR);
}, });
};
let valiter = tabkey.iter().peekable(); let valiter = tabkey.iter().peekable();
println!( println!(