Implement Display and std::error::Error for Error

Relates to #19
This commit is contained in:
Sebastian Schmidt 2019-03-23 14:27:44 +02:00
parent 16bde54aa5
commit a4d1a34a0c
2 changed files with 115 additions and 4 deletions

111
src/error/display.rs Normal file
View File

@ -0,0 +1,111 @@
use std::fmt;
use Error;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use Error::*;
match self {
WrongOperatorArgumentAmount { expected, actual } => write!(
f,
"An operator expected {} arguments, but got {}.",
expected, actual
),
WrongFunctionArgumentAmount { expected, actual } => write!(
f,
"A function expected {} arguments, but got {}.",
expected, actual
),
ExpectedString { actual } => {
write!(f, "Expected a Value::String, but got {:?}.", actual)
},
ExpectedInt { actual } => write!(f, "Expected a Value::Int, but got {:?}.", actual),
ExpectedFloat { actual } => write!(f, "Expected a Value::Float, but got {:?}.", actual),
ExpectedNumber { actual } => {
write!(f, "Expected a Value::Number, but got {:?}.", actual)
},
ExpectedBoolean { actual } => {
write!(f, "Expected a Value::Boolean, but got {:?}.", actual)
},
ExpectedTuple { actual } => write!(f, "Expected a Value::Tuple, but got {:?}.", actual),
EmptyExpression => write!(
f,
"Got an empty expression that cannot be parsed into a node tree, because it \
returns nothing."
),
AppendedToLeafNode => write!(f, "Tried to append a node to a leaf node."),
PrecedenceViolation => write!(
f,
"Tried to append a node to another node with higher precedence."
),
VariableIdentifierNotFound(identifier) => write!(
f,
"Variable identifier is not bound to anything by configuration: {:?}.",
identifier
),
FunctionIdentifierNotFound(identifier) => write!(
f,
"Function identifier is not bound to anything by configuration: {:?}.",
identifier
),
TypeError { expected, actual } => {
write!(f, "Expected one of {:?}, but got {:?}.", expected, actual)
},
UnmatchedLBrace => write!(f, "Found an unmatched opening parenthesis '('."),
UnmatchedRBrace => write!(f, "Found an unmatched closing parenthesis ')'."),
UnmatchedPartialToken { first, second } => {
if let Some(second) = second {
write!(
f,
"Found a partial token '{}' that should not be followed by '{}'.",
first, second
)
} else {
write!(
f,
"Found a partial token '{}' that should be followed by another partial \
token.",
first
)
}
},
AdditionError { augend, addend } => write!(f, "Error adding {} + {}", augend, addend),
SubtractionError {
minuend,
subtrahend,
} => write!(f, "Error subtracting {} - {}", minuend, subtrahend),
NegationError { argument } => write!(f, "Error negating -{}", argument),
MultiplicationError {
multiplicand,
multiplier,
} => write!(f, "Error multiplying {} * {}", multiplicand, multiplier),
DivisionError { dividend, divisor } => {
write!(f, "Error dividing {} / {}", dividend, divisor)
},
ModulationError { dividend, divisor } => {
write!(f, "Error modulating {} % {}", dividend, divisor)
},
}
}
}

View File

@ -9,6 +9,8 @@ use crate::value::Value;
use token::PartialToken; use token::PartialToken;
use value::TupleType; use value::TupleType;
mod display;
/// Errors used in this crate. /// Errors used in this crate.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
@ -68,10 +70,6 @@ pub enum Error {
/// The given expression is empty /// The given expression is empty
EmptyExpression, EmptyExpression,
/// 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. /// Tried to append a child to a leaf node.
/// Leaf nodes cannot have children. /// Leaf nodes cannot have children.
AppendedToLeafNode, AppendedToLeafNode,
@ -274,3 +272,5 @@ pub fn expect_boolean(actual: &Value) -> Result<bool, Error> {
_ => Err(Error::expected_boolean(actual.clone())), _ => Err(Error::expected_boolean(actual.clone())),
} }
} }
impl std::error::Error for Error {}