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
use std::error;
use std::fmt;
use std::io;
use std::str;
use std::string;

/// Represents results from this library
pub type Result<T> = std::result::Result<T, Error>;

/// Represents different failure cases
#[derive(Debug)]
pub enum Error {
    /// a std::io error
    Io(io::Error),
    #[cfg(feature = "ram_bundle")]
    /// a scroll error
    Scroll(scroll::Error),
    /// a std::str::Utf8Error
    Utf8(str::Utf8Error),
    /// a JSON parsing related failure
    BadJson(serde_json::Error),
    /// a VLQ string was malformed and data was left over
    VlqLeftover,
    /// a VLQ string was empty and no values could be decoded.
    VlqNoValues,
    /// Overflow in Vlq handling
    VlqOverflow,
    /// a mapping segment had an unsupported size
    BadSegmentSize(u32),
    /// a reference to a non existing source was encountered
    BadSourceReference(u32),
    /// a reference to a non existing name was encountered
    BadNameReference(u32),
    /// Indicates that an indexed sourcemap was encountered when
    /// a regular sourcemap was expected
    IndexedSourcemap,
    /// Indicates that an regular (non-indexed) sourcemap was when
    /// a sourcemap index was expected
    RegularSourcemap,
    /// Indicates an invalid data URL
    InvalidDataUrl,
    /// Flatten failed
    CannotFlatten(String),
    /// The magic of a RAM bundle did not match
    InvalidRamBundleMagic,
    /// The RAM bundle index was malformed
    InvalidRamBundleIndex,
    /// A RAM bundle entry was invalid
    InvalidRamBundleEntry,
    /// Tried to operate on a non RAM bundle file
    NotARamBundle,
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::Io(err)
    }
}

#[cfg(feature = "ram_bundle")]
impl From<scroll::Error> for Error {
    fn from(err: scroll::Error) -> Self {
        Error::Scroll(err)
    }
}

impl From<string::FromUtf8Error> for Error {
    fn from(err: string::FromUtf8Error) -> Error {
        From::from(err.utf8_error())
    }
}

impl From<str::Utf8Error> for Error {
    fn from(err: str::Utf8Error) -> Error {
        Error::Utf8(err)
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Error {
        Error::BadJson(err)
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        use crate::Error::*;
        match *self {
            Io(ref err) => err.description(),
            Utf8(ref err) => err.description(),
            BadJson(ref err) => err.description(),
            #[cfg(feature = "ram_bundle")]
            Scroll(ref err) => err.description(),
            VlqLeftover => "vlq leftover",
            VlqNoValues => "no vlq values",
            VlqOverflow => "overflow in vlq",
            BadSegmentSize(_) => "bad segment size",
            BadSourceReference(_) => "bad source reference",
            BadNameReference(_) => "bad name reference",
            IndexedSourcemap => "unexpected indexed sourcemap",
            RegularSourcemap => "unexpected sourcemap",
            InvalidDataUrl => "invalid data URL",
            CannotFlatten(_) => "cannot flatten the given indexed sourcemap",
            InvalidRamBundleMagic => "invalid magic number for ram bundle",
            InvalidRamBundleIndex => "invalid module index in ram bundle",
            InvalidRamBundleEntry => "invalid ram bundle module entry",
            NotARamBundle => "not a ram bundle",
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        use crate::Error::*;
        match *self {
            Io(ref err) => Some(&*err),
            Utf8(ref err) => Some(&*err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use crate::Error::*;
        match *self {
            Io(ref msg) => write!(f, "{}", msg),
            Utf8(ref msg) => write!(f, "{}", msg),
            BadJson(ref err) => write!(f, "bad json: {}", err),
            #[cfg(feature = "ram_bundle")]
            Scroll(ref err) => write!(f, "parse error: {}", err),
            VlqLeftover => write!(f, "leftover cur/shift in vlq decode"),
            VlqNoValues => write!(f, "vlq decode did not produce any values"),
            VlqOverflow => write!(f, "vlq decode caused an overflow"),
            BadSegmentSize(size) => write!(f, "got {} segments, expected 4 or 5", size),
            BadSourceReference(id) => write!(f, "bad reference to source #{}", id),
            BadNameReference(id) => write!(f, "bad reference to name #{}", id),
            IndexedSourcemap => write!(f, "encountered unexpected indexed sourcemap"),
            RegularSourcemap => write!(
                f,
                "encountered unexpected sourcemap where index was expected"
            ),
            InvalidDataUrl => write!(f, "the provided data URL is invalid"),
            CannotFlatten(ref msg) => write!(f, "cannot flatten the indexed sourcemap: {}", msg),
            InvalidRamBundleMagic => write!(f, "invalid magic number for ram bundle"),
            InvalidRamBundleIndex => write!(f, "invalid module index in ram bundle"),
            InvalidRamBundleEntry => write!(f, "invalid ram bundle module entry"),
            NotARamBundle => write!(f, "not a ram bundle"),
        }
    }
}