rasn_compiler/generator/
error.rs1use core::fmt::{Display, Formatter, Result};
2use std::error::Error;
3
4use proc_macro2::LexError;
5
6use crate::intermediate::{error::GrammarError, ToplevelDefinition};
7
8#[derive(Debug, Clone, PartialEq)]
9pub struct GeneratorError {
10 pub top_level_declaration: Option<ToplevelDefinition>,
11 pub details: String,
12 pub kind: GeneratorErrorType,
13}
14
15impl GeneratorError {
16 pub fn new(tld: Option<ToplevelDefinition>, details: &str, kind: GeneratorErrorType) -> Self {
17 GeneratorError {
18 top_level_declaration: tld,
19 details: details.into(),
20 kind,
21 }
22 }
23}
24
25#[derive(Debug, Clone, PartialEq)]
26pub enum GeneratorErrorType {
27 Asn1TypeMismatch,
28 EmptyChoiceType,
29 MissingCustomSyntax,
30 SyntaxMismatch,
31 MissingClassKey,
32 Unidentified,
33 LexerError,
34 FormattingError,
35 IO,
36 NotYetInplemented,
37 Unsupported,
38}
39
40impl Error for GeneratorError {}
41
42impl Default for GeneratorError {
43 fn default() -> Self {
44 Self {
45 top_level_declaration: Default::default(),
46 details: Default::default(),
47 kind: GeneratorErrorType::Unidentified,
48 }
49 }
50}
51
52impl From<GrammarError> for GeneratorError {
53 fn from(value: GrammarError) -> Self {
54 Self {
55 details: value.details,
56 top_level_declaration: None,
57 kind: GeneratorErrorType::Unidentified,
58 }
59 }
60}
61
62impl From<LexError> for GeneratorError {
63 fn from(value: LexError) -> Self {
64 Self {
65 details: value.to_string(),
66 top_level_declaration: None,
67 kind: GeneratorErrorType::LexerError,
68 }
69 }
70}
71
72impl Display for GeneratorError {
73 fn fmt(&self, f: &mut Formatter) -> Result {
74 let name = match &self.top_level_declaration {
75 Some(ToplevelDefinition::Type(t)) => &t.name,
76 Some(ToplevelDefinition::Value(v)) => &v.name,
77 Some(ToplevelDefinition::Information(i)) => &i.name,
78 None => "",
79 };
80 write!(
81 f,
82 "{:?} generating bindings for {name}: {}",
83 self.kind, self.details
84 )
85 }
86}