Skip to main content

antlr_rust_codegen/
error.rs

1use std::fmt;
2use std::io;
3use std::path::PathBuf;
4
5/// Broad category of a generator failure.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7#[non_exhaustive]
8pub enum ErrorKind {
9    Configuration,
10    Compilation,
11    Generation,
12    Filesystem,
13}
14
15/// Severity of one grammar diagnostic.
16#[derive(Clone, Copy, Debug, Eq, PartialEq)]
17pub enum Severity {
18    Warning,
19    Error,
20}
21
22/// A located grammar compiler diagnostic.
23#[derive(Clone, Debug, Eq, PartialEq)]
24pub struct Diagnostic {
25    code: &'static str,
26    severity: Severity,
27    message: String,
28    path: PathBuf,
29    line: Option<usize>,
30    column: Option<usize>,
31}
32
33impl Diagnostic {
34    pub(crate) fn new(
35        code: &'static str,
36        severity: Severity,
37        message: String,
38        path: PathBuf,
39        position: Option<(usize, usize)>,
40    ) -> Self {
41        Self {
42            code,
43            severity,
44            message,
45            path,
46            line: position.map(|(line, _)| line),
47            column: position.map(|(_, column)| column),
48        }
49    }
50
51    pub const fn code(&self) -> &'static str {
52        self.code
53    }
54
55    pub const fn severity(&self) -> Severity {
56        self.severity
57    }
58
59    pub fn message(&self) -> &str {
60        &self.message
61    }
62
63    pub fn path(&self) -> &std::path::Path {
64        &self.path
65    }
66
67    pub const fn line(&self) -> Option<usize> {
68        self.line
69    }
70
71    pub const fn column(&self) -> Option<usize> {
72        self.column
73    }
74}
75
76/// Structured error returned by [`crate::Builder`].
77#[derive(Debug)]
78pub struct Error {
79    kind: ErrorKind,
80    message: String,
81    diagnostics: Vec<Diagnostic>,
82    source: Option<io::Error>,
83}
84
85impl Error {
86    pub(crate) fn configuration(message: impl Into<String>) -> Self {
87        Self {
88            kind: ErrorKind::Configuration,
89            message: message.into(),
90            diagnostics: Vec::new(),
91            source: None,
92        }
93    }
94
95    pub(crate) const fn compilation(message: String, diagnostics: Vec<Diagnostic>) -> Self {
96        Self {
97            kind: ErrorKind::Compilation,
98            message,
99            diagnostics,
100            source: None,
101        }
102    }
103
104    pub(crate) fn generation(source: io::Error) -> Self {
105        let kind = if matches!(
106            source.kind(),
107            io::ErrorKind::NotFound
108                | io::ErrorKind::PermissionDenied
109                | io::ErrorKind::AlreadyExists
110                | io::ErrorKind::ReadOnlyFilesystem
111        ) {
112            ErrorKind::Filesystem
113        } else {
114            ErrorKind::Generation
115        };
116        Self {
117            kind,
118            message: source.to_string(),
119            diagnostics: Vec::new(),
120            source: Some(source),
121        }
122    }
123
124    pub const fn kind(&self) -> ErrorKind {
125        self.kind
126    }
127
128    pub fn diagnostics(&self) -> &[Diagnostic] {
129        &self.diagnostics
130    }
131
132    pub(crate) fn into_io_error(mut self) -> io::Error {
133        self.source.take().unwrap_or_else(|| {
134            let kind =
135                if self.kind == ErrorKind::Configuration || self.kind == ErrorKind::Compilation {
136                    io::ErrorKind::InvalidInput
137                } else {
138                    io::ErrorKind::Other
139                };
140            io::Error::new(kind, self.message)
141        })
142    }
143}
144
145impl fmt::Display for Error {
146    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
147        formatter.write_str(&self.message)
148    }
149}
150
151impl std::error::Error for Error {
152    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
153        self.source
154            .as_ref()
155            .map(|source| -> &(dyn std::error::Error + 'static) { source })
156    }
157}
158
159impl From<io::Error> for Error {
160    fn from(source: io::Error) -> Self {
161        Self::generation(source)
162    }
163}