From 01572f037d4964d061d722cd077acfc7c10ff2d2 Mon Sep 17 00:00:00 2001 From: DonIsaac <22823424+DonIsaac@users.noreply.github.com> Date: Wed, 26 Jun 2024 05:10:48 +0000 Subject: [PATCH] feat(sourcemap): impl `std::fmt::Display` for `Error` (#3902) --- crates/oxc_sourcemap/src/error.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/oxc_sourcemap/src/error.rs b/crates/oxc_sourcemap/src/error.rs index 290e0932b..d2aadb96f 100644 --- a/crates/oxc_sourcemap/src/error.rs +++ b/crates/oxc_sourcemap/src/error.rs @@ -1,3 +1,5 @@ +use std::{error, fmt}; + #[derive(Debug)] pub enum Error { /// a VLQ string was malformed and data was left over @@ -15,6 +17,35 @@ pub enum Error { /// a reference to a non existing name was encountered BadNameReference(u32), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::VlqLeftover => write!(f, "VLQ string was malformed and data was left over"), + Error::VlqNoValues => write!(f, "VLQ string was empty and no values could be decoded"), + Error::VlqOverflow => write!(f, "The input encoded a number that didn't fit into i64"), + Error::BadJson(err) => write!(f, "JSON parsing error: {err}"), + Error::BadSegmentSize(size) => { + write!(f, "Mapping segment had an unsupported size of {size}") + } + Error::BadSourceReference(idx) => { + write!(f, "Reference to non-existing source at position {idx}") + } + Error::BadNameReference(idx) => { + write!(f, "Reference to non-existing name at position {idx}") + } + } + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + if let Self::BadJson(err) = self { + Some(err) + } else { + None + } + } +} /// The result of decoding. pub type Result = std::result::Result;