TypeScript_Rust_Compiler/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, CompilerError>;
7
8#[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 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 pub fn type_error(message: impl Into<String>) -> Self {
49 Self::TypeError {
50 message: message.into(),
51 }
52 }
53
54 pub fn semantic_error(message: impl Into<String>) -> Self {
56 Self::SemanticError {
57 message: message.into(),
58 }
59 }
60
61 pub fn generation_error(message: impl Into<String>) -> Self {
63 Self::GenerationError {
64 message: message.into(),
65 }
66 }
67
68 pub fn unsupported_feature(feature: impl Into<String>) -> Self {
70 Self::UnsupportedFeature {
71 feature: feature.into(),
72 }
73 }
74
75 pub fn internal_error(message: impl Into<String>) -> Self {
77 Self::InternalError {
78 message: message.into(),
79 }
80 }
81}