]> Repositorios git - scryer-prolog.git/commitdiff
Don't panic when parsing results fails
authorNicolas Luck <[email protected]>
Thu, 20 Jul 2023 19:34:43 +0000 (21:34 +0200)
committerNicolas Luck <[email protected]>
Thu, 20 Jul 2023 19:34:43 +0000 (21:34 +0200)
src/machine/parsed_results.rs

index 7938106a3d49b0ea96307ff2d0d22196e82aed98..329042a22e450f8a9f771f38a461aea316a9fbb7 100644 (file)
@@ -114,9 +114,8 @@ impl TryFrom<String> 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<String> 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))