rsx_compiler/errors/
mod.rs

1use std::error::Error as StdError;
2use std::fmt;
3use crate::SourceId;
4
5mod for_proc_macro2;
6mod for_syn;
7mod for_source_map;
8
9// Define a standard result type for the library
10pub type Result<T> = std::result::Result<T, TranslatorError>;
11
12/// Unified errors type for the rsx-compiler library.
13#[derive(Debug)]
14pub enum TranslatorError {
15    Io(std::io::Error),
16    SourceNotFound(SourceId),
17    InvalidAstData(String),
18    MissingAstData(String),
19    DecodeError { format: String, message: String },
20    SourceMap(oxc_sourcemap::Error),
21    Utf8(std::string::FromUtf8Error),
22    Generation(String),
23}
24
25impl fmt::Display for TranslatorError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            TranslatorError::Io(err) => write!(f, "I/O Error: {}", err),
29            TranslatorError::SourceNotFound(id) => {
30                write!(f, "Source content not found for id: {}", id)
31            }
32            TranslatorError::InvalidAstData(kind) => {
33                write!(f, "Invalid AST data for node kind: {}", kind)
34            }
35            TranslatorError::MissingAstData(kind) => {
36                write!(f, "Missing AST data for node kind: {}", kind)
37            }
38            TranslatorError::SourceMap(err) => write!(f, "Source map generation errors: {}", err),
39            TranslatorError::Utf8(err) => write!(f, "UTF-8 conversion errors: {}", err),
40            TranslatorError::Generation(msg) => write!(f, "Generation failed: {}", msg),
41            TranslatorError::DecodeError { format, message } => {
42                write!(f, "Decoding errors: {}: {}", format, message)
43            }
44        }
45    }
46}
47
48impl StdError for TranslatorError {
49    fn source(&self) -> Option<&(dyn StdError + 'static)> {
50        match self {
51            TranslatorError::Io(err) => Some(err),
52            TranslatorError::SourceMap(err) => Some(err),
53            TranslatorError::Utf8(err) => Some(err),
54            _ => None,
55        }
56    }
57}
58
59impl From<std::io::Error> for TranslatorError {
60    fn from(err: std::io::Error) -> Self {
61        TranslatorError::Io(err)
62    }
63}
64
65
66
67impl From<std::string::FromUtf8Error> for TranslatorError {
68    fn from(err: std::string::FromUtf8Error) -> Self {
69        TranslatorError::Utf8(err)
70    }
71}
72