resynth/
err.rs

1use std::fmt;
2use std::io;
3
4/// Error code for resynth program. Think of it as base exception type for the resynth language.
5#[allow(clippy::enum_variant_names)]
6#[derive(Debug)]
7pub enum Error {
8    IoError(io::Error),
9    LexError,
10    ParseError,
11    MemoryError,
12    ImportError(String),
13    NameError,
14    TypeError,
15    RuntimeError,
16    MultipleAssignError(String),
17}
18
19impl From<io::Error> for Error {
20    fn from(e: io::Error) -> Self {
21        Self::IoError(e)
22    }
23}
24
25impl Eq for Error {}
26impl PartialEq for Error {
27    fn eq(&self, other: &Self) -> bool {
28        use Error::*;
29
30        match self {
31            IoError(a) => {
32                if let IoError(b) = other {
33                    a.kind() == b.kind()
34                } else {
35                    false
36                }
37            }
38            LexError => matches!(other, LexError),
39            ParseError => matches!(other, ParseError),
40            MemoryError => matches!(other, MemoryError),
41            ImportError(a) => {
42                if let ImportError(b) = other {
43                    a == b
44                } else {
45                    false
46                }
47            }
48            NameError => matches!(other, NameError),
49            TypeError => matches!(other, TypeError),
50            RuntimeError => matches!(other, RuntimeError),
51            MultipleAssignError(a) => {
52                if let MultipleAssignError(b) = other {
53                    a == b
54                } else {
55                    false
56                }
57            }
58        }
59    }
60}
61
62impl fmt::Display for Error {
63    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
64        use Error::*;
65
66        match self {
67            IoError(ref io) => io.fmt(fmt),
68            LexError => write!(fmt, "Lex Error"),
69            ParseError => write!(fmt, "Parse Error"),
70            MemoryError => write!(fmt, "Memory Error"),
71            ImportError(s) => write!(fmt, "Import Error: Unknown module '{}'", s),
72            NameError => write!(fmt, "Name Error"),
73            TypeError => write!(fmt, "Type Error"),
74            RuntimeError => write!(fmt, "Runtime Error"),
75            MultipleAssignError(s) => write!(fmt, "Variable '{}' reassigned", s),
76        }
77    }
78}