Use EvalexprResult<T> everywhere

Relates to #31
This commit is contained in:
Sebastian Schmidt 2019-03-27 19:08:47 +01:00
parent d5544cdbf2
commit d77fa15864
10 changed files with 84 additions and 91 deletions

View File

@ -215,7 +215,7 @@ Variables have a precedence of 200.
This crate also allows to define arbitrary functions to be used in parsed expressions.
A function is defined as a `Function` instance.
It contains two properties, the `argument_amount` and the `function`.
The `function` is a boxed `Fn(&[Value]) -> Result<Value, Error>`.
The `function` is a boxed `Fn(&[Value]) -> EvalexprResult<Value, Error>`.
The `argument_amount` determines the length of the slice that is passed to `function` if it is `Some(_)`, otherwise the function is defined to take an arbitrary amount of arguments.
It is verified on execution by the crate and does not need to be verified by the `function`.

View File

@ -106,7 +106,7 @@ impl ContextMut for HashMapContext {
&mut self,
identifier: S,
value: V,
) -> Result<(), EvalexprError> {
) -> EvalexprResult<()> {
let identifier = identifier.into();
let value = value.into();
if let Some(existing_value) = self.variables.get_mut(&identifier) {
@ -127,7 +127,7 @@ impl ContextMut for HashMapContext {
&mut self,
identifier: S,
function: Function,
) -> Result<(), EvalexprError> {
) -> EvalexprResult<()> {
self.functions.insert(identifier.into(), function);
Ok(())
}

View File

@ -261,7 +261,7 @@ impl EvalexprError {
pub(crate) fn expect_operator_argument_amount(
actual: usize,
expected: usize,
) -> Result<(), EvalexprError> {
) -> EvalexprResult<()> {
if actual == expected {
Ok(())
} else {
@ -275,7 +275,7 @@ pub(crate) fn expect_operator_argument_amount(
pub(crate) fn expect_function_argument_amount(
actual: usize,
expected: usize,
) -> Result<(), EvalexprError> {
) -> EvalexprResult<()> {
if actual == expected {
Ok(())
} else {
@ -288,7 +288,7 @@ pub(crate) fn expect_function_argument_amount(
/// Returns `Ok(())` if the given value is numeric.
/// Numeric types are `Value::Int` and `Value::Float`.
/// Otherwise, `Err(Error::ExpectedNumber)` is returned.
pub fn expect_number(actual: &Value) -> Result<(), EvalexprError> {
pub fn expect_number(actual: &Value) -> EvalexprResult<()> {
match actual {
Value::Float(_) | Value::Int(_) => Ok(()),
_ => Err(EvalexprError::expected_number(actual.clone())),
@ -296,7 +296,7 @@ pub fn expect_number(actual: &Value) -> Result<(), EvalexprError> {
}
/// Returns `Ok(())` if the given value is a `Value::Boolean`, or `Err(Error::ExpectedBoolean)` otherwise.
pub fn expect_boolean(actual: &Value) -> Result<bool, EvalexprError> {
pub fn expect_boolean(actual: &Value) -> EvalexprResult<bool> {
match actual {
Value::Boolean(boolean) => Ok(*boolean),
_ => Err(EvalexprError::expected_boolean(actual.clone())),

View File

@ -1,4 +1,4 @@
use error::{self, EvalexprError};
use error::{self, EvalexprResult};
use value::Value;
pub(crate) mod builtin;
@ -19,7 +19,7 @@ pub(crate) mod builtin;
/// ```
pub struct Function {
argument_amount: Option<usize>,
function: Box<Fn(&[Value]) -> Result<Value, EvalexprError>>,
function: Box<Fn(&[Value]) -> EvalexprResult<Value>>,
}
impl Function {
@ -28,10 +28,10 @@ impl Function {
/// The `argument_amount` is the amount of arguments this function takes.
/// It is verified before the actual function is executed, assuming it is not `None`.
///
/// The `function` is a boxed function that takes a slice of values and returns a `Result<Value, Error>`.
/// The `function` is a boxed function that takes a slice of values and returns a `EvalexprResult<Value, Error>`.
pub fn new(
argument_amount: Option<usize>,
function: Box<Fn(&[Value]) -> Result<Value, EvalexprError>>,
function: Box<Fn(&[Value]) -> EvalexprResult<Value>>,
) -> Self {
Self {
argument_amount,
@ -39,7 +39,7 @@ impl Function {
}
}
pub(crate) fn call(&self, arguments: &[Value]) -> Result<Value, EvalexprError> {
pub(crate) fn call(&self, arguments: &[Value]) -> EvalexprResult<Value> {
if let Some(argument_amount) = self.argument_amount {
error::expect_function_argument_amount(arguments.len(), argument_amount)?;
}

View File

@ -1,6 +1,7 @@
use Context;
use EmptyContext;
use EvalexprError;
use EvalexprResult;
use FloatType;
use IntType;
use Node;
@ -20,7 +21,7 @@ use value::TupleType;
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval(string: &str) -> Result<Value, EvalexprError> {
pub fn eval(string: &str) -> EvalexprResult<Value> {
eval_with_context(string, &EmptyContext)
}
@ -39,7 +40,7 @@ pub fn eval(string: &str) -> Result<Value, EvalexprError> {
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_with_context(string: &str, context: &Context) -> Result<Value, EvalexprError> {
pub fn eval_with_context(string: &str, context: &Context) -> EvalexprResult<Value> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
}
@ -67,14 +68,14 @@ pub fn eval_with_context(string: &str, context: &Context) -> Result<Value, Evale
/// ```
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn build_operator_tree(string: &str) -> Result<Node, EvalexprError> {
pub fn build_operator_tree(string: &str) -> EvalexprResult<Node> {
tree::tokens_to_operator_tree(token::tokenize(string)?)
}
/// Evaluate the given expression string into a string.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_string(string: &str) -> Result<String, EvalexprError> {
pub fn eval_string(string: &str) -> EvalexprResult<String> {
match eval(string) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -85,7 +86,7 @@ pub fn eval_string(string: &str) -> Result<String, EvalexprError> {
/// Evaluate the given expression string into an integer.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_int(string: &str) -> Result<IntType, EvalexprError> {
pub fn eval_int(string: &str) -> EvalexprResult<IntType> {
match eval(string) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -96,7 +97,7 @@ pub fn eval_int(string: &str) -> Result<IntType, EvalexprError> {
/// Evaluate the given expression string into a float.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_float(string: &str) -> Result<FloatType, EvalexprError> {
pub fn eval_float(string: &str) -> EvalexprResult<FloatType> {
match eval(string) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
@ -108,7 +109,7 @@ pub fn eval_float(string: &str) -> Result<FloatType, EvalexprError> {
/// If the result of the expression is an integer, it is silently converted into a float.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_number(string: &str) -> Result<FloatType, EvalexprError> {
pub fn eval_number(string: &str) -> EvalexprResult<FloatType> {
match eval(string) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(int as FloatType),
@ -120,7 +121,7 @@ pub fn eval_number(string: &str) -> Result<FloatType, EvalexprError> {
/// Evaluate the given expression string into a boolean.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_boolean(string: &str) -> Result<bool, EvalexprError> {
pub fn eval_boolean(string: &str) -> EvalexprResult<bool> {
match eval(string) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -131,7 +132,7 @@ pub fn eval_boolean(string: &str) -> Result<bool, EvalexprError> {
/// Evaluate the given expression string into a tuple.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_tuple(string: &str) -> Result<TupleType, EvalexprError> {
pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
match eval(string) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
@ -142,7 +143,7 @@ pub fn eval_tuple(string: &str) -> Result<TupleType, EvalexprError> {
/// Evaluate the given expression string into a string with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_string_with_context(string: &str, context: &Context) -> Result<String, EvalexprError> {
pub fn eval_string_with_context(string: &str, context: &Context) -> EvalexprResult<String> {
match eval_with_context(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -153,7 +154,7 @@ pub fn eval_string_with_context(string: &str, context: &Context) -> Result<Strin
/// Evaluate the given expression string into an integer with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_int_with_context(string: &str, context: &Context) -> Result<IntType, EvalexprError> {
pub fn eval_int_with_context(string: &str, context: &Context) -> EvalexprResult<IntType> {
match eval_with_context(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -164,10 +165,7 @@ pub fn eval_int_with_context(string: &str, context: &Context) -> Result<IntType,
/// Evaluate the given expression string into a float with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_float_with_context(
string: &str,
context: &Context,
) -> Result<FloatType, EvalexprError> {
pub fn eval_float_with_context(string: &str, context: &Context) -> EvalexprResult<FloatType> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
@ -179,10 +177,7 @@ pub fn eval_float_with_context(
/// If the result of the expression is an integer, it is silently converted into a float.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_number_with_context(
string: &str,
context: &Context,
) -> Result<FloatType, EvalexprError> {
pub fn eval_number_with_context(string: &str, context: &Context) -> EvalexprResult<FloatType> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(int as FloatType),
@ -194,7 +189,7 @@ pub fn eval_number_with_context(
/// Evaluate the given expression string into a boolean with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_boolean_with_context(string: &str, context: &Context) -> Result<bool, EvalexprError> {
pub fn eval_boolean_with_context(string: &str, context: &Context) -> EvalexprResult<bool> {
match eval_with_context(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -205,10 +200,7 @@ pub fn eval_boolean_with_context(string: &str, context: &Context) -> Result<bool
/// Evaluate the given expression string into a tuple with the given context.
///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_tuple_with_context(
string: &str,
context: &Context,
) -> Result<TupleType, EvalexprError> {
pub fn eval_tuple_with_context(string: &str, context: &Context) -> EvalexprResult<TupleType> {
match eval_with_context(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),

View File

@ -202,7 +202,7 @@
//! This crate also allows to define arbitrary functions to be used in parsed expressions.
//! A function is defined as a `Function` instance.
//! It contains two properties, the `argument_amount` and the `function`.
//! The `function` is a boxed `Fn(&[Value]) -> Result<Value, Error>`.
//! The `function` is a boxed `Fn(&[Value]) -> EvalexprResult<Value, Error>`.
//! The `argument_amount` determines the length of the slice that is passed to `function` if it is `Some(_)`, otherwise the function is defined to take an arbitrary amount of arguments.
//! It is verified on execution by the crate and does not need to be verified by the `function`.
//!

View File

@ -29,7 +29,7 @@ pub trait Operator: Debug + Display {
fn argument_amount(&self) -> usize;
/// Evaluates the operator with the given arguments and context.
fn eval(&self, arguments: &[Value], context: &Context) -> Result<Value, EvalexprError>;
fn eval(&self, arguments: &[Value], context: &Context) -> EvalexprResult<Value>;
}
#[derive(Debug)]
@ -118,7 +118,7 @@ impl Operator for RootNode {
1
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 1)?;
Ok(arguments[0].clone())
}
@ -137,7 +137,7 @@ impl Operator for Add {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -173,7 +173,7 @@ impl Operator for Sub {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -209,7 +209,7 @@ impl Operator for Neg {
1
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 1)?;
expect_number(&arguments[0])?;
@ -239,7 +239,7 @@ impl Operator for Mul {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -275,7 +275,7 @@ impl Operator for Div {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -311,7 +311,7 @@ impl Operator for Mod {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -347,7 +347,7 @@ impl Operator for Exp {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -374,7 +374,7 @@ impl Operator for Eq {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
if arguments[0] == arguments[1] {
@ -398,7 +398,7 @@ impl Operator for Neq {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
if arguments[0] != arguments[1] {
@ -422,7 +422,7 @@ impl Operator for Gt {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -456,7 +456,7 @@ impl Operator for Lt {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -490,7 +490,7 @@ impl Operator for Geq {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -524,7 +524,7 @@ impl Operator for Leq {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
expect_number(&arguments[0])?;
expect_number(&arguments[1])?;
@ -558,7 +558,7 @@ impl Operator for And {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
let a = expect_boolean(&arguments[0])?;
let b = expect_boolean(&arguments[1])?;
@ -584,7 +584,7 @@ impl Operator for Or {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 2)?;
let a = expect_boolean(&arguments[0])?;
let b = expect_boolean(&arguments[1])?;
@ -610,7 +610,7 @@ impl Operator for Not {
1
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 1)?;
let a = expect_boolean(&arguments[0])?;
@ -635,7 +635,7 @@ impl Operator for Tuple {
2
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
if let Value::Tuple(tuple) = &arguments[0] {
let mut tuple = tuple.clone();
if let Value::Tuple(tuple2) = &arguments[1] {
@ -672,7 +672,7 @@ impl Operator for Const {
0
}
fn eval(&self, arguments: &[Value], _context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 0)?;
Ok(self.value.clone())
@ -692,7 +692,7 @@ impl Operator for VariableIdentifier {
0
}
fn eval(&self, _arguments: &[Value], context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, _arguments: &[Value], context: &Context) -> EvalexprResult<Value> {
if let Some(value) = context.get_value(&self.identifier).cloned() {
Ok(value)
} else {
@ -716,7 +716,7 @@ impl Operator for FunctionIdentifier {
1
}
fn eval(&self, arguments: &[Value], context: &Context) -> Result<Value, EvalexprError> {
fn eval(&self, arguments: &[Value], context: &Context) -> EvalexprResult<Value> {
expect_operator_argument_amount(arguments.len(), 1)?;
let arguments = if let Value::Tuple(arguments) = &arguments[0] {

View File

@ -1,4 +1,4 @@
use error::EvalexprError;
use error::{EvalexprError, EvalexprResult};
use value::{FloatType, IntType};
mod display;
@ -173,7 +173,7 @@ fn str_to_tokens(string: &str) -> Vec<PartialToken> {
}
/// Resolves all partial tokens by converting them to complex tokens.
fn resolve_literals(mut tokens: &[PartialToken]) -> Result<Vec<Token>, EvalexprError> {
fn resolve_literals(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<Token>> {
let mut result = Vec::new();
while tokens.len() > 0 {
let first = tokens[0].clone();
@ -244,6 +244,6 @@ fn resolve_literals(mut tokens: &[PartialToken]) -> Result<Vec<Token>, EvalexprE
Ok(result)
}
pub(crate) fn tokenize(string: &str) -> Result<Vec<Token>, EvalexprError> {
pub(crate) fn tokenize(string: &str) -> EvalexprResult<Vec<Token>> {
resolve_literals(&str_to_tokens(string))
}

View File

@ -4,7 +4,12 @@ use IntType;
use token::Token;
use value::TupleType;
use crate::{context::Context, error::EvalexprError, operator::*, value::Value};
use crate::{
context::Context,
error::{EvalexprError, EvalexprResult},
operator::*,
value::Value,
};
mod display;
@ -46,7 +51,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node with the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_with_context(&self, context: &Context) -> Result<Value, EvalexprError> {
pub fn eval_with_context(&self, context: &Context) -> EvalexprResult<Value> {
let mut arguments = Vec::new();
for child in self.children() {
arguments.push(child.eval_with_context(context)?);
@ -57,14 +62,14 @@ impl Node {
/// Evaluates the operator tree rooted at this node with an empty context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval(&self) -> Result<Value, EvalexprError> {
pub fn eval(&self) -> EvalexprResult<Value> {
self.eval_with_context(&EmptyContext)
}
/// Evaluates the operator tree rooted at this node into a string with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_string_with_context(&self, context: &Context) -> Result<String, EvalexprError> {
pub fn eval_string_with_context(&self, context: &Context) -> EvalexprResult<String> {
match self.eval_with_context(context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
@ -75,7 +80,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a float with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_float_with_context(&self, context: &Context) -> Result<FloatType, EvalexprError> {
pub fn eval_float_with_context(&self, context: &Context) -> EvalexprResult<FloatType> {
match self.eval_with_context(context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
@ -86,7 +91,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into an integer with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_int_with_context(&self, context: &Context) -> Result<IntType, EvalexprError> {
pub fn eval_int_with_context(&self, context: &Context) -> EvalexprResult<IntType> {
match self.eval_with_context(context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
@ -98,7 +103,7 @@ 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(&self, context: &Context) -> Result<FloatType, EvalexprError> {
pub fn eval_number_with_context(&self, context: &Context) -> EvalexprResult<FloatType> {
match self.eval_with_context(context) {
Ok(Value::Int(int)) => Ok(int as FloatType),
Ok(Value::Float(float)) => Ok(float),
@ -110,7 +115,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a boolean with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_boolean_with_context(&self, context: &Context) -> Result<bool, EvalexprError> {
pub fn eval_boolean_with_context(&self, context: &Context) -> EvalexprResult<bool> {
match self.eval_with_context(context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
@ -121,7 +126,7 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a tuple with an the given context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_tuple_with_context(&self, context: &Context) -> Result<TupleType, EvalexprError> {
pub fn eval_tuple_with_context(&self, context: &Context) -> EvalexprResult<TupleType> {
match self.eval_with_context(context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
@ -132,21 +137,21 @@ impl Node {
/// Evaluates the operator tree rooted at this node into a string with an empty context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_string(&self) -> Result<String, EvalexprError> {
pub fn eval_string(&self) -> EvalexprResult<String> {
self.eval_string_with_context(&EmptyContext)
}
/// Evaluates the operator tree rooted at this node into a float with an empty context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_float(&self) -> Result<FloatType, EvalexprError> {
pub fn eval_float(&self) -> EvalexprResult<FloatType> {
self.eval_float_with_context(&EmptyContext)
}
/// Evaluates the operator tree rooted at this node into an integer with an empty context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_int(&self) -> Result<IntType, EvalexprError> {
pub fn eval_int(&self) -> EvalexprResult<IntType> {
self.eval_int_with_context(&EmptyContext)
}
@ -154,21 +159,21 @@ 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(&self) -> Result<FloatType, EvalexprError> {
pub fn eval_number(&self) -> EvalexprResult<FloatType> {
self.eval_number_with_context(&EmptyContext)
}
/// Evaluates the operator tree rooted at this node into a boolean with an empty context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_boolean(&self) -> Result<bool, EvalexprError> {
pub fn eval_boolean(&self) -> EvalexprResult<bool> {
self.eval_boolean_with_context(&EmptyContext)
}
/// Evaluates the operator tree rooted at this node into a tuple with an empty context.
///
/// Fails, if one of the operators in the expression tree fails.
pub fn eval_tuple(&self) -> Result<TupleType, EvalexprError> {
pub fn eval_tuple(&self) -> EvalexprResult<TupleType> {
self.eval_tuple_with_context(&EmptyContext)
}
@ -184,11 +189,7 @@ impl Node {
self.children().len() == self.operator().argument_amount()
}
fn insert_back_prioritized(
&mut self,
node: Node,
is_root_node: bool,
) -> Result<(), EvalexprError> {
fn insert_back_prioritized(&mut self, node: Node, is_root_node: bool) -> EvalexprResult<()> {
if self.operator().precedence() < node.operator().precedence() || is_root_node
// Right-to-left chaining
|| (self.operator().precedence() == node.operator().precedence() && !self.operator().is_left_to_right() && !node.operator().is_left_to_right())
@ -228,7 +229,7 @@ impl Node {
}
}
pub(crate) fn tokens_to_operator_tree(tokens: Vec<Token>) -> Result<Node, EvalexprError> {
pub(crate) fn tokens_to_operator_tree(tokens: Vec<Token>) -> EvalexprResult<Node> {
let mut root = vec![Node::root_node()];
let mut last_token_is_rightsided_value = false;
let mut token_iter = tokens.iter().peekable();

View File

@ -1,4 +1,4 @@
use error::EvalexprError;
use error::{EvalexprError, EvalexprResult};
mod display;
pub mod value_type;
@ -77,7 +77,7 @@ impl Value {
}
/// Clones the value stored in `self` as `String`, or returns `Err` if `self` is not a `Value::String`.
pub fn as_string(&self) -> Result<String, EvalexprError> {
pub fn as_string(&self) -> EvalexprResult<String> {
match self {
Value::String(string) => Ok(string.clone()),
value => Err(EvalexprError::expected_string(value.clone())),
@ -85,7 +85,7 @@ impl Value {
}
/// Clones the value stored in `self` as `IntType`, or returns `Err` if `self` is not a `Value::Int`.
pub fn as_int(&self) -> Result<IntType, EvalexprError> {
pub fn as_int(&self) -> EvalexprResult<IntType> {
match self {
Value::Int(i) => Ok(*i),
value => Err(EvalexprError::expected_int(value.clone())),
@ -93,7 +93,7 @@ impl Value {
}
/// Clones the value stored in `self` as `FloatType`, or returns `Err` if `self` is not a `Value::Float`.
pub fn as_float(&self) -> Result<FloatType, EvalexprError> {
pub fn as_float(&self) -> EvalexprResult<FloatType> {
match self {
Value::Float(f) => Ok(*f),
value => Err(EvalexprError::expected_float(value.clone())),
@ -102,7 +102,7 @@ impl Value {
/// Clones the value stored in `self` as `FloatType`, or returns `Err` if `self` is not a `Value::Float` or `Value::Int`.
/// Note that this method silently converts `IntType` to `FloatType`, if `self` is a `Value::Int`.
pub fn as_number(&self) -> Result<FloatType, EvalexprError> {
pub fn as_number(&self) -> EvalexprResult<FloatType> {
match self {
Value::Float(f) => Ok(*f),
Value::Int(i) => Ok(*i as FloatType),
@ -111,7 +111,7 @@ impl Value {
}
/// Clones the value stored in `self` as `bool`, or returns `Err` if `self` is not a `Value::Boolean`.
pub fn as_boolean(&self) -> Result<bool, EvalexprError> {
pub fn as_boolean(&self) -> EvalexprResult<bool> {
match self {
Value::Boolean(boolean) => Ok(*boolean),
value => Err(EvalexprError::expected_boolean(value.clone())),
@ -119,7 +119,7 @@ impl Value {
}
/// Clones the value stored in `self` as `TupleType`, or returns`Err` if `self` is not a `Value::Tuple`.
pub fn as_tuple(&self) -> Result<TupleType, EvalexprError> {
pub fn as_tuple(&self) -> EvalexprResult<TupleType> {
match self {
Value::Tuple(tuple) => Ok(tuple.clone()),
value => Err(EvalexprError::expected_tuple(value.clone())),
@ -163,7 +163,7 @@ impl From<TupleType> for Value {
}
}
impl From<Value> for Result<Value, EvalexprError> {
impl From<Value> for EvalexprResult<Value> {
fn from(value: Value) -> Self {
Ok(value)
}