Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value and QueryResolution serialization with serde #2493

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bytes = "1"
chrono = "0.4.38"
cpu-time = "1.0.0"
crrl = "0.9.0"
dashu = "0.4.2"
dashu = { version = "0.4.2", features = ["num-traits"] }
derive_more = "0.99.18"
dirs-next = "2.0.0"
divrem = "1.0.0"
Expand All @@ -71,6 +71,7 @@ ryu = "1.0.18"
sha3 = "0.10.8"
smallvec = "1.13.2"
static_assertions = "1.1.0"
num-traits = "0.2.19"

scraper = { version = "0.19.1", default-features = false, features = [
"errors",
Expand All @@ -79,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
61 changes: 9 additions & 52 deletions 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 Expand Up @@ -271,10 +272,10 @@ mod tests {
output,
Ok(QueryResolution::Matches(vec![
QueryMatch::from(btreemap! {
"P" => Value::from("p1"),
"P" => Value::String("p1".into()),
}),
QueryMatch::from(btreemap! {
"P" => Value::from("p2"),
"P" => Value::String("p2".into()),
}),
]))
);
Expand Down Expand Up @@ -425,10 +426,10 @@ mod tests {
output,
Ok(QueryResolution::Matches(vec![
QueryMatch::from(btreemap! {
"P" => Value::from("p1"),
"P" => Value::String("p1".into()),
}),
QueryMatch::from(btreemap! {
"P" => Value::from("p2"),
"P" => Value::String("p2".into()),
}),
]))
);
Expand Down Expand Up @@ -463,50 +464,6 @@ mod tests {
);
}

#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
#[ignore = "uses old flawed interface"]
fn integration_test() {
let mut machine = Machine::new_lib();

// File with test commands, i.e. program code to consult and queries to run
let code = include_str!("./lib_integration_test_commands.txt");

// Split the code into blocks
let blocks = code.split("=====");

let mut i = 0;
let mut last_result: Option<_> = None;
// Iterate over the blocks
for block in blocks {
// Trim the block to remove any leading or trailing whitespace
let block = block.trim();

// Skip empty blocks
if block.is_empty() {
continue;
}

// Check if the block is a query
if let Some(query) = block.strip_prefix("query") {
// Parse and execute the query
let result = machine.run_query(query.to_string());
assert!(result.is_ok());

last_result = Some(result);
} else if let Some(code) = block.strip_prefix("consult") {
// Load the code into the machine
machine.consult_module_string("facts", code.to_string());
} else if let Some(result) = block.strip_prefix("result") {
i += 1;
if let Some(Ok(ref last_result)) = last_result {
println!("\n\n=====Result No. {i}=======\n{last_result}\n===============");
assert_eq!(last_result.to_string(), result.to_string().trim(),)
}
}
}
}

#[test]
#[cfg_attr(miri, ignore = "it takes too long to run")]
fn findall() {
Expand All @@ -531,8 +488,8 @@ mod tests {
btreemap! {
"Result" => Value::List(
Vec::from([
Value::List([Value::from("p1"), Value::from("b")].into()),
Value::List([Value::from("p2"), Value::from("b")].into()),
Value::List([Value::String("p1".into()), Value::String("b".into())].into()),
Value::List([Value::String("p2".into()), Value::String("b".into())].into()),
])
),
}
Expand Down Expand Up @@ -690,7 +647,7 @@ mod tests {
let result = machine.run_query(query);

let expected = Value::Structure(
// Composite term
// Compound term
"a".into(),
vec![
Value::String("asdf".into()), // String
Expand Down
Loading
Loading