asn1_compiler/
error.rs

1//! Errors
2
3use crate::tokenizer::Token;
4
5/// Error Type for the crate.
6///
7/// Defines separate variants for the Tokenization, Parsing and Symbol Resolution Errors.
8#[derive(Debug)]
9pub enum Error {
10    /// Error when tokenizing the ASN.1 Input (Cause, Line, Column)
11    TokenizeError(usize, usize, usize),
12
13    /// Unexpected End of Tokens while parsing tokens.
14    UnexpectedEndOfTokens,
15
16    /// Unexpected Token while parsing tokens.
17    UnexpectedToken(String, Token),
18
19    /// Invalid token while parsing.
20    InvalidToken(Token),
21
22    /// Unknown Object Identifier Name (For Well known names).
23    UnknownOIDName(Token),
24
25    /// A Generic parsing error.
26    ParseError(String),
27
28    /// Error while resolving the parsed definitions.
29    ResolveError(String),
30
31    /// Error related to resolving constraints for a type.
32    ConstraintError(String),
33
34    /// Error related to code generation from resolved types.
35    CodeGenerationError(String),
36
37    /// Any IO Error during compilation
38    IOError(String),
39}
40
41impl std::fmt::Display for Error {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Error::TokenizeError(ref cause, ref l, ref c) => {
45                write!(
46                    f,
47                    "Tokenize Error ({}) at Line: {}, Column: {}",
48                    cause, l, c
49                )
50            }
51            Error::UnexpectedEndOfTokens => {
52                write!(f, "Unexpected end of tokens!")
53            }
54            Error::UnexpectedToken(ref un, ref tok) => {
55                write!(
56                    f,
57                    "Expected '{}'. Found '{}' at {}.",
58                    un,
59                    tok.text,
60                    tok.span().start()
61                )
62            }
63            Error::InvalidToken(ref tok) => {
64                write!(
65                    f,
66                    "Token Value '{}' is invalid at {}.",
67                    tok.text,
68                    tok.span().start()
69                )
70            }
71            Error::UnknownOIDName(ref tok) => {
72                write!(f,
73                    "Named only Identifier '{}' in Object Identifier is not one of the well-known one at {}",
74                    tok.text,
75                    tok.span().start()
76                )
77            }
78            Error::ParseError(ref errstr) => {
79                write!(f, "Parsing Error: {}", errstr)
80            }
81            Error::ResolveError(ref errstr) => {
82                write!(f, "Compilation Error: Resolve: {}", errstr)
83            }
84            Error::ConstraintError(ref errstr) => {
85                write!(f, "Compilation Error: Constraint: {}", errstr)
86            }
87            Error::CodeGenerationError(ref errstr) => {
88                write!(f, "Compilation Error: Code Generation: {}", errstr)
89            }
90            Error::IOError(ref errstr) => {
91                write!(f, "Compilation Error: IO Error: {}", errstr)
92            }
93        }
94    }
95}
96
97impl std::error::Error for Error {}
98
99#[doc(hidden)]
100impl From<Error> for std::io::Error {
101    fn from(e: Error) -> Self {
102        std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{}", e))
103    }
104}
105
106// Macros: Use the Macros for returning Errors instead of creating the types inside any of the
107// routines. This allows us to later log inside the macros if needed.
108macro_rules! unexpected_token {
109    ($lit: literal, $tok: expr) => {
110        crate::error::Error::UnexpectedToken($lit.to_string(), $tok.clone())
111    };
112}
113
114macro_rules! parse_error_log {
115    ($($arg: tt)*) => {
116        {
117            log::error!("{}", format!($($arg)*));
118            crate::error::Error::ParseError(format!($($arg)*))
119        }
120    };
121}
122
123macro_rules! parse_error {
124    ($($arg: tt)*) => {
125        crate::error::Error::ParseError(format!($($arg)*))
126    };
127}
128
129macro_rules! unexpected_end {
130    () => {
131        crate::error::Error::UnexpectedEndOfTokens
132    };
133}
134
135macro_rules! invalid_token {
136    ($tok: expr) => {
137        crate::error::Error::InvalidToken($tok.clone())
138    };
139}
140
141macro_rules! unknown_oid_name {
142    ($tok: expr) => {
143        crate::error::Error::UnknownOIDName($tok.clone())
144    };
145}
146
147macro_rules! resolve_error {
148    ($($arg: tt)*) => {
149        crate::error::Error::ResolveError(format!($($arg)*))
150    };
151}
152
153macro_rules! code_generate_error {
154    ($($arg: tt)*) => {
155        crate::error::Error::CodeGenerationError(format!($($arg)*))
156    };
157}
158
159macro_rules! io_error {
160    ($($arg: tt)*) => {
161        crate::error::Error::IOError(format!($($arg)*))
162    };
163}
164
165macro_rules! constraint_error {
166    ($($arg: tt)*) => {
167        crate::error::Error::ConstraintError(format!($($arg)*))
168    };
169}