use fastn_type::evalexpr::{
token, tree, value::TupleType, Context, ContextWithMutableVariables, EmptyType, EvalexprError,
EvalexprResult, ExprNode, FloatType, HashMapContext, IntType, Value, EMPTY_VALUE,
};
pub fn eval(string: &str) -> EvalexprResult<Value> {
eval_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<Value> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
}
pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<Value> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context)
}
pub fn build_operator_tree(string: &str) -> EvalexprResult<ExprNode> {
tree::tokens_to_operator_tree(token::tokenize(string)?)
}
pub fn eval_string(string: &str) -> EvalexprResult<String> {
eval_string_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_int(string: &str) -> EvalexprResult<IntType> {
eval_int_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_float(string: &str) -> EvalexprResult<FloatType> {
eval_float_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_number(string: &str) -> EvalexprResult<FloatType> {
eval_number_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_boolean(string: &str) -> EvalexprResult<bool> {
eval_boolean_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
eval_tuple_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_empty(string: &str) -> EvalexprResult<EmptyType> {
eval_empty_with_context_mut(string, &mut HashMapContext::new())
}
pub fn eval_string_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<String> {
match eval_with_context(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
Err(error) => Err(error),
}
}
pub fn eval_int_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<IntType> {
match eval_with_context(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
Err(error) => Err(error),
}
}
pub fn eval_float_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<FloatType> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
Err(error) => Err(error),
}
}
pub fn eval_number_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<FloatType> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(int as FloatType),
Ok(value) => Err(EvalexprError::expected_number(value)),
Err(error) => Err(error),
}
}
pub fn eval_boolean_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<bool> {
match eval_with_context(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
Err(error) => Err(error),
}
}
pub fn eval_tuple_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<TupleType> {
match eval_with_context(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
Err(error) => Err(error),
}
}
pub fn eval_empty_with_context<C: Context>(string: &str, context: &C) -> EvalexprResult<EmptyType> {
match eval_with_context(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
Err(error) => Err(error),
}
}
pub fn eval_string_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<String> {
match eval_with_context_mut(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
Err(error) => Err(error),
}
}
pub fn eval_int_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<IntType> {
match eval_with_context_mut(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
Err(error) => Err(error),
}
}
pub fn eval_float_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<FloatType> {
match eval_with_context_mut(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
Err(error) => Err(error),
}
}
pub fn eval_number_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<FloatType> {
match eval_with_context_mut(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(int as FloatType),
Ok(value) => Err(EvalexprError::expected_number(value)),
Err(error) => Err(error),
}
}
pub fn eval_boolean_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<bool> {
match eval_with_context_mut(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
Err(error) => Err(error),
}
}
pub fn eval_tuple_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<TupleType> {
match eval_with_context_mut(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
Err(error) => Err(error),
}
}
pub fn eval_empty_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<EmptyType> {
match eval_with_context_mut(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
Err(error) => Err(error),
}
}