xshade_parser/error/
mod.rs1use ::std::convert::From;
2use ::std::error::Error;
3use ::std::fmt;
4use ::ast::Span;
5
6pub type ParseResult<T> = Result<T, ParseError>;
7
8#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
9pub enum ParseErrorKind {
10 Unknown = 0,
11 UnrecognizedCharacter = 1,
12 InvalidTopLevelItem = 1000,
13 InvalidStructName = 2000,
14 InvalidFunctionName = 3000,
15 MissingArgumentList = 3001,
16 MissingOpenCurlyBraces = 5000,
17 MissingClosingCurlyBraces = 5001,
18 MissingError = 999999,
19}
20
21impl From<u32> for ParseErrorKind {
22 fn from(value: u32) -> Self {
23 match value {
24 1 => ParseErrorKind::UnrecognizedCharacter,
25 1000 => ParseErrorKind::InvalidTopLevelItem,
26 2000 => ParseErrorKind::InvalidStructName,
27 3000 => ParseErrorKind::InvalidFunctionName,
28 3001 => ParseErrorKind::MissingArgumentList,
29 5000 => ParseErrorKind::MissingOpenCurlyBraces,
30 5001 => ParseErrorKind::MissingClosingCurlyBraces,
31 999999 => ParseErrorKind::MissingError,
32
33 _ => ParseErrorKind::Unknown,
34 }
35 }
36}
37
38#[derive(Debug, Serialize, Deserialize)]
39pub struct ParseError {
40 span: Span,
41 kind: ParseErrorKind,
42}
43
44impl ParseError {
45 pub fn new(span: Span, kind: ParseErrorKind) -> ParseError {
46 ParseError {
47 span,
48 kind,
49 }
50 }
51}
52
53impl Error for ParseError {
54 fn description(&self) -> &str {
55 "ParseError"
56 }
57
58 fn cause(&self) -> Option<&Error> {
59 None
60 }
61}
62
63impl fmt::Display for ParseError {
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 match self.kind {
66 ParseErrorKind::Unknown => write!(f, "ParseError: Unknown error at {}.", self.span),
67 ParseErrorKind::UnrecognizedCharacter => write!(f, "ParseError: Unrecognized character at {}.", self.span),
68 ParseErrorKind::InvalidTopLevelItem => write!(f, "ParseError: Invalid top level item at {}.", self.span),
69 ParseErrorKind::MissingOpenCurlyBraces => write!(f, "ParseError: Missing opening curly braces at {}.", self.span),
70 ParseErrorKind::MissingClosingCurlyBraces => write!(f, "ParseError: Missing closing curly braces at {}.", self.span),
71 ParseErrorKind::InvalidStructName => write!(f, "ParseError: Invalid struct name at {}.", self.span),
72 ParseErrorKind::InvalidFunctionName => write!(f, "ParseError: Invalid function name at {}.", self.span),
73 ParseErrorKind::MissingArgumentList => write!(f, "ParseError: Missing argument list at {}.", self.span),
74 ParseErrorKind::MissingError => write!(f, "ParseError: A custom error is missing in a nom parser."),
75 }
76 }
77}