diff --git a/Cargo.toml b/Cargo.toml index 9bd44fa..f6013e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,10 @@ path = "src/lib.rs" [dependencies] serde = { version = "1", optional = true} +serde_derive = { version = "1", optional = true} [features] +serde_support = ["serde", "serde_derive"] [dev-dependencies] ron = "0.4" diff --git a/README.md b/README.md index 523084d..b75b010 100644 --- a/README.md +++ b/README.md @@ -318,12 +318,12 @@ Functions have a precedence of 190. ### [Serde](https://serde.rs) -To use this crate with serde, the serde feature flag has to be set. +To use this crate with serde, the `serde_support` feature flag has to be set. This can be done like this in the `Cargo.toml`: ```toml [dependencies] -evalexpr = {version = "3", features = ["serde"]} +evalexpr = {version = "3", features = ["serde_support"]} ``` This crate implements `serde::de::Deserialize` for its type `Node` that represents a parsed expression tree. @@ -349,6 +349,9 @@ match ron::de::from_str::(serialized_free) { With `serde`, expressions can be integrated into arbitrarily complex data. +The crate also implements `Serialize` and `Deserialize` for the `HashMapContext`. +But note that only the variables get serialized, not the functions. + ## License This crate is primarily distributed under the terms of the MIT license. diff --git a/src/context/mod.rs b/src/context/mod.rs index 9edc734..8ab9c99 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -1,9 +1,9 @@ use std::collections::HashMap; -use function::Function; -use value::value_type::ValueType; use EvalexprError; use EvalexprResult; +use function::Function; +use value::value_type::ValueType; use crate::value::Value; @@ -49,8 +49,11 @@ impl Context for EmptyContext { /// *Value and function mappings are stored independently, meaning that there can be a function and a value with the same identifier.* /// /// This context is type-safe, meaning that an identifier that is assigned a value of some type once cannot be assigned a value of another type. +#[derive(Debug)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub struct HashMapContext { variables: HashMap, + #[cfg_attr(feature = "serde_support", serde(skip))] functions: HashMap, } diff --git a/src/function/mod.rs b/src/function/mod.rs index 745a88a..b861adb 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -1,3 +1,5 @@ +use std::fmt; + use error::{self, EvalexprResult}; use value::Value; @@ -47,3 +49,13 @@ impl Function { (self.function)(arguments) } } + +impl fmt::Debug for Function { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!( + f, + "Function {{ argument_amount: {:?}, function: [...] }}", + self.argument_amount + ) + } +} diff --git a/src/lib.rs b/src/lib.rs index 60d0a17..51624d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -305,12 +305,12 @@ //! //! ### [Serde](https://serde.rs) //! -//! To use this crate with serde, the serde feature flag has to be set. +//! To use this crate with serde, the `serde_support` feature flag has to be set. //! This can be done like this in the `Cargo.toml`: //! //! ```toml //! [dependencies] -//! evalexpr = {version = "3", features = ["serde"]} +//! evalexpr = {version = "3", features = ["serde_support"]} //! ``` //! //! This crate implements `serde::de::Deserialize` for its type `Node` that represents a parsed expression tree. @@ -336,6 +336,9 @@ //! //! With `serde`, expressions can be integrated into arbitrarily complex data. //! +//! The crate also implements `Serialize` and `Deserialize` for the `HashMapContext`. +//! But note that only the variables get serialized, not the functions. +//! //! ## License //! //! This crate is primarily distributed under the terms of the MIT license. @@ -346,8 +349,11 @@ #[cfg(test)] extern crate ron; -#[cfg(feature = "serde")] +#[cfg(feature = "serde_support")] extern crate serde; +#[cfg(feature = "serde_support")] +#[macro_use] +extern crate serde_derive; pub use context::{Context, EmptyContext, HashMapContext}; pub use error::{EvalexprError, EvalexprResult}; @@ -360,7 +366,7 @@ pub use value::{ mod context; pub mod error; -#[cfg(feature = "serde")] +#[cfg(feature = "serde_support")] mod feature_serde; mod function; mod interface; diff --git a/src/value/mod.rs b/src/value/mod.rs index 67e766f..b2e8cee 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -21,6 +21,7 @@ pub const EMPTY_VALUE: () = (); /// The value type used by the parser. /// Values can be of different subtypes that are the variants of this enum. #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))] pub enum Value { /// A string value. String(String),