From 836f6c1d5b0944debdd0a1ce22da50e75c0046d8 Mon Sep 17 00:00:00 2001 From: Nicolas Luck Date: Thu, 20 Jul 2023 21:34:43 +0200 Subject: [PATCH] Don't panic when parsing results fails --- src/machine/parsed_results.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 7938106a..329042a2 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -114,9 +114,8 @@ impl TryFrom for QueryResultLine { .filter(|s| !s.is_empty()) .map(|s| -> Result<(String, Value), ()> { let mut iter = s.split(" = "); - - let key = iter.next().unwrap().to_string(); - let value = iter.next().unwrap().to_string(); + let key = iter.next().ok_or(())?.to_string(); + let value = iter.next().ok_or(())?.to_string(); Ok((key, Value::try_from(value)?)) }) @@ -148,31 +147,29 @@ impl TryFrom for Value { Ok(Value::List(values)) } else if trimmed.starts_with("{") && trimmed.ends_with("}") { let mut iter = trimmed[1..trimmed.len() - 1].split(","); - let mut values = vec![]; while let Some(value) = iter.next() { - let mut iter = value.split(":"); - - let _key = iter.next().unwrap().to_string(); - let value = iter.next().unwrap().to_string(); - - values.push(Value::try_from(value)?); + let items: Vec<_> = value.split(":").collect(); + if items.len() == 2 { + let _key = items[0].to_string(); + let value = items[1].to_string(); + values.push(Value::try_from(value)?); + } } Ok(Value::Structure(atom!("{}"), values)) } else if trimmed.starts_with("<<") && trimmed.ends_with(">>") { let mut iter = trimmed[2..trimmed.len() - 2].split(","); - let mut values = vec![]; while let Some(value) = iter.next() { - let mut iter = value.split(":"); - - let _key = iter.next().unwrap().to_string(); - let value = iter.next().unwrap().to_string(); - - values.push(Value::try_from(value)?); + let items: Vec<_> = value.split(":").collect(); + if items.len() == 2 { + let _key = items[0].to_string(); + let value = items[1].to_string(); + values.push(Value::try_from(value)?); + } } Ok(Value::Structure(atom!("<<>>"), values)) -- 2.54.0