1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use rkyv::ser::serializers::{AllocScratchError, CompositeSerializerError};
use std::{convert::Infallible, io};

// Errors that can occur during processing/modifying source map
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum SourceMapErrorType {
    // NB: 0 is reserved for OK.
    // The mappings contained a negative line, column, source index, or name index.
    UnexpectedNegativeNumber = 1,

    // The mappings contained a number larger than `u32::MAX`.
    UnexpectedlyBigNumber = 2,

    // Reached EOF while in the middle of parsing a VLQ.
    VlqUnexpectedEof = 3,

    // Encountered an invalid base 64 character while parsing a VLQ.
    VlqInvalidBase64 = 4,

    // VLQ encountered a number that, when decoded, would not fit in a u32.
    VlqOverflow = 5,

    // General IO Error
    IOError = 6,

    // Name out of range
    NameOutOfRange = 7,

    // Source out of range
    SourceOutOfRange = 8,

    // Failed to write buffer
    BufferError = 9,

    // FilePath is invalid
    InvalidFilePath = 10,

    // Failed to convert utf-8 to array
    FromUtf8Error = 11,

    // Failed to serialize to JSON
    JSONError = 12,

    // Failed to parse data url
    #[cfg(feature = "json")]
    DataUrlError = 13,
}

#[derive(Debug)]
pub struct SourceMapError {
    pub error_type: SourceMapErrorType,
    pub reason: Option<String>,
}

impl SourceMapError {
    pub fn new(error_type: SourceMapErrorType) -> Self {
        Self {
            error_type,
            reason: None,
        }
    }

    pub fn new_with_reason(error_type: SourceMapErrorType, reason: &str) -> Self {
        Self {
            error_type,
            reason: Some(String::from(reason)),
        }
    }
}

impl std::error::Error for SourceMapError {}

impl std::fmt::Display for SourceMapError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Prefix all errors, so it's obvious they originate from this library
        write!(f, "[parcel-sourcemap] ")?;

        // Convert error type into an error message...
        match self.error_type {
            SourceMapErrorType::UnexpectedNegativeNumber => {
                write!(f, "Unexpected Negative Number")?;
            }
            SourceMapErrorType::UnexpectedlyBigNumber => {
                write!(f, "Unexpected Big Number")?;
            }
            SourceMapErrorType::VlqUnexpectedEof => {
                write!(f, "VLQ Unexpected end of file")?;
            }
            SourceMapErrorType::VlqInvalidBase64 => {
                write!(f, "VLQ Invalid Base 64 value")?;
            }
            SourceMapErrorType::VlqOverflow => {
                write!(f, "VLQ Value overflowed, does not fit in u32")?;
            }
            SourceMapErrorType::IOError => {
                write!(f, "IO Error")?;
            }
            SourceMapErrorType::NameOutOfRange => {
                write!(f, "Name out of range")?;
            }
            SourceMapErrorType::SourceOutOfRange => {
                write!(f, "Source out of range")?;
            }
            SourceMapErrorType::InvalidFilePath => {
                write!(f, "Invalid FilePath")?;
            }
            SourceMapErrorType::BufferError => {
                write!(
                    f,
                    "Something went wrong while writing/reading a sourcemap buffer"
                )?;
            }
            SourceMapErrorType::FromUtf8Error => {
                write!(f, "Could not convert utf-8 array to string")?;
            }
            SourceMapErrorType::JSONError => {
                write!(f, "Error reading or writing to JSON")?;
            }
            #[cfg(feature = "json")]
            SourceMapErrorType::DataUrlError => {
                write!(f, "Error parsing data url")?;
            }
        }

        // Add reason to error string if there is one

        if let Some(r) = &self.reason {
            write!(f, ", ")?;
            write!(f, "{}", &r[..])?;
        }

        Ok(())
    }
}

impl From<vlq::Error> for SourceMapError {
    #[inline]
    fn from(e: vlq::Error) -> SourceMapError {
        match e {
            vlq::Error::UnexpectedEof => SourceMapError::new(SourceMapErrorType::VlqUnexpectedEof),
            vlq::Error::InvalidBase64(_) => {
                SourceMapError::new(SourceMapErrorType::VlqInvalidBase64)
            }
            vlq::Error::Overflow => SourceMapError::new(SourceMapErrorType::VlqOverflow),
        }
    }
}

impl From<io::Error> for SourceMapError {
    #[inline]
    fn from(_err: io::Error) -> SourceMapError {
        SourceMapError::new(SourceMapErrorType::IOError)
    }
}

impl From<Infallible> for SourceMapError {
    #[inline]
    fn from(_err: Infallible) -> SourceMapError {
        SourceMapError::new(SourceMapErrorType::BufferError)
    }
}

impl From<CompositeSerializerError<Infallible, AllocScratchError, Infallible>> for SourceMapError {
    #[inline]
    fn from(
        _err: CompositeSerializerError<Infallible, AllocScratchError, Infallible>,
    ) -> SourceMapError {
        SourceMapError::new(SourceMapErrorType::BufferError)
    }
}

impl From<std::string::FromUtf8Error> for SourceMapError {
    #[inline]
    fn from(_err: std::string::FromUtf8Error) -> SourceMapError {
        SourceMapError::new(SourceMapErrorType::FromUtf8Error)
    }
}

#[cfg(feature = "json")]
impl From<serde_json::Error> for SourceMapError {
    #[inline]
    fn from(_err: serde_json::Error) -> SourceMapError {
        SourceMapError::new(SourceMapErrorType::JSONError)
    }
}

#[cfg(feature = "json")]
impl From<data_url::DataUrlError> for SourceMapError {
    #[inline]
    fn from(_err: data_url::DataUrlError) -> SourceMapError {
        SourceMapError::new(SourceMapErrorType::DataUrlError)
    }
}