Error handling
This commit is contained in:
@@ -5,23 +5,27 @@ impl Machine {
|
||||
Machine::new(MachineConfig::in_memory().with_toplevel(include_str!("../lib_toplevel.pl")))
|
||||
}
|
||||
|
||||
pub fn run_query(&mut self, query: String) -> QueryResult {
|
||||
pub fn run_query(&mut self, query: String) -> Result<QueryResult, String> {
|
||||
self.set_user_input(query);
|
||||
self.run_top_level(atom!("$toplevel"), (atom!("run_input_once"), 0));
|
||||
self.parse_output()
|
||||
}
|
||||
|
||||
pub fn parse_output(&self) -> QueryResult {
|
||||
let output = self.get_user_output();
|
||||
output
|
||||
.split(";")
|
||||
.map(|s| s.trim())
|
||||
.map(|s| s.replace(".", ""))
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(QueryResultLine::try_from)
|
||||
.filter_map(Result::ok)
|
||||
.collect::<Vec<QueryResultLine>>()
|
||||
.into()
|
||||
pub fn parse_output(&self) -> Result<QueryResult, String> {
|
||||
let output = self.get_user_output().trim().to_string();
|
||||
if output.starts_with("error(") {
|
||||
Err(output)
|
||||
} else {
|
||||
Ok(output
|
||||
.split(";")
|
||||
.map(|s| s.trim())
|
||||
.map(|s| s.replace(".", ""))
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(QueryResultLine::try_from)
|
||||
.filter_map(Result::ok)
|
||||
.collect::<Vec<QueryResultLine>>()
|
||||
.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,24 +52,35 @@ mod tests {
|
||||
let output = machine.run_query(query);
|
||||
assert_eq!(
|
||||
output,
|
||||
QueryResult::Matches(vec![
|
||||
Ok(QueryResult::Matches(vec![
|
||||
QueryMatch::from(btreemap! {
|
||||
"P" => Value::from("p1"),
|
||||
}),
|
||||
QueryMatch::from(btreemap! {
|
||||
"P" => Value::from("p2"),
|
||||
}),
|
||||
])
|
||||
]))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
machine.run_query(String::from(r#"triple("a","p1","b")."#)),
|
||||
QueryResult::True
|
||||
Ok(QueryResult::True)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
machine.run_query(String::from(r#"triple("x","y","z")."#)),
|
||||
QueryResult::False
|
||||
Ok(QueryResult::False)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failing_query() {
|
||||
let mut machine = Machine::new_lib();
|
||||
let query = String::from(r#"triple("a",P,"b")."#);
|
||||
let output = machine.run_query(query);
|
||||
assert_eq!(
|
||||
output,
|
||||
Err(String::from("error(existence_error(procedure,triple/3),triple/3)."))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user