arrays!!!!!

This commit is contained in:
Emma Tebibyte 2022-12-19 13:51:28 -05:00
parent eb76b0b30d
commit 3ec60dbc04
1 changed files with 59 additions and 9 deletions

View File

@ -20,6 +20,7 @@ use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::process;
use std::str::FromStr;
use toml::Value;
@ -27,7 +28,7 @@ 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);
eprintln!("Usage: {} [table...].[value] [file...]", argv0);
process::exit(64); // sysexits(3) EX_USAGE
}
let input = &arguments[1];
@ -38,7 +39,22 @@ fn main() {
File::open(file).unwrap().read_to_string(&mut content).unwrap();
} else { content = input.to_string(); }
let tabkey: Vec<&str> = arguments[0].split(".").collect();
let mut tabkey: Vec<&str> = arguments[0].split(".").collect();
let mut indexvec: Vec<&str> = tabkey[1].split(&['[', ']'][..]).collect();
tabkey[1] = indexvec.remove(0);
let mut index: usize = 0;
if ! indexvec.is_empty() {
let istr = indexvec.remove(0);
match usize::from_str(istr) {
Ok(i) => index = i,
Err(_) => {
eprintln!("{}: {}: Cannot index by given value.", argv0, istr);
process::exit(65); // sysexits(3) EX_DATAERR
},
};
}
let mut root = content.parse::<Value>().unwrap();
let mut out = String::new();
@ -49,26 +65,60 @@ fn main() {
Some(value) => {
match value {
// TODO: Implement other type parsing
Value::Array(_array) => {},
Value::Array(array) => {
match valiter.peek() {
Some(_) => {
eprintln!("{}: {}: Not a table.", argv0, item);
process::exit(65); // sysexits(3) EX_DATAERR
},
None => {
match array.get(index) {
Some(element) => {
match element.as_str() {
Some(val) => out.push_str(val),
None => {
eprintln!(
"{}: {:?}: No value at given index.", argv0, index
);
process::exit(65); // sysexits(3) EX_DATAERR
},
};
},
None => {
eprintln!(
"{}: {:?}: No value at given index.", argv0, index
);
process::exit(65); // sysexits(3) EX_DATAERR
},
};
},
};
},
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);
Some(_) => {
eprintln!("{}: {}: 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()),
},
Value::Table(table) => {
match valiter.peek() {
Some(_) => {
root = toml::Value::Table(table.to_owned());
},
None => {}, // out.push_str(table.as_str()),
};
},
};
},
None => {
println!("{}: {}: No such table or key.", argv0, item);
eprintln!("{}: {}: No such table or key.", argv0, item);
process::exit(65); // sysexits(3) EX_DATAERR
},
};