Skip to main content

oxc_sourcemap/
error.rs

1use std::{error, fmt};
2
3#[derive(Debug)]
4pub enum Error {
5    /// a VLQ string was malformed and data was left over
6    VlqLeftover,
7    /// a VLQ string was empty and no values could be decoded.
8    VlqNoValues,
9    /// The input encoded a number that didn't fit into i64.
10    VlqOverflow,
11    /// `serde_json` parsing failure
12    BadJson(serde_json::Error),
13    /// a mapping segment had an unsupported size
14    BadSegmentSize(u32),
15    /// a reference to a non existing source was encountered
16    BadSourceReference(u32),
17    /// a reference to a non existing name was encountered
18    BadNameReference(u32),
19}
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Error::VlqLeftover => write!(f, "VLQ string was malformed and data was left over"),
24            Error::VlqNoValues => write!(f, "VLQ string was empty and no values could be decoded"),
25            Error::VlqOverflow => write!(f, "The input encoded a number that didn't fit into i64"),
26            Error::BadJson(err) => write!(f, "JSON parsing error: {err}"),
27            Error::BadSegmentSize(size) => {
28                write!(f, "Mapping segment had an unsupported size of {size}")
29            }
30            Error::BadSourceReference(idx) => {
31                write!(f, "Reference to non-existing source at position {idx}")
32            }
33            Error::BadNameReference(idx) => {
34                write!(f, "Reference to non-existing name at position {idx}")
35            }
36        }
37    }
38}
39
40impl error::Error for Error {
41    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
42        if let Self::BadJson(err) = self { Some(err) } else { None }
43    }
44}
45
46/// The result of decoding.
47pub type Result<T> = std::result::Result<T, Error>;
48
49impl From<serde_json::Error> for Error {
50    fn from(err: serde_json::Error) -> Error {
51        Error::BadJson(err)
52    }
53}