diff --git a/README.md b/README.md index cad5f23..828ab71 100644 --- a/README.md +++ b/README.md @@ -438,31 +438,35 @@ Values are denoted as displayed in the following table. | `Value::Tuple` | `(3, 55.0, false, ())`, `(1, 2)` | | `Value::Empty` | `()` | -Integers are internally represented as `i64`, and floating point numbers are represented as `f64`. +By default, integers are internally represented as `i64`, and floating point numbers are represented as `f64`. +The numeric types are defined by the `Context` trait and can for example be customised by implementing a custom context. +Alternatively, for example the standard `HashMapContext` type takes the numeric types as type parameters, so it works with arbitrary numeric types. Tuples are represented as `Vec` and empty values are not stored, but represented by Rust's unit type `()` where necessary. There exist type aliases for some of the types. They include `IntType`, `FloatType`, `TupleType` and `EmptyType`. -Values can be constructed either directly or using the `From` trait. -They can be decomposed using the `Value::as_[type]` methods. +Values can be constructed either directly or using `from` functions. +For integers and floats, the `from` functions are `from_int` and `from_float`, and all others use the `From` trait. +See the examples below for further details. +Values can also be decomposed using the `Value::as_[type]` methods. The type of a value can be checked using the `Value::is_[type]` methods. **Examples for constructing a value:** | Code | Result | |------|--------| -| `Value::from(4)` | `Value::Int(4)` | -| `Value::from(4.4)` | `Value::Float(4.4)` | +| `Value::from_int(4)` | `Value::Int(4)` | +| `Value::from_float(4.4)` | `Value::Float(4.4)` | | `Value::from(true)` | `Value::Boolean(true)` | -| `Value::from(vec![Value::from(3)])` | `Value::Tuple(vec![Value::Int(3)])` | +| `Value::from(vec![Value::from_int(3)])` | `Value::Tuple(vec![Value::Int(3)])` | **Examples for deconstructing a value:** | Code | Result | |------|--------| -| `Value::from(4).as_int()` | `Ok(4)` | -| `Value::from(4.4).as_float()` | `Ok(4.4)` | +| `Value::from_int(4).as_int()` | `Ok(4)` | +| `Value::from_float(4.4).as_float()` | `Ok(4.4)` | | `Value::from(true).as_int()` | `Err(Error::ExpectedInt {actual: Value::Boolean(true)})` | Values have a precedence of 200. @@ -522,20 +526,6 @@ Here are some examples and counter-examples on expressions that are interpreted Functions have a precedence of 190. -### [Serde](https://serde.rs) - -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 = "", features = ["serde_support"]} -``` - -This crate implements `serde::de::Deserialize` for its type `Node` that represents a parsed expression tree. -The implementation expects a [serde `string`](https://serde.rs/data-model.html) as input. -Example parsing with [ron format](docs.rs/ron): - ### Comments Evalexpr supports C-style inline comments and end-of-line comments. @@ -558,6 +548,20 @@ assert_eq!( ); ``` +### [Serde](https://serde.rs) + +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 = "", features = ["serde_support"]} +``` + +This crate implements `serde::de::Deserialize` for its type `Node` that represents a parsed expression tree. +The implementation expects a [serde `string`](https://serde.rs/data-model.html) as input. +Example parsing with [ron format](https://docs.rs/ron): + ```rust extern crate ron; use evalexpr::*; @@ -621,3 +625,4 @@ If there is already an issue describing what you want to say, please add a thumb * This crate uses the [`sync-readme`](https://github.com/phaazon/cargo-sync-readme) cargo subcommand to keep the documentation in `src/lib.rs` and `README.md` in sync. The subcommand only syncs from the documentation in `src/lib.rs` to `README.md`. So please alter the documentation in the `src/lib.rs` rather than altering anything in between `` and `` in the `README.md`. + * Your contributions are assumed to be licensed under the [MIT License](https://opensource.org/license/mit). I relicense them under the [AGPL3 license](/LICENSE). diff --git a/src/context/mod.rs b/src/context/mod.rs index c135b5e..3aba764 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -138,8 +138,14 @@ impl Context for EmptyContext } impl IterateVariablesContext for EmptyContext { - type VariableIterator<'a> = iter::Empty<(String, Value)> where Self: 'a; - type VariableNameIterator<'a> = iter::Empty where Self: 'a; + type VariableIterator<'a> + = iter::Empty<(String, Value)> + where + Self: 'a; + type VariableNameIterator<'a> + = iter::Empty + where + Self: 'a; fn iter_variables(&self) -> Self::VariableIterator<'_> { iter::empty() @@ -201,8 +207,14 @@ impl Context impl IterateVariablesContext for EmptyContextWithBuiltinFunctions { - type VariableIterator<'a> = iter::Empty<(String, Value)> where Self: 'a; - type VariableNameIterator<'a> = iter::Empty where Self:'a; + type VariableIterator<'a> + = iter::Empty<(String, Value)> + where + Self: 'a; + type VariableNameIterator<'a> + = iter::Empty + where + Self: 'a; fn iter_variables(&self) -> Self::VariableIterator<'_> { iter::empty() @@ -356,12 +368,17 @@ impl ContextWithMutableFunctions } impl IterateVariablesContext for HashMapContext { - type VariableIterator<'a> = std::iter::Map< + type VariableIterator<'a> + = std::iter::Map< std::collections::hash_map::Iter<'a, String, Value>, fn((&String, &Value)) -> (String, Value), - > where Self: 'a; - type VariableNameIterator<'a> = - std::iter::Cloned>> where Self: 'a; + > + where + Self: 'a; + type VariableNameIterator<'a> + = std::iter::Cloned>> + where + Self: 'a; fn iter_variables(&self) -> Self::VariableIterator<'_> { self.variables diff --git a/src/error/mod.rs b/src/error/mod.rs index 7b3cf6b..a52a216 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -7,8 +7,13 @@ use std::ops::RangeInclusive; -use crate::value::numeric_types::{DefaultNumericTypes, EvalexprNumericTypes}; -use crate::{token::PartialToken, value::value_type::ValueType}; +use crate::{ + token::PartialToken, + value::{ + numeric_types::{DefaultNumericTypes, EvalexprNumericTypes}, + value_type::ValueType, + }, +}; use crate::{operator::Operator, value::Value}; diff --git a/src/lib.rs b/src/lib.rs index 5a090de..f983020 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -418,31 +418,35 @@ //! | `Value::Tuple` | `(3, 55.0, false, ())`, `(1, 2)` | //! | `Value::Empty` | `()` | //! -//! Integers are internally represented as `i64`, and floating point numbers are represented as `f64`. +//! By default, integers are internally represented as `i64`, and floating point numbers are represented as `f64`. +//! The numeric types are defined by the `Context` trait and can for example be customised by implementing a custom context. +//! Alternatively, for example the standard `HashMapContext` type takes the numeric types as type parameters, so it works with arbitrary numeric types. //! Tuples are represented as `Vec` and empty values are not stored, but represented by Rust's unit type `()` where necessary. //! //! There exist type aliases for some of the types. //! They include `IntType`, `FloatType`, `TupleType` and `EmptyType`. //! -//! Values can be constructed either directly or using the `From` trait. -//! They can be decomposed using the `Value::as_[type]` methods. +//! Values can be constructed either directly or using `from` functions. +//! For integers and floats, the `from` functions are `from_int` and `from_float`, and all others use the `From` trait. +//! See the examples below for further details. +//! Values can also be decomposed using the `Value::as_[type]` methods. //! The type of a value can be checked using the `Value::is_[type]` methods. //! //! **Examples for constructing a value:** //! //! | Code | Result | //! |------|--------| -//! | `Value::from(4)` | `Value::Int(4)` | -//! | `Value::from(4.4)` | `Value::Float(4.4)` | +//! | `Value::from_int(4)` | `Value::Int(4)` | +//! | `Value::from_float(4.4)` | `Value::Float(4.4)` | //! | `Value::from(true)` | `Value::Boolean(true)` | -//! | `Value::from(vec![Value::from(3)])` | `Value::Tuple(vec![Value::Int(3)])` | +//! | `Value::from(vec![Value::from_int(3)])` | `Value::Tuple(vec![Value::Int(3)])` | //! //! **Examples for deconstructing a value:** //! //! | Code | Result | //! |------|--------| -//! | `Value::from(4).as_int()` | `Ok(4)` | -//! | `Value::from(4.4).as_float()` | `Ok(4.4)` | +//! | `Value::from_int(4).as_int()` | `Ok(4)` | +//! | `Value::from_float(4.4).as_float()` | `Ok(4.4)` | //! | `Value::from(true).as_int()` | `Err(Error::ExpectedInt {actual: Value::Boolean(true)})` | //! //! Values have a precedence of 200. @@ -502,20 +506,6 @@ //! //! Functions have a precedence of 190. //! -//! ### [Serde](https://serde.rs) -//! -//! 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 = "", features = ["serde_support"]} -//! ``` -//! -//! This crate implements `serde::de::Deserialize` for its type `Node` that represents a parsed expression tree. -//! The implementation expects a [serde `string`](https://serde.rs/data-model.html) as input. -//! Example parsing with [ron format](docs.rs/ron): -//! //! ### Comments //! //! Evalexpr supports C-style inline comments and end-of-line comments. @@ -538,6 +528,20 @@ //! ); //! ``` //! +//! ### [Serde](https://serde.rs) +//! +//! 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 = "", features = ["serde_support"]} +//! ``` +//! +//! This crate implements `serde::de::Deserialize` for its type `Node` that represents a parsed expression tree. +//! The implementation expects a [serde `string`](https://serde.rs/data-model.html) as input. +//! Example parsing with [ron format](https://docs.rs/ron): +//! //! ```rust //! # #[cfg(feature = "serde_support")] { //! extern crate ron; diff --git a/src/operator/mod.rs b/src/operator/mod.rs index 65e6f9e..13ca5f0 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -1,9 +1,14 @@ use crate::function::builtin::builtin_function; -use crate::value::numeric_types::{ - DefaultNumericTypes, EvalexprFloat, EvalexprInt, EvalexprNumericTypes, +use crate::{ + context::Context, + error::*, + value::{ + numeric_types::{DefaultNumericTypes, EvalexprFloat, EvalexprInt, EvalexprNumericTypes}, + Value, + }, + ContextWithMutableVariables, }; -use crate::{context::Context, error::*, value::Value, ContextWithMutableVariables}; mod display;