fixed array matching and printing
This commit is contained in:
parent
de4b897aa8
commit
e3e1807656
71
src/main.rs
71
src/main.rs
@ -39,7 +39,7 @@ fn parse_toml(
|
|||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
|
|
||||||
while let Some(item) = tabkey.next() {
|
while let Some(item) = tabkey.next() {
|
||||||
let value = match root.get(item) {
|
let mut value = match root.get(item) {
|
||||||
Some(val) => val,
|
Some(val) => val,
|
||||||
None => {
|
None => {
|
||||||
return Err((format!("{}: No such table or key.", item), EX_DATAERR));
|
return Err((format!("{}: No such table or key.", item), EX_DATAERR));
|
||||||
@ -47,6 +47,24 @@ fn parse_toml(
|
|||||||
};
|
};
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
match array.get(i) {
|
||||||
|
Some(val) => value = val,
|
||||||
|
None => {
|
||||||
|
return Err((format!("No value at index {}.", i), EX_DATAERR));
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
Value::Table(table) => {
|
Value::Table(table) => {
|
||||||
if tabkey.peek().is_some() {
|
if tabkey.peek().is_some() {
|
||||||
root = toml::Value::Table(table.to_owned());
|
root = toml::Value::Table(table.to_owned());
|
||||||
@ -61,38 +79,20 @@ fn parse_toml(
|
|||||||
};
|
};
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
Value::Array(array) => { // TODO: Split Array logic into separate function
|
Value::Array(array) => {
|
||||||
let element: String;
|
for v in array.iter() { out.push_str(&format!("{}\n", v.to_string())); }
|
||||||
match index {
|
},
|
||||||
Some(i) => {
|
Value::Boolean(boolean) => out.push_str(&format!("{:?}\n", boolean)),
|
||||||
element = match array.get(i) {
|
Value::Datetime(datetime) => out.push_str(&format!("{:?}\n", datetime)),
|
||||||
Some(element) => {
|
Value::Float(float) => out.push_str(&format!("{:?}\n", float)),
|
||||||
match element.as_str() {
|
Value::Integer(int) => out.push_str(&format!("{:?}\n", int)),
|
||||||
Some(val) => val.to_owned(),
|
Value::String(string) => {
|
||||||
None => {
|
let contents = string.to_owned();
|
||||||
return Err((
|
let mut lines: Vec<&str> = contents.lines().collect();
|
||||||
format!("{:?}: No value at given key.", i),
|
if lines.last().unwrap().is_empty() { _ = lines.pop(); }
|
||||||
EX_DATAERR
|
|
||||||
));
|
for line in lines.iter() { out.push_str(&format!("{}\n", line)); }
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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()),
|
|
||||||
_ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)),
|
_ => return Err((format!("{:?}: No such key.", item), EX_DATAERR)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -125,11 +125,10 @@ fn rust_main(args: Args) {
|
|||||||
exit(EX_UNAVAILABLE);
|
exit(EX_UNAVAILABLE);
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
}.read_to_end(&mut content)
|
}.read_to_end(&mut content).unwrap_or_else(|_| {
|
||||||
.unwrap_or_else(|_| {
|
|
||||||
eprintln!("{}: Could not read input.", argv[0]);
|
eprintln!("{}: Could not read input.", argv[0]);
|
||||||
exit(EX_OSERR);
|
exit(EX_OSERR);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
let mut tabkey: Vec<&str> = argv[1].split(".").collect();
|
let mut tabkey: Vec<&str> = argv[1].split(".").collect();
|
||||||
@ -164,7 +163,7 @@ fn rust_main(args: Args) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let valiter = tabkey.iter().peekable();
|
let valiter = tabkey.iter().peekable();
|
||||||
println!(
|
print!(
|
||||||
"{}",
|
"{}",
|
||||||
match parse_toml(root, valiter, index) {
|
match parse_toml(root, valiter, index) {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
|
Loading…
Reference in New Issue
Block a user