Compare commits
8 Commits
088824efdf
...
0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fbe8f7733 | |||
|
f33c18a2b3
|
|||
|
e3e1807656
|
|||
|
de4b897aa8
|
|||
|
19e5f4e2f4
|
|||
|
03e02b9882
|
|||
|
152348a17a
|
|||
|
7fc82adc1c
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target/
|
||||
76
Cargo.lock
generated
76
Cargo.lock
generated
@@ -2,6 +2,15 @@
|
||||
# It is not intended for manual editing.
|
||||
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]]
|
||||
name = "bindgen"
|
||||
version = "0.63.0"
|
||||
@@ -47,9 +56,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clang-sys"
|
||||
version = "1.6.0"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77ed9a53e5d4d9c573ae844bfac6872b159cb1d1585a83b29e7a64b7eef7332a"
|
||||
checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f"
|
||||
dependencies = [
|
||||
"glob",
|
||||
"libc",
|
||||
@@ -82,9 +91,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.138"
|
||||
version = "0.2.147"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
|
||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
@@ -98,12 +107,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.17"
|
||||
version = "0.4.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
@@ -129,9 +135,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.17.1"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
|
||||
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
|
||||
|
||||
[[package]]
|
||||
name = "peeking_take_while"
|
||||
@@ -141,36 +147,50 @@ checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.51"
|
||||
version = "1.0.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6"
|
||||
checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.23"
|
||||
version = "1.0.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b"
|
||||
checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.7.1"
|
||||
version = "1.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
|
||||
checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575"
|
||||
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",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.28"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
||||
checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
@@ -180,9 +200,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.148"
|
||||
version = "1.0.171"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e53f64bb4ba0191d6d0676e1b141ca55047d83b74f5607e6d8eb88126c52c2dc"
|
||||
checksum = "30e27d1e4fd7659406c492fd6cfaf2066ba8773de45ca75e855590f856dc34a9"
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
@@ -203,7 +223,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tomcat"
|
||||
version = "0.0.1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"toml",
|
||||
"yacexits",
|
||||
@@ -211,18 +231,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.9"
|
||||
version = "0.5.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
|
||||
checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.6"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
|
||||
checksum = "22049a19f4a68748a168c0fc439f9516686aa045927ff767eca0a85101fb6e73"
|
||||
|
||||
[[package]]
|
||||
name = "which"
|
||||
@@ -259,9 +279,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "yacexits"
|
||||
version = "0.1.3"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3afbe270dff95fe94b3a55c7e2dce91457a89b2b0dc6013814bba9806d099be"
|
||||
checksum = "53fe740dd05c1bbc919431e842e6c1bea30195e0518ae99cae35b7f0730ddc18"
|
||||
dependencies = [
|
||||
"bindgen",
|
||||
"libc",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "tomcat"
|
||||
version = "0.0.1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0-or-later"
|
||||
authors = [ "Emma Tebibyte <emma@tebibyte.media>" ]
|
||||
|
||||
160
src/main.rs
160
src/main.rs
@@ -17,13 +17,11 @@
|
||||
*/
|
||||
|
||||
use std::{
|
||||
env,
|
||||
env::args,
|
||||
fs::File,
|
||||
io::{
|
||||
Read,
|
||||
stdin,
|
||||
},
|
||||
io::Read,
|
||||
iter::Peekable,
|
||||
os::fd::{ FromRawFd },
|
||||
path::Path,
|
||||
str::FromStr,
|
||||
};
|
||||
@@ -39,7 +37,7 @@ fn parse_toml(
|
||||
let mut out = String::new();
|
||||
|
||||
while let Some(item) = tabkey.next() {
|
||||
let value = match root.get(item) {
|
||||
let mut value = match root.get(item) {
|
||||
Some(val) => val,
|
||||
None => {
|
||||
return Err((format!("{}: No such table or key.", item), EX_DATAERR));
|
||||
@@ -47,58 +45,50 @@ fn parse_toml(
|
||||
};
|
||||
|
||||
match value {
|
||||
Value::Table(table) => {
|
||||
match tabkey.peek() {
|
||||
Some(_) => {
|
||||
root = toml::Value::Table(table.to_owned());
|
||||
Value::Array(array) => {
|
||||
let i = match index {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
for v in array.iter() {
|
||||
out.push_str(&format!("{}\n", v.to_string()));
|
||||
}
|
||||
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() {
|
||||
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));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
match value {
|
||||
Value::Array(array) => { // TODO: Split Array logic into separate function
|
||||
let element: String;
|
||||
match index {
|
||||
Some(i) => {
|
||||
element = match array.get(i) {
|
||||
Some(element) => {
|
||||
match element.as_str() {
|
||||
Some(val) => val.to_owned(),
|
||||
None => {
|
||||
return Err((
|
||||
format!("{:?}: No value at given key.", i),
|
||||
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()),
|
||||
Value::Array(_) => {},
|
||||
Value::Boolean(boolean) => out.push_str(&format!("{:?}\n", boolean)),
|
||||
Value::Datetime(datetime) => out.push_str(&format!("{:?}\n", datetime)),
|
||||
Value::Float(float) => out.push_str(&format!("{:?}\n", float)),
|
||||
Value::Integer(int) => out.push_str(&format!("{:?}\n", int)),
|
||||
Value::String(string) => {
|
||||
let contents = string.to_owned();
|
||||
let mut lines: Vec<&str> = contents.lines().collect();
|
||||
if lines.last().unwrap().is_empty() { _ = lines.pop(); }
|
||||
|
||||
for line in lines.iter() { out.push_str(&format!("{}\n", line)); }
|
||||
},
|
||||
_ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)),
|
||||
};
|
||||
}
|
||||
@@ -106,52 +96,43 @@ fn parse_toml(
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
if argv.len() <= 1 {
|
||||
eprintln!("Usage: {} [table...].[value[index]] [file...]", argv[0]);
|
||||
exit(64); // sysexits(3) EX_USAGE
|
||||
eprintln!("{}", usage_info);
|
||||
exit(EX_USAGE);
|
||||
}
|
||||
|
||||
let input = match argv.get(2) {
|
||||
Some(val) => val,
|
||||
None => {
|
||||
eprintln!("Usage: {} [table...].[value[index]] [file...]", argv[0]);
|
||||
exit(EX_USAGE);
|
||||
},
|
||||
};
|
||||
|
||||
let input = argv.get(2).unwrap_or(&"".to_owned()).to_owned();
|
||||
|
||||
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.as_str() {
|
||||
"-" | "" => 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() {
|
||||
@@ -165,16 +146,19 @@ 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!(
|
||||
print!(
|
||||
"{}",
|
||||
match parse_toml(root, valiter, index) {
|
||||
Ok(val) => val,
|
||||
|
||||
Reference in New Issue
Block a user