Skip to main content

oak_source_map/
error.rs

1//! Error types for source map operations.
2
3use thiserror::Error;
4
5/// Result type alias for source map operations.
6pub type Result<T> = std::result::Result<T, SourceMapError>;
7
8/// Error type for source map operations.
9#[derive(Debug, Error)]
10pub enum SourceMapError {
11    /// Invalid source map version.
12    #[error("Invalid source map version: expected 3, got {0}")]
13    InvalidVersion(u8),
14
15    /// Missing required field.
16    #[error("Missing required field: {0}")]
17    MissingField(&'static str),
18
19    /// Invalid VLQ encoding.
20    #[error("Invalid VLQ encoding at position {position}: {message}")]
21    InvalidVlq {
22        /// Position in the mappings string.
23        position: usize,
24        /// Error message.
25        message: String,
26    },
27
28    /// Invalid mapping.
29    #[error("Invalid mapping at line {line}, column {column}: {message}")]
30    InvalidMapping {
31        /// Line number.
32        line: u32,
33        /// Column number.
34        column: u32,
35        /// Error message.
36        message: String,
37    },
38
39    /// JSON parsing error.
40    #[error("JSON parsing error: {0}")]
41    JsonError(#[from] serde_json::Error),
42
43    /// IO error.
44    #[error("IO error: {0}")]
45    IoError(#[from] std::io::Error),
46
47    /// Index out of bounds.
48    #[error("Index out of bounds: {index} >= {length}")]
49    IndexOutOfBounds {
50        /// The index that was out of bounds.
51        index: usize,
52        /// The length of the collection.
53        length: usize,
54    },
55
56    /// Invalid source index.
57    #[error("Invalid source index: {0}")]
58    InvalidSourceIndex(usize),
59
60    /// Invalid name index.
61    #[error("Invalid name index: {0}")]
62    InvalidNameIndex(usize),
63
64    /// Source map composition error.
65    #[error("Source map composition error: {0}")]
66    CompositionError(String),
67}
68
69impl SourceMapError {
70    /// Creates a new invalid VLQ error.
71    pub fn invalid_vlq(position: usize, message: impl Into<String>) -> Self {
72        SourceMapError::InvalidVlq { position, message: message.into() }
73    }
74
75    /// Creates a new invalid mapping error.
76    pub fn invalid_mapping(line: u32, column: u32, message: impl Into<String>) -> Self {
77        SourceMapError::InvalidMapping { line, column, message: message.into() }
78    }
79
80    /// Creates a new index out of bounds error.
81    pub fn index_out_of_bounds(index: usize, length: usize) -> Self {
82        SourceMapError::IndexOutOfBounds { index, length }
83    }
84}