diff --git a/src/expr/mod.rs b/src/expr/mod.rs index c79e99e..0f0e5c3 100644 --- a/src/expr/mod.rs +++ b/src/expr/mod.rs @@ -2,9 +2,14 @@ use {Function, Functions, Context, Contexts, Compiled, Value}; use tree::Tree; use error::Error; -use serde::Serialize; use to_value; -use std::fmt; +use std::{fmt, cmp}; + +use serde::de::{self, Deserialize}; +use serde::ser::Serialize; + +use serde::Deserializer; +use serde::Serializer; /// Expression builder @@ -61,7 +66,8 @@ impl Expr { } } - fn get_compiled(&self) -> Option<&Compiled> { + /// Get reference to compiled object + pub fn get_compiled(&self) -> Option<&Compiled> { self.compiled.as_ref() } } @@ -88,6 +94,31 @@ impl fmt::Debug for Expr { } } +impl cmp::PartialEq for Expr { + fn eq(&self, other: &Expr) -> bool { + self.expression == other.expression + } +} + +impl Serialize for Expr { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(format!("{:?}", self).as_str()) + } +} + +impl<'de> Deserialize<'de> for Expr { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + String::deserialize(deserializer) + .and_then(|expr| Expr::new(expr).compile().map_err(de::Error::custom)) + } +} + /// Execute options pub struct ExecOptions<'a> {