works now, but doesnt like when i use "table." syntax

This commit is contained in:
Emma Tebibyte 2022-12-18 14:39:56 -05:00
parent a8a1284ae1
commit f4f972c5d6
1 changed files with 38 additions and 11 deletions

View File

@ -17,27 +17,54 @@
use std::env;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
use std::process;
use toml::Value;
fn main() {
let arguments: Vec<String> = env::args().collect();
let argv0 = &arguments[0];
let input = &arguments[2];
let mut arguments: Vec<String> = env::args().collect();
let argv0 = arguments.remove(0);
let input = &arguments[1];
let mut content = String::new();
let file = Path::new(&input);
let mut file = Path::new(&input);
if file.is_file() {
File::open(file).expect("").read_to_string(&mut content).unwrap();
File::open(file).unwrap().read_to_string(&mut content).unwrap();
} else { content = input.to_string(); }
let keyval: Vec<&str> = arguments[1].split(".").collect();
let keyval: Vec<&str> = arguments[0].split(".").collect();
let toml = content.parse::<Value>().unwrap();
match toml.get(keyval[1]) {
Some(value) => println!("{}", value),
None => println!("{}: {}: No such table or key.", argv0, arguments[1]),
};
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);
},
};
}
},
None => {
eprintln!("{}: {}: No such table.", argv0, keyval[0]);
process::exit(1);
},
};
}
}