use rkyv::ser::serializers::{AllocScratchError, CompositeSerializerError};
use std::{convert::Infallible, io};
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum SourceMapErrorType {
UnexpectedNegativeNumber = 1,
UnexpectedlyBigNumber = 2,
VlqUnexpectedEof = 3,
VlqInvalidBase64 = 4,
VlqOverflow = 5,
IOError = 6,
NameOutOfRange = 7,
SourceOutOfRange = 8,
BufferError = 9,
InvalidFilePath = 10,
FromUtf8Error = 11,
JSONError = 12,
#[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 {
write!(f, "[parcel-sourcemap] ")?;
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")?;
}
}
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)
}
}