Compare commits

..

7 Commits

Author SHA1 Message Date
e6a82cfd36 Merge branch 'rust' 2023-07-13 12:32:02 -06:00
0fbe8f7733 removed redundant array code 2023-07-13 12:29:40 -06:00
f33c18a2b3 removed c-main dep 2023-04-09 22:08:32 -04:00
e3e1807656 fixed array matching and printing 2023-03-25 02:36:38 -04:00
de4b897aa8 removed accidental file 2023-03-25 00:47:30 -04:00
19e5f4e2f4 added c-main dependency 2023-03-25 00:46:37 -04:00
03e02b9882 code cleanup 2023-03-25 00:40:00 -04:00
4 changed files with 116 additions and 112 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target/

68
Cargo.lock generated
View File

@@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "bindgen" name = "bindgen"
version = "0.63.0" version = "0.63.0"
@@ -47,9 +56,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clang-sys" name = "clang-sys"
version = "1.6.0" version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ed9a53e5d4d9c573ae844bfac6872b159cb1d1585a83b29e7a64b7eef7332a" checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f"
dependencies = [ dependencies = [
"glob", "glob",
"libc", "libc",
@@ -82,9 +91,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.140" version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]] [[package]]
name = "libloading" name = "libloading"
@@ -98,12 +107,9 @@ dependencies = [
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.17" version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
@@ -129,9 +135,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.17.1" version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]] [[package]]
name = "peeking_take_while" name = "peeking_take_while"
@@ -141,36 +147,50 @@ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.53" version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba466839c78239c09faf015484e5cc04860f88242cff4d03eb038f04b4699b73" checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.26" version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.7.2" version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cce168fea28d3e05f158bda4576cf0c844d5045bc2cc3620fa0292ed5bb5814c" checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax", "regex-syntax",
] ]
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.6.29" version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
@@ -180,9 +200,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.158" version = "1.0.171"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9"
[[package]] [[package]]
name = "shlex" name = "shlex"
@@ -203,7 +223,7 @@ dependencies = [
[[package]] [[package]]
name = "tomcat" name = "tomcat"
version = "0.0.1" version = "0.1.0"
dependencies = [ dependencies = [
"toml", "toml",
"yacexits", "yacexits",
@@ -220,9 +240,9 @@ dependencies = [
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.8" version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73"
[[package]] [[package]]
name = "which" name = "which"

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "tomcat" name = "tomcat"
version = "0.0.1" version = "0.1.0"
edition = "2021" edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
authors = [ "Emma Tebibyte <emma@tebibyte.media>" ] authors = [ "Emma Tebibyte <emma@tebibyte.media>" ]

View File

@@ -17,13 +17,11 @@
*/ */
use std::{ use std::{
env, env::args,
fs::File, fs::File,
io::{ io::Read,
Read,
stdin,
},
iter::Peekable, iter::Peekable,
os::fd::{ FromRawFd },
path::Path, path::Path,
str::FromStr, str::FromStr,
}; };
@@ -39,7 +37,7 @@ fn parse_toml(
let mut out = String::new(); let mut out = String::new();
while let Some(item) = tabkey.next() { while let Some(item) = tabkey.next() {
let value = match root.get(item) { let mut value = match root.get(item) {
Some(val) => val, Some(val) => val,
None => { None => {
return Err((format!("{}: No such table or key.", item), EX_DATAERR)); return Err((format!("{}: No such table or key.", item), EX_DATAERR));
@@ -47,58 +45,50 @@ fn parse_toml(
}; };
match value { match value {
Value::Table(table) => { Value::Array(array) => {
match tabkey.peek() { let i = match index {
Some(_) => { Some(i) => i,
root = toml::Value::Table(table.to_owned()); None => {
for v in array.iter() {
out.push_str(&format!("{}\n", v.to_string()));
}
continue; continue;
}, },
None => {}, // out.push_str(table.as_str()), };
match array.get(i) {
Some(val) => value = val,
None => {
return Err((format!("No value at index {}.", i), EX_DATAERR));
},
};
},
Value::Table(table) => {
if tabkey.peek().is_some() {
root = toml::Value::Table(table.to_owned());
continue;
}; };
}, },
_ => { _ => {
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 => {},
};
}, },
}; };
match value { match value {
Value::Array(array) => { // TODO: Split Array logic into separate function Value::Array(_) => {},
let element: String; Value::Boolean(boolean) => out.push_str(&format!("{:?}\n", boolean)),
match index { Value::Datetime(datetime) => out.push_str(&format!("{:?}\n", datetime)),
Some(i) => { Value::Float(float) => out.push_str(&format!("{:?}\n", float)),
element = match array.get(i) { Value::Integer(int) => out.push_str(&format!("{:?}\n", int)),
Some(element) => { Value::String(string) => {
match element.as_str() { let contents = string.to_owned();
Some(val) => val.to_owned(), let mut lines: Vec<&str> = contents.lines().collect();
None => { if lines.last().unwrap().is_empty() { _ = lines.pop(); }
return Err((
format!("{:?}: No value at given key.", i), for line in lines.iter() { out.push_str(&format!("{}\n", line)); }
EX_DATAERR },
));
},
}
},
None => {
return Err(
(format!("{:?}: No value at given index.", i), EX_DATAERR)
);
},
};
},
None => element = format!("{:?}", array),
};
out.push_str(&element);
},
Value::Boolean(boolean) => out.push_str(&format!("{:?}", boolean)),
Value::Datetime(datetime) => out.push_str(&format!("{:?}", datetime)),
Value::Float(float) => out.push_str(&format!("{:?}", float)),
Value::Integer(int) => out.push_str(&format!("{:?}", int)),
Value::String(string) => out.push_str(string.as_str()),
_ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)), _ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)),
}; };
} }
@@ -106,53 +96,43 @@ fn parse_toml(
} }
fn main() { fn main() {
let argv: Vec<String> = env::args().collect(); let argv: Vec<String> = args().collect();
let usage_info = format!("Usage: {} [table.]key[[index]] [file...]", argv[0]); let usage_info = format!("Usage: {} [table.]key[[index]] [file...]", argv[0]);
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 = argv.get(2).unwrap_or(&"".to_owned()).to_owned();
Some(val) => val,
None => {
eprintln!("{}", usage_info);
exit(EX_USAGE);
},
};
let mut content = Vec::new(); let mut content = Vec::new();
let file = Path::new(&input); match input.as_str() {
"-" | "" => 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,16 +146,19 @@ 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!( print!(
"{}", "{}",
match parse_toml(root, valiter, index) { match parse_toml(root, valiter, index) {
Ok(val) => val, Ok(val) => val,