Skip to content

Commit

Permalink
serde derive for QueryMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
bakaq committed Aug 19, 2024
1 parent 56232ce commit e227b99
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ ego-tree = "0.6.2"


serde_json = "1.0.122"
serde = "1.0.204"
serde = { version="1.0.204", features = ["derive"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
crossterm = { version = "0.28.1", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/machine/lib_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::parser::parser::{Parser, Tokens};
use crate::read::{write_term_to_heap, TermWriteResult};
use indexmap::IndexMap;

use super::QueryMatch;
use super::{
streams::Stream, Atom, AtomCell, HeapCellValue, HeapCellValueTag, Machine, MachineConfig,
QueryResolutionLine, QueryResult, Value,
Expand Down Expand Up @@ -138,7 +139,7 @@ impl Iterator for QueryState<'_> {
// choice point, so we should break.
self.machine.machine_st.backtrack();

Some(Ok(QueryResolutionLine::Match(bindings)))
Some(Ok(QueryResolutionLine::Match(QueryMatch { bindings })))
}
}

Expand Down
41 changes: 13 additions & 28 deletions src/machine/parsed_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,16 @@ use serde::Serializer;

pub type QueryResult = Result<QueryResolution, String>;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct QueryMatch {
pub bindings: BTreeMap<String, Value>,
}

impl Serialize for QueryMatch {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("bindings", &self.bindings)?;
map.end()
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryResolutionLine {
True,
False,
Match(BTreeMap<String, Value>),
Match(QueryMatch),
}

impl Serialize for QueryResolutionLine {
Expand All @@ -53,11 +42,7 @@ impl Serialize for QueryResolutionLine {
match self {
QueryResolutionLine::True => serializer.serialize_bool(true),
QueryResolutionLine::False => serializer.serialize_bool(false),
QueryResolutionLine::Match(m) => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("bindings", m)?;
map.end()
}
QueryResolutionLine::Match(m) => m.serialize(serializer),
}
}
}
Expand Down Expand Up @@ -142,7 +127,8 @@ impl From<&QueryResolutionLine> for Value {
QueryResolutionLine::True => Value::Atom("true".into()),
QueryResolutionLine::False => Value::Atom("false".into()),
QueryResolutionLine::Match(m) => Value::conjunction(
&m.iter()
&m.bindings
.iter()
.map(|(k, v)| {
Value::Structure("=".into(), vec![Value::Var(k.clone()), v.clone()])
})
Expand Down Expand Up @@ -580,7 +566,7 @@ impl From<Vec<QueryResolutionLine>> for QueryResolution {
// If there is only one line, and it is an empty match, return false.
if query_result_lines.len() == 1 {
if let QueryResolutionLine::Match(m) = query_result_lines[0].clone() {
if m.is_empty() {
if m.bindings.is_empty() {
return QueryResolution::False;
}
}
Expand All @@ -600,10 +586,9 @@ impl From<Vec<QueryResolutionLine>> for QueryResolution {
// If there is at least one match, return all matches.
let all_matches = query_result_lines
.into_iter()
.filter(|l| matches!(l, QueryResolutionLine::Match(_)))
.map(|l| match l {
QueryResolutionLine::Match(m) => QueryMatch::from(m),
_ => unreachable!(),
.filter_map(|l| match l {
QueryResolutionLine::Match(m) => Some(m),
_ => None,
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -743,10 +728,10 @@ mod tests {
let json_value = json!(false);
assert_eq!(json_value, serde_json::to_value(qrl).unwrap());

let qrl = QueryResolutionLine::Match(btreemap! {
"X".into() => Value::Atom("asdf".into()),
"Y".into() => Value::String("fdsa".into()),
});
let qrl = QueryResolutionLine::Match(QueryMatch::from(btreemap! {
"X" => Value::Atom("asdf".into()),
"Y" => Value::String("fdsa".into()),
}));
let json_value = json!({
"bindings": {
"X": { "atom": "asdf" },
Expand Down
1 change: 0 additions & 1 deletion tests/scryer_lib/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use scryer_prolog::machine::Machine;
use serde_json;

#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
Expand Down

0 comments on commit e227b99

Please sign in to comment.