Skip to content

Commit

Permalink
Update readme (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
ISibboI authored Dec 27, 2024
2 parents 23e5782 + 7ec053d commit 8b28dfb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 57 deletions.
49 changes: 27 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>` 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.
Expand Down Expand Up @@ -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 = "<desired 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.
Expand All @@ -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 = "<desired 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::*;
Expand Down Expand Up @@ -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 `<!-- cargo-sync-readme start -->` and `<!-- cargo-sync-readme end -->` 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).
33 changes: 25 additions & 8 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ impl<NumericTypes: EvalexprNumericTypes> Context for EmptyContext<NumericTypes>
}

impl<NumericTypes: EvalexprNumericTypes> IterateVariablesContext for EmptyContext<NumericTypes> {
type VariableIterator<'a> = iter::Empty<(String, Value<Self::NumericTypes>)> where Self: 'a;
type VariableNameIterator<'a> = iter::Empty<String> where Self: 'a;
type VariableIterator<'a>
= iter::Empty<(String, Value<Self::NumericTypes>)>
where
Self: 'a;
type VariableNameIterator<'a>
= iter::Empty<String>
where
Self: 'a;

fn iter_variables(&self) -> Self::VariableIterator<'_> {
iter::empty()
Expand Down Expand Up @@ -201,8 +207,14 @@ impl<NumericTypes: EvalexprNumericTypes> Context
impl<NumericTypes: EvalexprNumericTypes> IterateVariablesContext
for EmptyContextWithBuiltinFunctions<NumericTypes>
{
type VariableIterator<'a> = iter::Empty<(String, Value<Self::NumericTypes>)> where Self: 'a;
type VariableNameIterator<'a> = iter::Empty<String> where Self:'a;
type VariableIterator<'a>
= iter::Empty<(String, Value<Self::NumericTypes>)>
where
Self: 'a;
type VariableNameIterator<'a>
= iter::Empty<String>
where
Self: 'a;

fn iter_variables(&self) -> Self::VariableIterator<'_> {
iter::empty()
Expand Down Expand Up @@ -356,12 +368,17 @@ impl<NumericTypes: EvalexprNumericTypes> ContextWithMutableFunctions
}

impl<NumericTypes: EvalexprNumericTypes> IterateVariablesContext for HashMapContext<NumericTypes> {
type VariableIterator<'a> = std::iter::Map<
type VariableIterator<'a>
= std::iter::Map<
std::collections::hash_map::Iter<'a, String, Value<NumericTypes>>,
fn((&String, &Value<NumericTypes>)) -> (String, Value<NumericTypes>),
> where Self: 'a;
type VariableNameIterator<'a> =
std::iter::Cloned<std::collections::hash_map::Keys<'a, String, Value<NumericTypes>>> where Self: 'a;
>
where
Self: 'a;
type VariableNameIterator<'a>
= std::iter::Cloned<std::collections::hash_map::Keys<'a, String, Value<NumericTypes>>>
where
Self: 'a;

fn iter_variables(&self) -> Self::VariableIterator<'_> {
self.variables
Expand Down
9 changes: 7 additions & 2 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
48 changes: 26 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>` 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.
Expand Down Expand Up @@ -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 = "<desired 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.
Expand All @@ -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 = "<desired 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;
Expand Down
11 changes: 8 additions & 3 deletions src/operator/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit 8b28dfb

Please sign in to comment.