Run rustfmt
This commit is contained in:
parent
6ace829117
commit
2399df16a1
@ -1,9 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::function::Function;
|
||||
use crate::value::value_type::ValueType;
|
||||
use crate::EvalexprError;
|
||||
use crate::EvalexprResult;
|
||||
use crate::{function::Function, value::value_type::ValueType, EvalexprError, EvalexprResult};
|
||||
|
||||
use crate::value::Value;
|
||||
|
||||
|
@ -35,7 +35,14 @@ impl fmt::Display for EvalexprError {
|
||||
write!(f, "Expected a Value::Boolean, but got {:?}.", actual)
|
||||
},
|
||||
ExpectedTuple { actual } => write!(f, "Expected a Value::Tuple, but got {:?}.", actual),
|
||||
ExpectedFixedLenTuple { expected_len, actual } => write!(f, "Expected a Value::Tuple of len {}, but got {:?}.", expected_len, actual),
|
||||
ExpectedFixedLenTuple {
|
||||
expected_len,
|
||||
actual,
|
||||
} => write!(
|
||||
f,
|
||||
"Expected a Value::Tuple of len {}, but got {:?}.",
|
||||
expected_len, actual
|
||||
),
|
||||
ExpectedEmpty { actual } => write!(f, "Expected a Value::Empty, but got {:?}.", actual),
|
||||
AppendedToLeafNode => write!(f, "Tried to append a node to a leaf node."),
|
||||
PrecedenceViolation => write!(
|
||||
|
@ -5,8 +5,10 @@
|
||||
//! The module also contains some helper functions starting with `expect_` that check for a condition and return `Err(_)` if the condition is not fulfilled.
|
||||
//! They are meant as shortcuts to not write the same error checking code everywhere.
|
||||
|
||||
use crate::token::PartialToken;
|
||||
use crate::value::{value_type::ValueType, TupleType};
|
||||
use crate::{
|
||||
token::PartialToken,
|
||||
value::{value_type::ValueType, TupleType},
|
||||
};
|
||||
|
||||
use crate::value::Value;
|
||||
|
||||
@ -244,7 +246,10 @@ impl EvalexprError {
|
||||
|
||||
/// Constructs `Error::ExpectedFixedLenTuple{expected_len, actual}`.
|
||||
pub fn expected_fixed_len_tuple(expected_len: usize, actual: Value) -> Self {
|
||||
EvalexprError::ExpectedFixedLenTuple { expected_len, actual }
|
||||
EvalexprError::ExpectedFixedLenTuple {
|
||||
expected_len,
|
||||
actual,
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs `Error::ExpectedEmpty{actual}`.
|
||||
|
@ -1,7 +1,6 @@
|
||||
use crate::interface::build_operator_tree;
|
||||
use crate::{interface::build_operator_tree, Node};
|
||||
use serde::{de, Deserialize, Deserializer};
|
||||
use std::fmt;
|
||||
use crate::Node;
|
||||
|
||||
impl<'de> Deserialize<'de> for Node {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
|
@ -1,11 +1,10 @@
|
||||
#[cfg(feature = "regex_support")]
|
||||
use regex::Regex;
|
||||
|
||||
|
||||
use crate::value::{FloatType, IntType};
|
||||
use crate::EvalexprError;
|
||||
use crate::Function;
|
||||
use crate::Value;
|
||||
use crate::{
|
||||
value::{FloatType, IntType},
|
||||
EvalexprError, Function, Value,
|
||||
};
|
||||
|
||||
pub fn builtin_function(identifier: &str) -> Option<Function> {
|
||||
match identifier {
|
||||
@ -82,7 +81,9 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
|
||||
let re_str = arguments[1].as_string()?;
|
||||
let repl = arguments[2].as_string()?;
|
||||
match Regex::new(&re_str) {
|
||||
Ok(re) => Ok(Value::String(re.replace_all(&subject, repl.as_str()).to_string())),
|
||||
Ok(re) => Ok(Value::String(
|
||||
re.replace_all(&subject, repl.as_str()).to_string(),
|
||||
)),
|
||||
Err(err) => Err(EvalexprError::invalid_regex(
|
||||
re_str.to_string(),
|
||||
format!("{}", err),
|
||||
|
@ -1,7 +1,6 @@
|
||||
use std::fmt;
|
||||
|
||||
use crate::error::EvalexprResult;
|
||||
use crate::value::Value;
|
||||
use crate::{error::EvalexprResult, value::Value};
|
||||
|
||||
pub(crate) mod builtin;
|
||||
|
||||
|
@ -1,15 +1,7 @@
|
||||
use crate::{token, HashMapContext};
|
||||
use crate::tree;
|
||||
use crate::value::TupleType;
|
||||
use crate::Context;
|
||||
use crate::EmptyType;
|
||||
use crate::EvalexprError;
|
||||
use crate::EvalexprResult;
|
||||
use crate::FloatType;
|
||||
use crate::IntType;
|
||||
use crate::Node;
|
||||
use crate::Value;
|
||||
use crate::EMPTY_VALUE;
|
||||
use crate::{
|
||||
token, tree, value::TupleType, Context, EmptyType, EvalexprError, EvalexprResult, FloatType,
|
||||
HashMapContext, IntType, Node, Value, EMPTY_VALUE,
|
||||
};
|
||||
|
||||
/// Evaluate the given expression string.
|
||||
///
|
||||
@ -224,7 +216,10 @@ pub fn eval_empty_with_context(string: &str, context: &dyn Context) -> EvalexprR
|
||||
/// Evaluate the given expression string into a string with the given mutable context.
|
||||
///
|
||||
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
||||
pub fn eval_string_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<String> {
|
||||
pub fn eval_string_with_context_mut(
|
||||
string: &str,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<String> {
|
||||
match eval_with_context_mut(string, context) {
|
||||
Ok(Value::String(string)) => Ok(string),
|
||||
Ok(value) => Err(EvalexprError::expected_string(value)),
|
||||
@ -235,7 +230,10 @@ pub fn eval_string_with_context_mut(string: &str, context: &mut dyn Context) ->
|
||||
/// Evaluate the given expression string into an integer with the given mutable context.
|
||||
///
|
||||
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
||||
pub fn eval_int_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<IntType> {
|
||||
pub fn eval_int_with_context_mut(
|
||||
string: &str,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<IntType> {
|
||||
match eval_with_context_mut(string, context) {
|
||||
Ok(Value::Int(int)) => Ok(int),
|
||||
Ok(value) => Err(EvalexprError::expected_int(value)),
|
||||
@ -276,7 +274,10 @@ pub fn eval_number_with_context_mut(
|
||||
/// Evaluate the given expression string into a boolean with the given mutable context.
|
||||
///
|
||||
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
|
||||
pub fn eval_boolean_with_context_mut(string: &str, context: &mut dyn Context) -> EvalexprResult<bool> {
|
||||
pub fn eval_boolean_with_context_mut(
|
||||
string: &str,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<bool> {
|
||||
match eval_with_context_mut(string, context) {
|
||||
Ok(Value::Boolean(boolean)) => Ok(boolean),
|
||||
Ok(value) => Err(EvalexprError::expected_boolean(value)),
|
||||
|
14
src/lib.rs
14
src/lib.rs
@ -369,13 +369,13 @@ extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
pub use crate::context::{Context, EmptyContext, HashMapContext};
|
||||
pub use crate::error::{EvalexprError, EvalexprResult};
|
||||
pub use crate::function::Function;
|
||||
pub use crate::interface::*;
|
||||
pub use crate::tree::Node;
|
||||
pub use crate::value::{
|
||||
value_type::ValueType, EmptyType, FloatType, IntType, TupleType, Value, EMPTY_VALUE,
|
||||
pub use crate::{
|
||||
context::{Context, EmptyContext, HashMapContext},
|
||||
error::{EvalexprError, EvalexprResult},
|
||||
function::Function,
|
||||
interface::*,
|
||||
tree::Node,
|
||||
value::{value_type::ValueType, EmptyType, FloatType, IntType, TupleType, Value, EMPTY_VALUE},
|
||||
};
|
||||
|
||||
mod context;
|
||||
|
@ -1,5 +1,7 @@
|
||||
use crate::error::{EvalexprError, EvalexprResult};
|
||||
use crate::value::{FloatType, IntType};
|
||||
use crate::{
|
||||
error::{EvalexprError, EvalexprResult},
|
||||
value::{FloatType, IntType},
|
||||
};
|
||||
|
||||
mod display;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::fmt::{Display, Error, Formatter};
|
||||
use crate::Node;
|
||||
use std::fmt::{Display, Error, Formatter};
|
||||
|
||||
impl Display for Node {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use std::slice::Iter;
|
||||
use crate::Node;
|
||||
use std::slice::Iter;
|
||||
|
||||
/// An iterator that traverses an operator tree in pre-order.
|
||||
pub struct NodeIter<'a> {
|
||||
|
@ -1,9 +1,8 @@
|
||||
use crate::token::Token;
|
||||
use crate::value::{TupleType, EMPTY_VALUE};
|
||||
use crate::EmptyContext;
|
||||
use crate::EmptyType;
|
||||
use crate::FloatType;
|
||||
use crate::IntType;
|
||||
use crate::{
|
||||
token::Token,
|
||||
value::{TupleType, EMPTY_VALUE},
|
||||
EmptyContext, EmptyType, FloatType, IntType,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
context::Context,
|
||||
@ -228,7 +227,10 @@ impl Node {
|
||||
/// Evaluates the operator tree rooted at this node into a string with an the given mutable context.
|
||||
///
|
||||
/// Fails, if one of the operators in the expression tree fails.
|
||||
pub fn eval_string_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<String> {
|
||||
pub fn eval_string_with_context_mut(
|
||||
&self,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<String> {
|
||||
match self.eval_with_context_mut(context) {
|
||||
Ok(Value::String(string)) => Ok(string),
|
||||
Ok(value) => Err(EvalexprError::expected_string(value)),
|
||||
@ -239,7 +241,10 @@ impl Node {
|
||||
/// Evaluates the operator tree rooted at this node into a float with an the given mutable context.
|
||||
///
|
||||
/// Fails, if one of the operators in the expression tree fails.
|
||||
pub fn eval_float_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<FloatType> {
|
||||
pub fn eval_float_with_context_mut(
|
||||
&self,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<FloatType> {
|
||||
match self.eval_with_context_mut(context) {
|
||||
Ok(Value::Float(float)) => Ok(float),
|
||||
Ok(value) => Err(EvalexprError::expected_float(value)),
|
||||
@ -262,7 +267,10 @@ impl Node {
|
||||
/// If the result of the expression is an integer, it is silently converted into a float.
|
||||
///
|
||||
/// Fails, if one of the operators in the expression tree fails.
|
||||
pub fn eval_number_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<FloatType> {
|
||||
pub fn eval_number_with_context_mut(
|
||||
&self,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<FloatType> {
|
||||
match self.eval_with_context_mut(context) {
|
||||
Ok(Value::Int(int)) => Ok(int as FloatType),
|
||||
Ok(Value::Float(float)) => Ok(float),
|
||||
@ -285,7 +293,10 @@ impl Node {
|
||||
/// Evaluates the operator tree rooted at this node into a tuple with an the given mutable context.
|
||||
///
|
||||
/// Fails, if one of the operators in the expression tree fails.
|
||||
pub fn eval_tuple_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<TupleType> {
|
||||
pub fn eval_tuple_with_context_mut(
|
||||
&self,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<TupleType> {
|
||||
match self.eval_with_context_mut(context) {
|
||||
Ok(Value::Tuple(tuple)) => Ok(tuple),
|
||||
Ok(value) => Err(EvalexprError::expected_tuple(value)),
|
||||
@ -296,7 +307,10 @@ impl Node {
|
||||
/// Evaluates the operator tree rooted at this node into an empty value with an the given mutable context.
|
||||
///
|
||||
/// Fails, if one of the operators in the expression tree fails.
|
||||
pub fn eval_empty_with_context_mut(&self, context: &mut dyn Context) -> EvalexprResult<EmptyType> {
|
||||
pub fn eval_empty_with_context_mut(
|
||||
&self,
|
||||
context: &mut dyn Context,
|
||||
) -> EvalexprResult<EmptyType> {
|
||||
match self.eval_with_context_mut(context) {
|
||||
Ok(Value::Empty) => Ok(EMPTY_VALUE),
|
||||
Ok(value) => Err(EvalexprError::expected_empty(value)),
|
||||
|
@ -639,12 +639,27 @@ fn test_tuple_definitions() {
|
||||
|
||||
#[test]
|
||||
fn test_implicit_context() {
|
||||
assert_eq!(eval("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"), Ok(Value::from(true)));
|
||||
assert_eq!(eval_boolean("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"), Ok(true));
|
||||
assert_eq!(
|
||||
eval("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"),
|
||||
Ok(Value::from(true))
|
||||
);
|
||||
assert_eq!(
|
||||
eval_boolean("a = 2 + 4 * 2; b = -5 + 3 * 5; a == b"),
|
||||
Ok(true)
|
||||
);
|
||||
assert_eq!(eval_int("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0));
|
||||
assert_eq!(eval_float("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b + 0.5"), Ok(0.5));
|
||||
assert_eq!(
|
||||
eval_float("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b + 0.5"),
|
||||
Ok(0.5)
|
||||
);
|
||||
assert_eq!(eval_number("a = 2 + 4 * 2; b = -5 + 3 * 5; a - b"), Ok(0.0));
|
||||
assert_eq!(eval_empty("a = 2 + 4 * 2; b = -5 + 3 * 5;"), Ok(()));
|
||||
assert_eq!(eval_tuple("a = 2 + 4 * 2; b = -5 + 3 * 5; a, b + 0.5"), Ok(vec![Value::from(10), Value::from(10.5)]));
|
||||
assert_eq!(eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"), Ok("xyzabc".to_string()));
|
||||
}
|
||||
assert_eq!(
|
||||
eval_tuple("a = 2 + 4 * 2; b = -5 + 3 * 5; a, b + 0.5"),
|
||||
Ok(vec![Value::from(10), Value::from(10.5)])
|
||||
);
|
||||
assert_eq!(
|
||||
eval_string("a = \"xyz\"; b = \"abc\"; c = a + b; c"),
|
||||
Ok("xyzabc".to_string())
|
||||
);
|
||||
}
|
||||
|
@ -11,4 +11,4 @@ fn test_serde() {
|
||||
let serde_tree: Node = ron::de::from_str(&format!("\"{}\"", string)).unwrap();
|
||||
assert_eq!(manual_tree.eval(), serde_tree.eval());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user