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::{
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<usize> = 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::<Value>() {
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::<Value>()
.unwrap_or_else(|_| {
eprintln!("{}: Unable to parse TOML.", argv[0]);
exit(EX_DATAERR);
},
};
});
let valiter = tabkey.iter().peekable();
println!(