Rename as_float to as_number and add strict as_float

Relates to #20
This commit is contained in:
Sebastian Schmidt 2019-03-27 18:45:33 +01:00
parent b18521b8aa
commit 7d0abc8406
3 changed files with 30 additions and 13 deletions

View File

@ -18,6 +18,7 @@
* Rename `Error` to `EvalexprError` * Rename `Error` to `EvalexprError`
* Rename `Configuration` to `Context` * Rename `Configuration` to `Context`
* Rename `HashMapConfiguration` to `HashMapContext` and `EmptyConfiguration` to `EmptyContext` * 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 ### Fixed

View File

@ -154,7 +154,7 @@ impl Operator for Add {
} }
} else { } else {
Ok(Value::Float( 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 { } else {
Ok(Value::Float( 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())) Err(EvalexprError::negation_error(arguments[0].clone()))
} }
} else { } 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 { } else {
Ok(Value::Float( 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 { } else {
Ok(Value::Float( 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 { } else {
Ok(Value::Float( 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( Ok(Value::Float(
arguments[0] arguments[0]
.as_float() .as_number()
.unwrap() .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)) Ok(Value::Boolean(false))
} }
} else { } 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)) Ok(Value::Boolean(true))
} else { } else {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
@ -468,7 +468,7 @@ impl Operator for Lt {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
} }
} else { } 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)) Ok(Value::Boolean(true))
} else { } else {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
@ -502,7 +502,7 @@ impl Operator for Geq {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
} }
} else { } 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)) Ok(Value::Boolean(true))
} else { } else {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
@ -536,7 +536,7 @@ impl Operator for Leq {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))
} }
} else { } 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)) Ok(Value::Boolean(true))
} else { } else {
Ok(Value::Boolean(false)) Ok(Value::Boolean(false))

View File

@ -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`. /// Returns true if `self` is a `Value::Boolean`.
pub fn is_boolean(&self) -> bool { pub fn is_boolean(&self) -> bool {
match self { 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<FloatType, EvalexprError> {
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`. /// 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`. /// Note that this method silently converts `IntType` to `FloatType`, if `self` is a `Value::Int`.
pub fn as_float(&self) -> Result<FloatType, EvalexprError> { pub fn as_number(&self) -> Result<FloatType, EvalexprError> {
match self { match self {
Value::Float(f) => Ok(*f), Value::Float(f) => Ok(*f),
Value::Int(i) => Ok(*i as FloatType), Value::Int(i) => Ok(*i as FloatType),