fmt machine/parsed_results.rs

This commit is contained in:
Nicolas Luck
2023-07-17 21:35:00 +02:00
parent 5f8cc3c64b
commit e7f1e32ee3

View File

@@ -1,8 +1,7 @@
use crate::atom_table::*;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use rug::*; use rug::*;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::atom_table::*;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryResult { pub enum QueryResult {
@@ -13,7 +12,7 @@ pub enum QueryResult {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueryMatch { pub struct QueryMatch {
pub bindings: BTreeMap<String, Value> pub bindings: BTreeMap<String, Value>,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -38,18 +37,17 @@ pub enum Value {
impl From<BTreeMap<&str, Value>> for QueryMatch { impl From<BTreeMap<&str, Value>> for QueryMatch {
fn from(bindings: BTreeMap<&str, Value>) -> Self { fn from(bindings: BTreeMap<&str, Value>) -> Self {
QueryMatch { QueryMatch {
bindings: bindings.into_iter() bindings: bindings
.into_iter()
.map(|(k, v)| (k.to_string(), v)) .map(|(k, v)| (k.to_string(), v))
.collect::<BTreeMap<_, _>>() .collect::<BTreeMap<_, _>>(),
} }
} }
} }
impl From<BTreeMap<String, Value>> for QueryMatch { impl From<BTreeMap<String, Value>> for QueryMatch {
fn from(bindings: BTreeMap<String, Value>) -> Self { fn from(bindings: BTreeMap<String, Value>) -> Self {
QueryMatch { QueryMatch { bindings }
bindings
}
} }
} }
@@ -65,26 +63,34 @@ impl From<Vec<QueryResultLine>> for QueryResult {
} }
// If there is at least one line with true and no matches, return true. // If there is at least one line with true and no matches, return true.
if query_result_lines.iter().any(|l| l == &QueryResultLine::True) if query_result_lines
.iter()
.any(|l| l == &QueryResultLine::True)
&& !query_result_lines.iter().any(|l| { && !query_result_lines.iter().any(|l| {
if let &QueryResultLine::Match(_) = l { true } else { false } if let &QueryResultLine::Match(_) = l {
}) { true
} else {
false
}
})
{
return QueryResult::True; return QueryResult::True;
} }
// If there is at least one match, return all matches. // If there is at least one match, return all matches.
let all_matches = query_result_lines.into_iter() let all_matches = query_result_lines
.into_iter()
.filter(|l| { .filter(|l| {
if let &QueryResultLine::Match(_) = l { true } else { false } if let &QueryResultLine::Match(_) = l {
}) true
.map(|l| { } else {
match l { false
QueryResultLine::Match(m) => {
QueryMatch::from(m)
},
_ => unreachable!()
} }
}) })
.map(|l| match l {
QueryResultLine::Match(m) => QueryMatch::from(m),
_ => unreachable!(),
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if !all_matches.is_empty() { if !all_matches.is_empty() {
@@ -95,7 +101,6 @@ impl From<Vec<QueryResultLine>> for QueryResult {
} }
} }
impl TryFrom<String> for QueryResultLine { impl TryFrom<String> for QueryResultLine {
type Error = (); type Error = ();
fn try_from(string: String) -> Result<Self, Self::Error> { fn try_from(string: String) -> Result<Self, Self::Error> {
@@ -103,10 +108,11 @@ impl TryFrom<String> for QueryResultLine {
"true" => Ok(QueryResultLine::True), "true" => Ok(QueryResultLine::True),
"false" => Ok(QueryResultLine::False), "false" => Ok(QueryResultLine::False),
_ => Ok(QueryResultLine::Match( _ => Ok(QueryResultLine::Match(
string.split(",") string
.split(",")
.map(|s| s.trim()) .map(|s| s.trim())
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
.map(|s| -> Result<(String, Value), ()>{ .map(|s| -> Result<(String, Value), ()> {
let mut iter = s.split(" = "); let mut iter = s.split(" = ");
let key = iter.next().unwrap().to_string(); let key = iter.next().unwrap().to_string();
@@ -115,8 +121,8 @@ impl TryFrom<String> for QueryResultLine {
Ok((key, Value::try_from(value)?)) Ok((key, Value::try_from(value)?))
}) })
.filter_map(Result::ok) .filter_map(Result::ok)
.collect::<BTreeMap<_, _>>() .collect::<BTreeMap<_, _>>(),
)) )),
} }
} }
} }
@@ -128,8 +134,7 @@ impl TryFrom<String> for Value {
if trimmed.starts_with("'") && trimmed.ends_with("'") { if trimmed.starts_with("'") && trimmed.ends_with("'") {
Ok(Value::String(trimmed[1..trimmed.len() - 1].into())) Ok(Value::String(trimmed[1..trimmed.len() - 1].into()))
} else } else if trimmed.starts_with("\"") && trimmed.ends_with("\"") {
if trimmed.starts_with("\"") && trimmed.ends_with("\"") {
Ok(Value::String(trimmed[1..trimmed.len() - 1].into())) Ok(Value::String(trimmed[1..trimmed.len() - 1].into()))
} else if trimmed.starts_with("[") && trimmed.ends_with("]") { } else if trimmed.starts_with("[") && trimmed.ends_with("]") {
let mut iter = trimmed[1..trimmed.len() - 1].split(","); let mut iter = trimmed[1..trimmed.len() - 1].split(",");
@@ -181,4 +186,4 @@ impl From<&str> for Value {
fn from(str: &str) -> Self { fn from(str: &str) -> Self {
Value::String(str.to_string()) Value::String(str.to_string())
} }
} }