elemental/
error.rs

1//! Provides abstract error handling for the Elemental interpreter.
2
3use colored::*;
4
5/// Enumerates the types of errors available to the Elemental interpreter.
6pub enum Error {
7    CouldNotFlushOutput,
8    CouldNotReadStdin,
9    ImproperDimensions,
10    InvalidOperands,
11    InvalidOperator,
12    InvalidValue,
13    CouldNotParseNumeric,
14    UnexpectedEof,
15    CouldNotFindFunction,
16    WrongNumberOfArgs,
17    RequiresUnitMatrix,
18    SquareMatrixRequired,
19    ExpectedIdentifier,
20    ExpectedCloseParen,
21    DividedByZero,
22    UndeclaredVariable (String),
23    CouldNotReadFile (String),
24    CouldNotWriteToFile,
25    CouldNotDisplayPlot,
26}
27
28pub use Error::*;
29
30
31/// Throws errors.
32pub fn throw(error: Error) {
33    let message: String = match error {
34        CouldNotFlushOutput => "could not flush stdout".to_string(),
35        CouldNotReadStdin => "could not read stdin".to_string(),
36        ImproperDimensions => "improper dimensions".to_string(),
37        InvalidOperands => "invalid binary operands".to_string(),
38        InvalidOperator => "invalid operator".to_string(),
39        InvalidValue => "at least one value in this matrix is not a numeric literal".to_string(),
40        CouldNotParseNumeric => "could not parse numeric input".to_string(),
41        UnexpectedEof => "unexpected token or end of token stream".to_string(),
42        CouldNotFindFunction => "could not find function in standard library".to_string(),
43        WrongNumberOfArgs => "wrong number of arguments passed to function".to_string(),
44        RequiresUnitMatrix => "function requires a unit (1x1) matrix".to_string(),
45        SquareMatrixRequired => "function requires a square matrix".to_string(),
46        ExpectedIdentifier => "expected identifier".to_string(),
47        ExpectedCloseParen => "expected closing parenthesis".to_string(),
48        DividedByZero => "attempted to divide by zero".to_string(),
49        UndeclaredVariable (s) => format!("found undeclared variable {}", s),
50        CouldNotReadFile (s) => format!("could not read file {}", s),
51        CouldNotWriteToFile => "unable to export data to file".to_string(),
52        CouldNotDisplayPlot => "could not display plot in terminal".to_string(),
53    };
54
55    println!("{}: {}", "error".bold().red(), message);
56}