expressive/src/error/mod.rs

55 lines
1.3 KiB
Rust
Raw Normal View History

use crate::value::Value;
2016-11-16 16:12:26 +00:00
#[derive(Debug, PartialEq)]
pub enum Error {
WrongArgumentAmount {
expected: usize,
actual: usize,
},
ExpectedNumber {
actual: Value,
},
2016-11-16 16:12:26 +00:00
/// The given expression is empty
EmptyExpression,
2016-11-16 16:12:26 +00:00
/// Tried to evaluate the root node.
/// The root node should only be used as dummy node.
EvaluatedRootNode,
/// Tried to append a child to a leaf node.
/// Leaf nodes cannot have children.
AppendedToLeafNode,
/// Tried to append a child to a node such that the precedence of the child is not higher.
PrecedenceViolation,
/// An identifier operation did not find its value in the configuration.
IdentifierNotFound,
}
impl Error {
pub fn wrong_argument_amount(actual: usize, expected: usize) -> Self {
Error::WrongArgumentAmount { actual, expected }
}
pub fn expected_number(actual: Value) -> Self {
Error::ExpectedNumber { actual }
}
}
pub fn expect_argument_amount(actual: usize, expected: usize) -> Result<(), Error> {
if actual == expected {
Ok(())
} else {
Err(Error::wrong_argument_amount(actual, expected))
}
}
pub fn expect_number(actual: &Value) -> Result<f64, Error> {
match actual {
Value::Number(number) => Ok(*number),
_ => Err(Error::expected_number(actual.clone())),
2016-11-16 16:12:26 +00:00
}
}