better & faster parsing of toml
This commit is contained in:
parent
f4f972c5d6
commit
eaff71e721
63
src/main.rs
63
src/main.rs
@ -17,7 +17,6 @@
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use std::process;
|
||||
@ -27,44 +26,52 @@ use toml::Value;
|
||||
fn main() {
|
||||
let mut arguments: Vec<String> = env::args().collect();
|
||||
let argv0 = arguments.remove(0);
|
||||
if arguments[1].is_empty() {
|
||||
println!("Usage: {} [table...].[value] [file...]", argv0);
|
||||
process::exit(64); // sysexits(3) EX_USAGE
|
||||
}
|
||||
let input = &arguments[1];
|
||||
let mut content = String::new();
|
||||
|
||||
let mut file = Path::new(&input);
|
||||
let file = Path::new(&input);
|
||||
if file.is_file() {
|
||||
File::open(file).unwrap().read_to_string(&mut content).unwrap();
|
||||
} else { content = input.to_string(); }
|
||||
|
||||
let keyval: Vec<&str> = arguments[0].split(".").collect();
|
||||
let toml = content.parse::<Value>().unwrap();
|
||||
let tabkey: Vec<&str> = arguments[0].split(".").collect();
|
||||
let mut root = content.parse::<Value>().unwrap();
|
||||
|
||||
let mut table = &toml;
|
||||
if ! keyval[0].is_empty() {
|
||||
match toml.get(keyval[0]) {
|
||||
Some(table) => {
|
||||
if keyval[1].is_empty() {
|
||||
println!("{}", table.as_str().unwrap());
|
||||
} else {
|
||||
match table.get(keyval[1]) {
|
||||
Some(value) => {
|
||||
let out = value
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.trim_start_matches('"')
|
||||
.trim_end_matches('"');
|
||||
println!("{}", out);
|
||||
},
|
||||
None => {
|
||||
eprintln!("{}: {}: No such key.", argv0, keyval[1]);
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
}
|
||||
let mut out = String::new();
|
||||
let mut valiter = tabkey.iter().peekable();
|
||||
|
||||
while let Some(item) = valiter.next() {
|
||||
match root.get(item) {
|
||||
Some(value) => {
|
||||
match value {
|
||||
// TODO: Implement other type parsing
|
||||
Value::Array(_array) => {},
|
||||
Value::Boolean(_boolean) => {},
|
||||
Value::Datetime(_datetime) => {},
|
||||
Value::Float(_float) => {},
|
||||
Value::Integer(_int) => {},
|
||||
Value::String(string) => {
|
||||
match valiter.peek() {
|
||||
Some(next) => {
|
||||
println!("{}: {}: Not a table.", argv0, item);
|
||||
process::exit(65); // sysexits(3) EX_DATAERR
|
||||
},
|
||||
None => out.push_str(string.as_str()),
|
||||
};
|
||||
break
|
||||
}
|
||||
Value::Table(table) => root = toml::Value::Table(table.to_owned()),
|
||||
};
|
||||
},
|
||||
None => {
|
||||
eprintln!("{}: {}: No such table.", argv0, keyval[0]);
|
||||
process::exit(1);
|
||||
println!("{}: {}: No such table or key.", argv0, item);
|
||||
process::exit(65); // sysexits(3) EX_DATAERR
|
||||
},
|
||||
};
|
||||
}
|
||||
println!("{}", out);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user