Add serde support for HashMapContext

Relates to #33
This commit is contained in:
Sebastian Schmidt 2019-03-28 15:39:50 +01:00
parent 0cd6acfeb6
commit 17bedddb11
6 changed files with 35 additions and 8 deletions

View File

@ -17,8 +17,10 @@ path = "src/lib.rs"
[dependencies] [dependencies]
serde = { version = "1", optional = true} serde = { version = "1", optional = true}
serde_derive = { version = "1", optional = true}
[features] [features]
serde_support = ["serde", "serde_derive"]
[dev-dependencies] [dev-dependencies]
ron = "0.4" ron = "0.4"

View File

@ -318,12 +318,12 @@ Functions have a precedence of 190.
### [Serde](https://serde.rs) ### [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`: This can be done like this in the `Cargo.toml`:
```toml ```toml
[dependencies] [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. 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::<Node>(serialized_free) {
With `serde`, expressions can be integrated into arbitrarily complex data. 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 ## License
This crate is primarily distributed under the terms of the MIT license. This crate is primarily distributed under the terms of the MIT license.

View File

@ -1,9 +1,9 @@
use std::collections::HashMap; use std::collections::HashMap;
use function::Function;
use value::value_type::ValueType;
use EvalexprError; use EvalexprError;
use EvalexprResult; use EvalexprResult;
use function::Function;
use value::value_type::ValueType;
use crate::value::Value; 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.* /// *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. /// 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 { pub struct HashMapContext {
variables: HashMap<String, Value>, variables: HashMap<String, Value>,
#[cfg_attr(feature = "serde_support", serde(skip))]
functions: HashMap<String, Function>, functions: HashMap<String, Function>,
} }

View File

@ -1,3 +1,5 @@
use std::fmt;
use error::{self, EvalexprResult}; use error::{self, EvalexprResult};
use value::Value; use value::Value;
@ -47,3 +49,13 @@ impl Function {
(self.function)(arguments) (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
)
}
}

View File

@ -305,12 +305,12 @@
//! //!
//! ### [Serde](https://serde.rs) //! ### [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`: //! This can be done like this in the `Cargo.toml`:
//! //!
//! ```toml //! ```toml
//! [dependencies] //! [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. //! 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. //! 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 //! ## License
//! //!
//! This crate is primarily distributed under the terms of the MIT license. //! This crate is primarily distributed under the terms of the MIT license.
@ -346,8 +349,11 @@
#[cfg(test)] #[cfg(test)]
extern crate ron; extern crate ron;
#[cfg(feature = "serde")] #[cfg(feature = "serde_support")]
extern crate serde; extern crate serde;
#[cfg(feature = "serde_support")]
#[macro_use]
extern crate serde_derive;
pub use context::{Context, EmptyContext, HashMapContext}; pub use context::{Context, EmptyContext, HashMapContext};
pub use error::{EvalexprError, EvalexprResult}; pub use error::{EvalexprError, EvalexprResult};
@ -360,7 +366,7 @@ pub use value::{
mod context; mod context;
pub mod error; pub mod error;
#[cfg(feature = "serde")] #[cfg(feature = "serde_support")]
mod feature_serde; mod feature_serde;
mod function; mod function;
mod interface; mod interface;

View File

@ -21,6 +21,7 @@ pub const EMPTY_VALUE: () = ();
/// The value type used by the parser. /// The value type used by the parser.
/// Values can be of different subtypes that are the variants of this enum. /// Values can be of different subtypes that are the variants of this enum.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub enum Value { pub enum Value {
/// A string value. /// A string value.
String(String), String(String),