-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2745169
commit bc6ebcd
Showing
3 changed files
with
38 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
"# | ||
|