dark_vm/errors/
error_kind.rs

1//! The ErrorKind enum maintains the different errors that can occur during the execution of the program.
2//! This allows for uniformity across the various errors because the error messages are the same.
3//! This also increases readibility within the code, because the ErrorKind's are more descriptive.
4
5pub enum ErrorKind {
6    UnrecognizedArgument(String),
7
8    UnknownCharacter,
9    InvalidNumberFormat,
10    InvalidLabelName,
11    UnterminatedString,
12
13    DuplicateLabel,
14    NoMainLabel,
15    EndWithoutLabel,
16
17    EmptyStack,
18    ExpectedArgs(usize),
19    ValueMismatch(String, String),
20    UnsupportedOperation(String, String),
21    NoEndOfLabel,
22    DivisionByZero,
23    OutOfBounds(usize, usize),
24    UndefinedVariable,
25    UndefinedLabel,
26}
27
28/// Converts the ErrorKind into a String.
29/// This is used in the prettify method to produce the error messages needed.
30impl Into<String> for ErrorKind {
31    fn into(self) -> String {
32        match self {
33            ErrorKind::UnrecognizedArgument(arg) => {
34                return format!("The Argument '{}' Is Not A Valid Argument.", arg)
35            }
36
37            ErrorKind::UnknownCharacter => "Unknown Character Found Here.",
38            ErrorKind::InvalidNumberFormat => "Invalid Number Format.",
39            ErrorKind::InvalidLabelName => "Invalid Label Name.",
40            ErrorKind::UnterminatedString => "Expected The End Of This String.",
41
42            ErrorKind::DuplicateLabel => "Another Label With This Name Was Defined Already.",
43            ErrorKind::NoMainLabel => "A Main Label Could Not Be Found.",
44            ErrorKind::EndWithoutLabel => "Found An End That Is Not Associated With A Label.",
45
46            ErrorKind::EmptyStack => "Tried To Pop From An Empty Stack.",
47            ErrorKind::ExpectedArgs(arg_amt) => {
48                return format!(
49                    "Expected {} More {}.",
50                    arg_amt,
51                    if arg_amt == 1 {
52                        "Argument"
53                    } else {
54                        "Arguments"
55                    }
56                )
57            }
58            ErrorKind::ValueMismatch(expected, actual) => {
59                return format!(
60                    "Expected The Value {:#?}, But Found The Value {:#?}.",
61                    expected, actual,
62                )
63            }
64            ErrorKind::UnsupportedOperation(operation, operand) => {
65                return format!(
66                    "The Operation '{}' Can Not Be Applied To {}",
67                    operation, operand
68                )
69            }
70            ErrorKind::NoEndOfLabel => "No 'end' Could Be Found To This Label.",
71            ErrorKind::DivisionByZero => "Tried To Divide By 0.",
72            ErrorKind::OutOfBounds(beginning, end) => {
73                return format!(
74                    "An Invalid Index Was Given. The Index Has To Be Between {} And {} Exclusive.",
75                    beginning, end
76                )
77            }
78            ErrorKind::UndefinedVariable => "Tried To Use A Variable That Has Not Been Defined.",
79            ErrorKind::UndefinedLabel => "Tried To Use A Label That Has Not Been Defined.",
80        }
81        .to_owned()
82    }
83}