Changes from_ssv_to_string_value to return an Option.

This commit is contained in:
Thomas Hartmann 2019-10-14 07:37:34 +02:00
parent 20e891db6e
commit 63039666b0

View file

@ -37,12 +37,11 @@ fn from_ssv_string_to_value(
s: &str, s: &str,
headerless: bool, headerless: bool,
tag: impl Into<Tag>, tag: impl Into<Tag>,
) -> Result<Tagged<Value>, &str> { ) -> Option<Tagged<Value>> {
let mut lines = s.lines(); let mut lines = s.lines();
let headers = lines let headers = lines
.next() .next()?
.expect("No content.")
.split_whitespace() .split_whitespace()
.map(|s| s.to_owned()) .map(|s| s.to_owned())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
@ -69,7 +68,7 @@ fn from_ssv_string_to_value(
}) })
.collect(); .collect();
Ok(Tagged::from_item(Value::Table(rows), tag)) Some(Tagged::from_item(Value::Table(rows), tag))
} }
fn from_ssv( fn from_ssv(
@ -101,13 +100,13 @@ fn from_ssv(
} }
match from_ssv_string_to_value(&concat_string, headerless, name) { match from_ssv_string_to_value(&concat_string, headerless, name) {
Ok(x) => match x { Some(x) => match x {
Tagged { item: Value::Table(list), ..} => { Tagged { item: Value::Table(list), ..} => {
for l in list { yield ReturnSuccess::value(l) } for l in list { yield ReturnSuccess::value(l) }
} }
x => yield ReturnSuccess::value(x) x => yield ReturnSuccess::value(x)
}, },
Err(_) => if let Some(last_tag) = latest_tag { None => if let Some(last_tag) = latest_tag {
yield Err(ShellError::labeled_error_with_secondary( yield Err(ShellError::labeled_error_with_secondary(
"Could not parse as SSV", "Could not parse as SSV",
"input cannot be parsed ssv", "input cannot be parsed ssv",
@ -115,7 +114,7 @@ fn from_ssv(
"value originates from here", "value originates from here",
last_tag, last_tag,
)) ))
} },
} }
}; };