TypeScript_Rust_Compiler/
error.rs

1//! Error handling for the TypeScript-Rust-Compiler
2
3use thiserror::Error;
4
5/// Result type alias for the compiler
6pub type Result<T> = std::result::Result<T, CompilerError>;
7
8/// Main error type for the compiler
9#[derive(Error, Debug)]
10pub enum CompilerError {
11    #[error("IO error: {0}")]
12    Io(#[from] std::io::Error),
13
14    #[error("Parse error at {line}:{column}: {message}")]
15    ParseError {
16        line: usize,
17        column: usize,
18        message: String,
19    },
20
21    #[error("Type error: {message}")]
22    TypeError { message: String },
23
24    #[error("Semantic error: {message}")]
25    SemanticError { message: String },
26
27    #[error("Generation error: {message}")]
28    GenerationError { message: String },
29
30    #[error("Unsupported TypeScript feature: {feature}")]
31    UnsupportedFeature { feature: String },
32
33    #[error("Internal compiler error: {message}")]
34    InternalError { message: String },
35}
36
37impl CompilerError {
38    /// Create a parse error
39    pub fn parse_error(line: usize, column: usize, message: impl Into<String>) -> Self {
40        Self::ParseError {
41            line,
42            column,
43            message: message.into(),
44        }
45    }
46
47    /// Create a type error
48    pub fn type_error(message: impl Into<String>) -> Self {
49        Self::TypeError {
50            message: message.into(),
51        }
52    }
53
54    /// Create a semantic error
55    pub fn semantic_error(message: impl Into<String>) -> Self {
56        Self::SemanticError {
57            message: message.into(),
58        }
59    }
60
61    /// Create a generation error
62    pub fn generation_error(message: impl Into<String>) -> Self {
63        Self::GenerationError {
64            message: message.into(),
65        }
66    }
67
68    /// Create an unsupported feature error
69    pub fn unsupported_feature(feature: impl Into<String>) -> Self {
70        Self::UnsupportedFeature {
71            feature: feature.into(),
72        }
73    }
74
75    /// Create an internal error
76    pub fn internal_error(message: impl Into<String>) -> Self {
77        Self::InternalError {
78            message: message.into(),
79        }
80    }
81}