1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, SourceMapError>;
7
8#[derive(Debug, Error)]
10pub enum SourceMapError {
11 #[error("Invalid source map version: expected 3, got {0}")]
13 InvalidVersion(u8),
14
15 #[error("Missing required field: {0}")]
17 MissingField(&'static str),
18
19 #[error("Invalid VLQ encoding at position {position}: {message}")]
21 InvalidVlq {
22 position: usize,
24 message: String,
26 },
27
28 #[error("Invalid mapping at line {line}, column {column}: {message}")]
30 InvalidMapping {
31 line: u32,
33 column: u32,
35 message: String,
37 },
38
39 #[error("JSON parsing error: {0}")]
41 JsonError(#[from] serde_json::Error),
42
43 #[error("IO error: {0}")]
45 IoError(#[from] std::io::Error),
46
47 #[error("Index out of bounds: {index} >= {length}")]
49 IndexOutOfBounds {
50 index: usize,
52 length: usize,
54 },
55
56 #[error("Invalid source index: {0}")]
58 InvalidSourceIndex(usize),
59
60 #[error("Invalid name index: {0}")]
62 InvalidNameIndex(usize),
63
64 #[error("Source map composition error: {0}")]
66 CompositionError(String),
67}
68
69impl SourceMapError {
70 pub fn invalid_vlq(position: usize, message: impl Into<String>) -> Self {
72 SourceMapError::InvalidVlq { position, message: message.into() }
73 }
74
75 pub fn invalid_mapping(line: u32, column: u32, message: impl Into<String>) -> Self {
77 SourceMapError::InvalidMapping { line, column, message: message.into() }
78 }
79
80 pub fn index_out_of_bounds(index: usize, length: usize) -> Self {
82 SourceMapError::IndexOutOfBounds { index, length }
83 }
84}