Skip to content

Commit

Permalink
evaluate a very basic query
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtrilling committed Jan 15, 2022
1 parent 2745169 commit bc6ebcd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
28 changes: 28 additions & 0 deletions rust/src/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::parse::{Expression, Reference, Value};

impl<'a> Expression<'a> {
pub fn evaluate(&'a self, context: &serde_json::Value) -> Result<serde_json::Value, String> {
match self {
Expression::Value(value) => value.evaluate(&context),
_ => Err("Unknown expression type".to_string()),
}
}
}

impl<'a> Value<'a> {
pub fn evaluate(&'a self, context: &serde_json::Value) -> Result<serde_json::Value, String> {
match self {
Value::Reference(reference) => reference.evaluate(&context),
_ => Err("Unknown value type".to_string()),
}
}
}

impl<'a> Reference<'a> {
pub fn evaluate(&'a self, context: &'a serde_json::Value) -> Result<serde_json::Value, String> {
match self {
Reference::At => Ok(context.clone()),
_ => Err("Unknown value type".to_string()),
}
}
}
16 changes: 9 additions & 7 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
pub mod eval;
pub mod parse;

pub fn query_value(
query_str: String,
_data: serde_json::Value,
) -> Result<serde_json::Value, &'static str> {
let ast = parse::expr(&query_str);
dbg!(ast);
Err("unimplemented")
data: serde_json::Value,
) -> Result<serde_json::Value, String> {
match parse::expr(&query_str) {
Ok((_, ast)) => ast.evaluate(&data),
Err(err) => Err(err.to_string()),
}
}

pub fn query(query_str: String, data_str: String) -> Result<serde_json::Value, &'static str> {
pub fn query(query_str: String, data_str: String) -> Result<serde_json::Value, String> {
match serde_json::from_str(&data_str) {
Ok(data) => query_value(query_str, data),
Err(_) => Err("Invalid JSON data"),
Err(_) => Err("Invalid JSON data".to_string()),
}
}
2 changes: 1 addition & 1 deletion rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() {
let query = "count @".to_string();
let query = "@".to_string();
let data = r#"
{ "foo": "bar" }
"#
Expand Down

0 comments on commit bc6ebcd

Please sign in to comment.