diff --git a/Cargo.toml b/Cargo.toml index df17bbd..7a77d58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "eval" -version = "0.3.2" +version = "0.4.0" description = "Expression evaluator" keywords = ["expression", "evaluate", "evaluator", "expr", "template"] authors = ["fengcen "] @@ -15,8 +15,8 @@ name = "eval" path = "src/lib.rs" [dependencies] -serde = "^0.8" -serde_json = "^0.8" +serde = "^0.9" +serde_json = "^0.9" quick-error = "^1.1" [features] diff --git a/README.md b/README.md index db58025..676a559 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ eval ==== [![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active) -[![docs](https://docs.rs/eval/badge.svg?version=0.3.2 "docs")](https://docs.rs/eval) +[![docs](https://docs.rs/eval/badge.svg?version=0.4.0 "docs")](https://docs.rs/eval) Eval is a powerful expression evaluator. @@ -23,7 +23,7 @@ Add dependency to Cargo.toml ```toml [dependencies] -eval = "^0.3" +eval = "^0.4" ``` In your `main.rs` or `lib.rs`: diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index a158b6d..b985fc8 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -82,14 +82,12 @@ fn create_is_empty_fuction() -> Function { Function { max_args: Some(1), min_args: Some(1), - compiled: Box::new(|values| { - match *values.first().unwrap() { - Value::String(ref string) => Ok(to_value(string.is_empty())), - Value::Array(ref array) => Ok(to_value(array.is_empty())), - Value::Object(ref object) => Ok(to_value(object.is_empty())), - Value::Null => Ok(to_value(true)), - _ => Ok(to_value(false)), - } + compiled: Box::new(|values| match *values.first().unwrap() { + Value::String(ref string) => Ok(to_value(string.is_empty())), + Value::Array(ref array) => Ok(to_value(array.is_empty())), + Value::Object(ref object) => Ok(to_value(object.is_empty())), + Value::Null => Ok(to_value(true)), + _ => Ok(to_value(false)), }), } } diff --git a/src/error/mod.rs b/src/error/mod.rs index f53895b..0be42d1 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -4,90 +4,90 @@ use operator::Operator; quick_error! { -/// Expression parsing error + /// Expression parsing error #[derive(Debug, PartialEq)] pub enum Error { -/// Unsupported operator yet. + /// Unsupported operator yet. UnsupportedOperator(operator: String) { display("Unsupported operator: {:?}", operator) } -/// This operator does not support execution. + /// This operator does not support execution. CanNotExec(operator: Operator) { display("This operator does not support execution: {:?}", operator) } -/// Your expression may start with non-value operator like ( + * ) + /// Your expression may start with non-value operator like ( + * ) StartWithNonValueOperator { display("Your expression may start with non-value operator like ( + * ).") } -/// Unpaired brackets, left brackets count does not equal right brackets count + /// Unpaired brackets, left brackets count does not equal right brackets count UnpairedBrackets { display("Unpaired brackets, left brackets count does not equal right brackets count.") } -/// Duplicate values node, you may have (2 3) but there is no operators between them + /// Duplicate values node, you may have (2 3) but there is no operators between them DuplicateValueNode { display("Duplicate values node, you may have (2 3) but there is no operators between them.") } -/// Duplicate operators node, you may have (+ +) but there is no values between them + /// Duplicate operators node, you may have (+ +) but there is no values between them DuplicateOperatorNode { display("Duplicate operators node, you may have (+ +) but there is no values between them.") } -/// You have a comma(,) , but there is no function in front of it. + /// You have a comma(,) , but there is no function in front of it. CommaNotWithFunction { display("You have a comma(,) , but there is no function in front of it.") } -/// You have empty brackets () , but there is no function in front of it. + /// You have empty brackets () , but there is no function in front of it. BracketNotWithFunction { display("You have empty brackets () , but there is no function in front of it.") } -/// Function not exists. + /// Function not exists. FunctionNotExists(ident: String) { display("Function not exists: {}", ident) } -/// Expected a boolean but the given value isn't. + /// Expected a boolean but the given value isn't. ExpectedBoolean(value: Value) { display("Expected a boolean, found: {}", value) } -/// Expected ident. + /// Expected ident. ExpectedIdentifier { display("Expected ident.") } -/// Expected array. + /// Expected array. ExpectedArray { display("Expected array.") } -/// Expected object. + /// Expected object. ExpectedObject { display("Expected object.") } -/// Expect number. + /// Expect number. ExpectedNumber { display("Expected number.") } -/// Failed to parse, no final expression. + /// Failed to parse, no final expression. NoFinalNode { display("Failed to parse, no final expression.") } -/// The number of arguments is greater than the maximum limit. + /// The number of arguments is greater than the maximum limit. ArgumentsGreater(max: usize) { display("The number of arguments is greater than the maximum limit: {}", max) } -/// The number of arguments is less than the minimum limit. + /// The number of arguments is less than the minimum limit. ArgumentsLess(min: usize) { display("The number of arguments is less than the minimum limit: {}", min) } -/// This two value types are different or do not support mathematical calculations. + /// This two value types are different or do not support mathematical calculations. UnsupportedTypes(a: String, b: String) { display("This two value types are different or do not support mathematical calculations: {}, {}", a, b) } -/// Invalid range expression like `1..2..3` + /// Invalid range expression like `1..2..3` InvalidRange(ident: String) { display("Invalid range expression: {}", ident) } -/// Can not add child node. + /// Can not add child node. CanNotAddChild { display("Can not add child node.") } -/// Custom error. + /// Custom error. Custom(detail: String) { display("{}", detail) } diff --git a/src/lib.rs b/src/lib.rs index 1bc8793..aaff616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,11 +109,19 @@ mod builtin; mod expr; pub use expr::ExecOptions; -pub use serde_json::{Value, to_value}; +pub use serde_json::Value; pub use error::Error; pub use function::Function; pub use expr::Expr; + use std::collections::HashMap; +use serde_json::to_value as json_to_value; +use serde::Serialize; + +/// Convert variable to `serde_json::Value` +pub fn to_value(v: S) -> Value { + json_to_value(v).unwrap() +} /// Custom context. pub type Context = HashMap; @@ -134,7 +142,7 @@ type Compiled = Box Result>; #[cfg(test)] mod tests { - use serde_json::to_value; + use to_value; use error::Error; use Expr; use tree::Tree; diff --git a/src/math/mod.rs b/src/math/mod.rs index fe4a306..304604f 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1,5 +1,6 @@ -use serde_json::{Value, to_value}; +use serde_json::Value; +use to_value; use error::Error; pub trait Math { @@ -162,9 +163,7 @@ trait Type { impl Type for Value { fn get_f64(&self) -> f64 { match *self { - Value::F64(f) => f, - Value::I64(f) => f as f64, - Value::U64(f) => f as f64, + Value::Number(ref n) => n.as_f64().unwrap(), _ => panic!("not a number"), } } diff --git a/src/operator/mod.rs b/src/operator/mod.rs index acb8bf5..d23423d 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -1,6 +1,7 @@ use std::str::FromStr; -use serde_json::{Value, to_value}; +use serde_json::Value; +use to_value; use error::Error; use node::Node; diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 2724b9d..0494c50 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -1,7 +1,8 @@ use std::str::FromStr; use std::clone::Clone; -use serde_json::{Value, to_value}; +use serde_json::Value; +use to_value; use math::Math; use operator::Operator; use node::Node; @@ -368,7 +369,7 @@ impl Tree { if child.operator.is_identifier() { value = value.as_ref() .unwrap() - .find(child.operator.get_identifier()) + .get(child.operator.get_identifier()) .cloned(); } else { return Err(Error::ExpectedIdentifier); @@ -405,7 +406,7 @@ impl Tree { if name.is_string() { value = value.as_ref() .unwrap() - .find(name.as_str().unwrap()) + .get(name.as_str().unwrap()) .cloned(); } else { return Err(Error::ExpectedIdentifier);