From 7d0abc8406c0409c1d14d30d972c0bcfaa07c05a Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 27 Mar 2019 18:45:33 +0100 Subject: [PATCH] Rename `as_float` to `as_number` and add strict `as_float` Relates to #20 --- CHANGELOG.md | 1 + src/operator/mod.rs | 24 ++++++++++++------------ src/value/mod.rs | 18 +++++++++++++++++- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cf4be6..9b65f70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * Rename `Error` to `EvalexprError` * Rename `Configuration` to `Context` * Rename `HashMapConfiguration` to `HashMapContext` and `EmptyConfiguration` to `EmptyContext` + * Rename `Value::as_float` to `Value::as_number` and add new `Value::as_float` that fails if value is an integer ### Fixed diff --git a/src/operator/mod.rs b/src/operator/mod.rs index f803679..83b5fbc 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -154,7 +154,7 @@ impl Operator for Add { } } else { Ok(Value::Float( - arguments[0].as_float().unwrap() + arguments[1].as_float().unwrap(), + arguments[0].as_number().unwrap() + arguments[1].as_number().unwrap(), )) } } @@ -190,7 +190,7 @@ impl Operator for Sub { } } else { Ok(Value::Float( - arguments[0].as_float().unwrap() - arguments[1].as_float().unwrap(), + arguments[0].as_number().unwrap() - arguments[1].as_number().unwrap(), )) } } @@ -221,7 +221,7 @@ impl Operator for Neg { Err(EvalexprError::negation_error(arguments[0].clone())) } } else { - Ok(Value::Float(-arguments[0].as_float().unwrap())) + Ok(Value::Float(-arguments[0].as_number().unwrap())) } } } @@ -256,7 +256,7 @@ impl Operator for Mul { } } else { Ok(Value::Float( - arguments[0].as_float().unwrap() * arguments[1].as_float().unwrap(), + arguments[0].as_number().unwrap() * arguments[1].as_number().unwrap(), )) } } @@ -292,7 +292,7 @@ impl Operator for Div { } } else { Ok(Value::Float( - arguments[0].as_float().unwrap() / arguments[1].as_float().unwrap(), + arguments[0].as_number().unwrap() / arguments[1].as_number().unwrap(), )) } } @@ -328,7 +328,7 @@ impl Operator for Mod { } } else { Ok(Value::Float( - arguments[0].as_float().unwrap() % arguments[1].as_float().unwrap(), + arguments[0].as_number().unwrap() % arguments[1].as_number().unwrap(), )) } } @@ -354,9 +354,9 @@ impl Operator for Exp { Ok(Value::Float( arguments[0] - .as_float() + .as_number() .unwrap() - .powf(arguments[1].as_float().unwrap()), + .powf(arguments[1].as_number().unwrap()), )) } } @@ -434,7 +434,7 @@ impl Operator for Gt { Ok(Value::Boolean(false)) } } else { - if arguments[0].as_float().unwrap() > arguments[1].as_float().unwrap() { + if arguments[0].as_number().unwrap() > arguments[1].as_number().unwrap() { Ok(Value::Boolean(true)) } else { Ok(Value::Boolean(false)) @@ -468,7 +468,7 @@ impl Operator for Lt { Ok(Value::Boolean(false)) } } else { - if arguments[0].as_float().unwrap() < arguments[1].as_float().unwrap() { + if arguments[0].as_number().unwrap() < arguments[1].as_number().unwrap() { Ok(Value::Boolean(true)) } else { Ok(Value::Boolean(false)) @@ -502,7 +502,7 @@ impl Operator for Geq { Ok(Value::Boolean(false)) } } else { - if arguments[0].as_float().unwrap() >= arguments[1].as_float().unwrap() { + if arguments[0].as_number().unwrap() >= arguments[1].as_number().unwrap() { Ok(Value::Boolean(true)) } else { Ok(Value::Boolean(false)) @@ -536,7 +536,7 @@ impl Operator for Leq { Ok(Value::Boolean(false)) } } else { - if arguments[0].as_float().unwrap() <= arguments[1].as_float().unwrap() { + if arguments[0].as_number().unwrap() <= arguments[1].as_number().unwrap() { Ok(Value::Boolean(true)) } else { Ok(Value::Boolean(false)) diff --git a/src/value/mod.rs b/src/value/mod.rs index 88792cb..d7345b9 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -52,6 +52,14 @@ impl Value { } } + /// Returns true if `self` is a `Value::Int` or `Value::Float`. + pub fn is_number(&self) -> bool { + match self { + Value::Int(_) | Value::Float(_) => true, + _ => false, + } + } + /// Returns true if `self` is a `Value::Boolean`. pub fn is_boolean(&self) -> bool { match self { @@ -84,9 +92,17 @@ 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 { + match self { + Value::Float(f) => Ok(*f), + value => Err(EvalexprError::expected_float(value.clone())), + } + } + /// 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_float(&self) -> Result { + pub fn as_number(&self) -> Result { match self { Value::Float(f) => Ok(*f), Value::Int(i) => Ok(*i as FloatType),