From d77fa158643d5cbf372c3b6f24ed16cc63040c61 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 27 Mar 2019 19:08:47 +0100 Subject: [PATCH] Use `EvalexprResult` everywhere Relates to #31 --- README.md | 2 +- src/context/mod.rs | 4 ++-- src/error/mod.rs | 8 ++++---- src/function/mod.rs | 10 +++++----- src/interface/mod.rs | 40 ++++++++++++++++------------------------ src/lib.rs | 2 +- src/operator/mod.rs | 44 ++++++++++++++++++++++---------------------- src/token/mod.rs | 6 +++--- src/tree/mod.rs | 43 ++++++++++++++++++++++--------------------- src/value/mod.rs | 16 ++++++++-------- 10 files changed, 84 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 62a201d..ff438d1 100644 --- a/README.md +++ b/README.md @@ -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`. +The `function` is a boxed `Fn(&[Value]) -> EvalexprResult`. 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`. diff --git a/src/context/mod.rs b/src/context/mod.rs index b2ce29f..d2b2849 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -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(()) } diff --git a/src/error/mod.rs b/src/error/mod.rs index 07cac90..03c7736 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -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 { +pub fn expect_boolean(actual: &Value) -> EvalexprResult { match actual { Value::Boolean(boolean) => Ok(*boolean), _ => Err(EvalexprError::expected_boolean(actual.clone())), diff --git a/src/function/mod.rs b/src/function/mod.rs index a4bf5da..29f850a 100644 --- a/src/function/mod.rs +++ b/src/function/mod.rs @@ -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, - function: Box Result>, + function: Box EvalexprResult>, } 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`. + /// The `function` is a boxed function that takes a slice of values and returns a `EvalexprResult`. pub fn new( argument_amount: Option, - function: Box Result>, + function: Box EvalexprResult>, ) -> Self { Self { argument_amount, @@ -39,7 +39,7 @@ impl Function { } } - pub(crate) fn call(&self, arguments: &[Value]) -> Result { + pub(crate) fn call(&self, arguments: &[Value]) -> EvalexprResult { if let Some(argument_amount) = self.argument_amount { error::expect_function_argument_amount(arguments.len(), argument_amount)?; } diff --git a/src/interface/mod.rs b/src/interface/mod.rs index e2b9b52..72fce1d 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -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 { +pub fn eval(string: &str) -> EvalexprResult { eval_with_context(string, &EmptyContext) } @@ -39,7 +40,7 @@ pub fn eval(string: &str) -> Result { /// ``` /// /// *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 { +pub fn eval_with_context(string: &str, context: &Context) -> EvalexprResult { 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 Result { +pub fn build_operator_tree(string: &str) -> EvalexprResult { 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 { +pub fn eval_string(string: &str) -> EvalexprResult { 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 { /// 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 { +pub fn eval_int(string: &str) -> EvalexprResult { 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 { /// 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 { +pub fn eval_float(string: &str) -> EvalexprResult { 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 { /// 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 { +pub fn eval_number(string: &str) -> EvalexprResult { 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 { /// 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 { +pub fn eval_boolean(string: &str) -> EvalexprResult { 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 { /// 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 { +pub fn eval_tuple(string: &str) -> EvalexprResult { 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 { /// 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 { +pub fn eval_string_with_context(string: &str, context: &Context) -> EvalexprResult { 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 Result { +pub fn eval_int_with_context(string: &str, context: &Context) -> EvalexprResult { 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 Result { +pub fn eval_float_with_context(string: &str, context: &Context) -> EvalexprResult { 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 { +pub fn eval_number_with_context(string: &str, context: &Context) -> EvalexprResult { 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 { +pub fn eval_boolean_with_context(string: &str, context: &Context) -> EvalexprResult { 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 Result { +pub fn eval_tuple_with_context(string: &str, context: &Context) -> EvalexprResult { match eval_with_context(string, context) { Ok(Value::Tuple(tuple)) => Ok(tuple), Ok(value) => Err(EvalexprError::expected_tuple(value)), diff --git a/src/lib.rs b/src/lib.rs index 87aee07..a0de8b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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`. +//! The `function` is a boxed `Fn(&[Value]) -> EvalexprResult`. //! 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`. //! diff --git a/src/operator/mod.rs b/src/operator/mod.rs index 83b5fbc..d5e8833 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -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; + fn eval(&self, arguments: &[Value], context: &Context) -> EvalexprResult; } #[derive(Debug)] @@ -118,7 +118,7 @@ impl Operator for RootNode { 1 } - fn eval(&self, arguments: &[Value], _context: &Context) -> Result { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], _context: &Context) -> EvalexprResult { 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 { + fn eval(&self, _arguments: &[Value], context: &Context) -> EvalexprResult { 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 { + fn eval(&self, arguments: &[Value], context: &Context) -> EvalexprResult { expect_operator_argument_amount(arguments.len(), 1)?; let arguments = if let Value::Tuple(arguments) = &arguments[0] { diff --git a/src/token/mod.rs b/src/token/mod.rs index be1ede8..379c229 100644 --- a/src/token/mod.rs +++ b/src/token/mod.rs @@ -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 { } /// Resolves all partial tokens by converting them to complex tokens. -fn resolve_literals(mut tokens: &[PartialToken]) -> Result, EvalexprError> { +fn resolve_literals(mut tokens: &[PartialToken]) -> EvalexprResult> { 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, EvalexprE Ok(result) } -pub(crate) fn tokenize(string: &str) -> Result, EvalexprError> { +pub(crate) fn tokenize(string: &str) -> EvalexprResult> { resolve_literals(&str_to_tokens(string)) } diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 6f501f7..f215db4 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -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 { + pub fn eval_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval(&self) -> EvalexprResult { 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 { + pub fn eval_string_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval_float_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval_int_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval_number_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval_boolean_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval_tuple_with_context(&self, context: &Context) -> EvalexprResult { 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 { + pub fn eval_string(&self) -> EvalexprResult { 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 { + pub fn eval_float(&self) -> EvalexprResult { 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 { + pub fn eval_int(&self) -> EvalexprResult { 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 { + pub fn eval_number(&self) -> EvalexprResult { 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 { + pub fn eval_boolean(&self) -> EvalexprResult { 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 { + pub fn eval_tuple(&self) -> EvalexprResult { 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) -> Result { +pub(crate) fn tokens_to_operator_tree(tokens: Vec) -> EvalexprResult { let mut root = vec![Node::root_node()]; let mut last_token_is_rightsided_value = false; let mut token_iter = tokens.iter().peekable(); diff --git a/src/value/mod.rs b/src/value/mod.rs index d7345b9..1de2200 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -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 { + pub fn as_string(&self) -> EvalexprResult { 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 { + pub fn as_int(&self) -> EvalexprResult { 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 { + pub fn as_float(&self) -> EvalexprResult { 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 { + pub fn as_number(&self) -> EvalexprResult { 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 { + pub fn as_boolean(&self) -> EvalexprResult { 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 { + pub fn as_tuple(&self) -> EvalexprResult { match self { Value::Tuple(tuple) => Ok(tuple.clone()), value => Err(EvalexprError::expected_tuple(value.clone())), @@ -163,7 +163,7 @@ impl From for Value { } } -impl From for Result { +impl From for EvalexprResult { fn from(value: Value) -> Self { Ok(value) }