update serde to version 0.9, bump version 0.4.0

This commit is contained in:
fengcen 2017-02-13 08:20:53 +08:00
parent 1c4d522007
commit ce671d0f0a
8 changed files with 52 additions and 45 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "eval" name = "eval"
version = "0.3.2" version = "0.4.0"
description = "Expression evaluator" description = "Expression evaluator"
keywords = ["expression", "evaluate", "evaluator", "expr", "template"] keywords = ["expression", "evaluate", "evaluator", "expr", "template"]
authors = ["fengcen <fengcen.love@gmail.com>"] authors = ["fengcen <fengcen.love@gmail.com>"]
@ -15,8 +15,8 @@ name = "eval"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
serde = "^0.8" serde = "^0.9"
serde_json = "^0.8" serde_json = "^0.9"
quick-error = "^1.1" quick-error = "^1.1"
[features] [features]

View File

@ -2,7 +2,7 @@
eval 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) [![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. Eval is a powerful expression evaluator.
@ -23,7 +23,7 @@ Add dependency to Cargo.toml
```toml ```toml
[dependencies] [dependencies]
eval = "^0.3" eval = "^0.4"
``` ```
In your `main.rs` or `lib.rs`: In your `main.rs` or `lib.rs`:

View File

@ -82,14 +82,12 @@ fn create_is_empty_fuction() -> Function {
Function { Function {
max_args: Some(1), max_args: Some(1),
min_args: Some(1), min_args: Some(1),
compiled: Box::new(|values| { compiled: Box::new(|values| match *values.first().unwrap() {
match *values.first().unwrap() { Value::String(ref string) => Ok(to_value(string.is_empty())),
Value::String(ref string) => Ok(to_value(string.is_empty())), Value::Array(ref array) => Ok(to_value(array.is_empty())),
Value::Array(ref array) => Ok(to_value(array.is_empty())), Value::Object(ref object) => Ok(to_value(object.is_empty())),
Value::Object(ref object) => Ok(to_value(object.is_empty())), Value::Null => Ok(to_value(true)),
Value::Null => Ok(to_value(true)), _ => Ok(to_value(false)),
_ => Ok(to_value(false)),
}
}), }),
} }
} }

View File

@ -4,90 +4,90 @@ use operator::Operator;
quick_error! { quick_error! {
/// Expression parsing error /// Expression parsing error
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
/// Unsupported operator yet. /// Unsupported operator yet.
UnsupportedOperator(operator: String) { UnsupportedOperator(operator: String) {
display("Unsupported operator: {:?}", operator) display("Unsupported operator: {:?}", operator)
} }
/// This operator does not support execution. /// This operator does not support execution.
CanNotExec(operator: Operator) { CanNotExec(operator: Operator) {
display("This operator does not support execution: {:?}", 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 { StartWithNonValueOperator {
display("Your expression may start with non-value operator like ( + * ).") 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 { UnpairedBrackets {
display("Unpaired brackets, left brackets count does not equal right brackets count.") 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 { DuplicateValueNode {
display("Duplicate values node, you may have (2 3) but there is no operators between them.") 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 { DuplicateOperatorNode {
display("Duplicate operators node, you may have (+ +) but there is no values between them.") 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 { CommaNotWithFunction {
display("You have a comma(,) , but there is no function in front of it.") 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 { BracketNotWithFunction {
display("You have empty brackets () , but there is no function in front of it.") display("You have empty brackets () , but there is no function in front of it.")
} }
/// Function not exists. /// Function not exists.
FunctionNotExists(ident: String) { FunctionNotExists(ident: String) {
display("Function not exists: {}", ident) 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) { ExpectedBoolean(value: Value) {
display("Expected a boolean, found: {}", value) display("Expected a boolean, found: {}", value)
} }
/// Expected ident. /// Expected ident.
ExpectedIdentifier { ExpectedIdentifier {
display("Expected ident.") display("Expected ident.")
} }
/// Expected array. /// Expected array.
ExpectedArray { ExpectedArray {
display("Expected array.") display("Expected array.")
} }
/// Expected object. /// Expected object.
ExpectedObject { ExpectedObject {
display("Expected object.") display("Expected object.")
} }
/// Expect number. /// Expect number.
ExpectedNumber { ExpectedNumber {
display("Expected number.") display("Expected number.")
} }
/// Failed to parse, no final expression. /// Failed to parse, no final expression.
NoFinalNode { NoFinalNode {
display("Failed to parse, no final expression.") 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) { ArgumentsGreater(max: usize) {
display("The number of arguments is greater than the maximum limit: {}", max) 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) { ArgumentsLess(min: usize) {
display("The number of arguments is less than the minimum limit: {}", min) 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) { UnsupportedTypes(a: String, b: String) {
display("This two value types are different or do not support mathematical calculations: {}, {}", a, b) 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) { InvalidRange(ident: String) {
display("Invalid range expression: {}", ident) display("Invalid range expression: {}", ident)
} }
/// Can not add child node. /// Can not add child node.
CanNotAddChild { CanNotAddChild {
display("Can not add child node.") display("Can not add child node.")
} }
/// Custom error. /// Custom error.
Custom(detail: String) { Custom(detail: String) {
display("{}", detail) display("{}", detail)
} }

View File

@ -109,11 +109,19 @@ mod builtin;
mod expr; mod expr;
pub use expr::ExecOptions; pub use expr::ExecOptions;
pub use serde_json::{Value, to_value}; pub use serde_json::Value;
pub use error::Error; pub use error::Error;
pub use function::Function; pub use function::Function;
pub use expr::Expr; pub use expr::Expr;
use std::collections::HashMap; 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<S: Serialize>(v: S) -> Value {
json_to_value(v).unwrap()
}
/// Custom context. /// Custom context.
pub type Context = HashMap<String, Value>; pub type Context = HashMap<String, Value>;
@ -134,7 +142,7 @@ type Compiled = Box<Fn(&[Context], &Functions) -> Result<Value, Error>>;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::to_value; use to_value;
use error::Error; use error::Error;
use Expr; use Expr;
use tree::Tree; use tree::Tree;

View File

@ -1,5 +1,6 @@
use serde_json::{Value, to_value}; use serde_json::Value;
use to_value;
use error::Error; use error::Error;
pub trait Math { pub trait Math {
@ -162,9 +163,7 @@ trait Type {
impl Type for Value { impl Type for Value {
fn get_f64(&self) -> f64 { fn get_f64(&self) -> f64 {
match *self { match *self {
Value::F64(f) => f, Value::Number(ref n) => n.as_f64().unwrap(),
Value::I64(f) => f as f64,
Value::U64(f) => f as f64,
_ => panic!("not a number"), _ => panic!("not a number"),
} }
} }

View File

@ -1,6 +1,7 @@
use std::str::FromStr; use std::str::FromStr;
use serde_json::{Value, to_value}; use serde_json::Value;
use to_value;
use error::Error; use error::Error;
use node::Node; use node::Node;

View File

@ -1,7 +1,8 @@
use std::str::FromStr; use std::str::FromStr;
use std::clone::Clone; use std::clone::Clone;
use serde_json::{Value, to_value}; use serde_json::Value;
use to_value;
use math::Math; use math::Math;
use operator::Operator; use operator::Operator;
use node::Node; use node::Node;
@ -368,7 +369,7 @@ impl Tree {
if child.operator.is_identifier() { if child.operator.is_identifier() {
value = value.as_ref() value = value.as_ref()
.unwrap() .unwrap()
.find(child.operator.get_identifier()) .get(child.operator.get_identifier())
.cloned(); .cloned();
} else { } else {
return Err(Error::ExpectedIdentifier); return Err(Error::ExpectedIdentifier);
@ -405,7 +406,7 @@ impl Tree {
if name.is_string() { if name.is_string() {
value = value.as_ref() value = value.as_ref()
.unwrap() .unwrap()
.find(name.as_str().unwrap()) .get(name.as_str().unwrap())
.cloned(); .cloned();
} else { } else {
return Err(Error::ExpectedIdentifier); return Err(Error::ExpectedIdentifier);