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]
name = "eval"
version = "0.3.2"
version = "0.4.0"
description = "Expression evaluator"
keywords = ["expression", "evaluate", "evaluator", "expr", "template"]
authors = ["fengcen <fengcen.love@gmail.com>"]
@ -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]

View File

@ -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`:

View File

@ -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)),
}),
}
}

View File

@ -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)
}

View File

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

View File

@ -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;

View File

@ -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);