From: Nicolas Luck Date: Thu, 20 Jul 2023 20:31:20 +0000 (+0200) Subject: type QueryResult = Result X-Git-Tag: remove^2~56 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=7c93450aa7f88475b2382407acfb20958c04fdf7;p=scryer-prolog.git type QueryResult = Result --- diff --git a/src/machine/lib_machine.rs b/src/machine/lib_machine.rs index 674a459e..f88577ce 100644 --- a/src/machine/lib_machine.rs +++ b/src/machine/lib_machine.rs @@ -1,17 +1,17 @@ -use super::{Machine, MachineConfig, QueryResult, QueryResultLine, Atom}; +use super::{Machine, MachineConfig, QueryResult, QueryResolution, QueryResolutionLine, Atom}; impl Machine { pub fn new_lib() -> Self { Machine::new(MachineConfig::in_memory().with_toplevel(include_str!("../lib_toplevel.pl"))) } - pub fn run_query(&mut self, query: String) -> Result { + pub fn run_query(&mut self, query: String) -> QueryResult { self.set_user_input(query); self.run_top_level(atom!("$toplevel"), (atom!("run_input_once"), 0)); self.parse_output() } - pub fn parse_output(&self) -> Result { + pub fn parse_output(&self) -> QueryResult { let output = self.get_user_output().trim().to_string(); if output.starts_with("error(") { Err(output) @@ -21,9 +21,9 @@ impl Machine { .map(|s| s.trim()) .map(|s| s.replace(".", "")) .filter(|s| !s.is_empty()) - .map(QueryResultLine::try_from) + .map(QueryResolutionLine::try_from) .filter_map(Result::ok) - .collect::>() + .collect::>() .into()) } } @@ -52,7 +52,7 @@ mod tests { let output = machine.run_query(query); assert_eq!( output, - Ok(QueryResult::Matches(vec![ + Ok(QueryResolution::Matches(vec![ QueryMatch::from(btreemap! { "P" => Value::from("p1"), }), @@ -64,12 +64,12 @@ mod tests { assert_eq!( machine.run_query(String::from(r#"triple("a","p1","b")."#)), - Ok(QueryResult::True) + Ok(QueryResolution::True) ); assert_eq!( machine.run_query(String::from(r#"triple("x","y","z")."#)), - Ok(QueryResult::False) + Ok(QueryResolution::False) ); } diff --git a/src/machine/parsed_results.rs b/src/machine/parsed_results.rs index 329042a2..f34eadea 100644 --- a/src/machine/parsed_results.rs +++ b/src/machine/parsed_results.rs @@ -3,8 +3,10 @@ use ordered_float::OrderedFloat; use rug::*; use std::collections::BTreeMap; +pub type QueryResult = Result; + #[derive(Debug, Clone, PartialEq, Eq)] -pub enum QueryResult { +pub enum QueryResolution { True, False, Matches(Vec), @@ -16,7 +18,7 @@ pub struct QueryMatch { } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum QueryResultLine { +pub enum QueryResolutionLine { True, False, Match(BTreeMap), @@ -51,13 +53,13 @@ impl From> for QueryMatch { } } -impl From> for QueryResult { - fn from(query_result_lines: Vec) -> Self { +impl From> for QueryResolution { + fn from(query_result_lines: Vec) -> Self { // If there is only one line, and it is true or false, return that. if query_result_lines.len() == 1 { match query_result_lines[0].clone() { - QueryResultLine::True => return QueryResult::True, - QueryResultLine::False => return QueryResult::False, + QueryResolutionLine::True => return QueryResolution::True, + QueryResolutionLine::False => return QueryResolution::False, _ => {} } } @@ -65,49 +67,49 @@ impl From> for QueryResult { // If there is at least one line with true and no matches, return true. if query_result_lines .iter() - .any(|l| l == &QueryResultLine::True) + .any(|l| l == &QueryResolutionLine::True) && !query_result_lines.iter().any(|l| { - if let &QueryResultLine::Match(_) = l { + if let &QueryResolutionLine::Match(_) = l { true } else { false } }) { - return QueryResult::True; + return QueryResolution::True; } // If there is at least one match, return all matches. let all_matches = query_result_lines .into_iter() .filter(|l| { - if let &QueryResultLine::Match(_) = l { + if let &QueryResolutionLine::Match(_) = l { true } else { false } }) .map(|l| match l { - QueryResultLine::Match(m) => QueryMatch::from(m), + QueryResolutionLine::Match(m) => QueryMatch::from(m), _ => unreachable!(), }) .collect::>(); if !all_matches.is_empty() { - return QueryResult::Matches(all_matches); + return QueryResolution::Matches(all_matches); } - QueryResult::False + QueryResolution::False } } -impl TryFrom for QueryResultLine { +impl TryFrom for QueryResolutionLine { type Error = (); fn try_from(string: String) -> Result { match string.as_str() { - "true" => Ok(QueryResultLine::True), - "false" => Ok(QueryResultLine::False), - _ => Ok(QueryResultLine::Match( + "true" => Ok(QueryResolutionLine::True), + "false" => Ok(QueryResolutionLine::False), + _ => Ok(QueryResolutionLine::Match( string .split(",") .map(|s| s.trim())