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