Removes unwrap.

A rogue unwrap had been left in the code, but has now been replaced by an option.
This commit is contained in:
Thomas Hartmann 2019-10-15 00:24:32 +02:00
parent de12393eaf
commit d21389d549

View File

@ -33,12 +33,11 @@ impl WholeStreamCommand for FromSSV {
} }
} }
fn string_to_table(s: &str, headerless: bool) -> Vec<Vec<(String, String)>> { fn string_to_table(s: &str, headerless: bool) -> Option<Vec<Vec<(String, String)>>> {
let mut lines = s.lines().filter(|l| !l.trim().is_empty()); let mut lines = s.lines().filter(|l| !l.trim().is_empty());
let headers = lines let headers = lines
.next() .next()?
.unwrap()
.split_whitespace() .split_whitespace()
.map(|s| s.to_owned()) .map(|s| s.to_owned())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
@ -51,6 +50,7 @@ fn string_to_table(s: &str, headerless: bool) -> Vec<Vec<(String, String)>> {
headers headers
}; };
Some(
lines lines
.map(|l| { .map(|l| {
header_row header_row
@ -59,7 +59,8 @@ fn string_to_table(s: &str, headerless: bool) -> Vec<Vec<(String, String)>> {
.map(|(a, b)| (String::from(a), String::from(b))) .map(|(a, b)| (String::from(a), String::from(b)))
.collect() .collect()
}) })
.collect() .collect(),
)
} }
fn from_ssv_string_to_value( fn from_ssv_string_to_value(
@ -68,7 +69,7 @@ fn from_ssv_string_to_value(
tag: impl Into<Tag>, tag: impl Into<Tag>,
) -> Option<Tagged<Value>> { ) -> Option<Tagged<Value>> {
let tag = tag.into(); let tag = tag.into();
let rows = string_to_table(s, headerless) let rows = string_to_table(s, headerless)?
.iter() .iter()
.map(|row| { .map(|row| {
let mut tagged_dict = TaggedDictBuilder::new(&tag); let mut tagged_dict = TaggedDictBuilder::new(&tag);
@ -153,10 +154,10 @@ mod tests {
let result = string_to_table(input, false); let result = string_to_table(input, false);
assert_eq!( assert_eq!(
result, result,
vec![ Some(vec![
vec![owned("a", "1"), owned("b", "2")], vec![owned("a", "1"), owned("b", "2")],
vec![owned("a", "3"), owned("b", "4")] vec![owned("a", "3"), owned("b", "4")]
] ])
); );
} }
@ -170,10 +171,17 @@ mod tests {
let result = string_to_table(input, true); let result = string_to_table(input, true);
assert_eq!( assert_eq!(
result, result,
vec![ Some(vec![
vec![owned("Column1", "1"), owned("Column2", "2")], vec![owned("Column1", "1"), owned("Column2", "2")],
vec![owned("Column1", "3"), owned("Column2", "4")] vec![owned("Column1", "3"), owned("Column2", "4")]
] ])
); );
} }
#[test]
fn it_returns_none_given_an_empty_string() {
let input = "";
let result = string_to_table(input, true);
assert_eq!(result, None);
}
} }