diff --git a/data-url/src/forgiving_base64.rs b/data-url/src/forgiving_base64.rs index 390d88bc4..5cb1bc43c 100644 --- a/data-url/src/forgiving_base64.rs +++ b/data-url/src/forgiving_base64.rs @@ -1,10 +1,26 @@ //! use alloc::vec::Vec; +use core::fmt; #[derive(Debug)] pub struct InvalidBase64(InvalidBase64Details); +impl fmt::Display for InvalidBase64 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.0 { + InvalidBase64Details::UnexpectedSymbol(code_point) => { + write!(f, "symbol with codepoint {} not expected", code_point) + } + InvalidBase64Details::AlphabetSymbolAfterPadding => { + write!(f, "alphabet symbol present after padding") + } + InvalidBase64Details::LoneAlphabetSymbol => write!(f, "lone alphabet symbol present"), + InvalidBase64Details::Padding => write!(f, "incorrect padding"), + } + } +} + #[derive(Debug)] enum InvalidBase64Details { UnexpectedSymbol(u8), @@ -19,6 +35,18 @@ pub enum DecodeError { WriteError(E), } +impl fmt::Display for DecodeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::InvalidBase64(inner) => write!(f, "base64 not valid: {}", inner), + Self::WriteError(err) => write!(f, "write error: {}", err), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for DecodeError {} + impl From for DecodeError { fn from(e: InvalidBase64Details) -> Self { DecodeError::InvalidBase64(InvalidBase64(e)) diff --git a/data-url/src/lib.rs b/data-url/src/lib.rs index 99e71a97e..b81a330c6 100644 --- a/data-url/src/lib.rs +++ b/data-url/src/lib.rs @@ -18,7 +18,7 @@ // For forwards compatibility #[cfg(feature = "std")] -extern crate std as _; +extern crate std; #[macro_use] extern crate alloc; @@ -27,6 +27,7 @@ extern crate alloc; compile_error!("the `alloc` feature must be enabled"); use alloc::{string::String, vec::Vec}; +use core::fmt; macro_rules! require { ($condition: expr) => { @@ -51,6 +52,21 @@ pub enum DataUrlError { NoComma, } +impl fmt::Display for DataUrlError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::NotADataUrl => write!(f, "not a valid data url"), + Self::NoComma => write!( + f, + "data url is missing comma delimiting attributes and body" + ), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for DataUrlError {} + impl<'a> DataUrl<'a> { /// /// but starting from a string rather than a parsed `Url`, to avoid extra string copies. diff --git a/data-url/src/mime.rs b/data-url/src/mime.rs index b4173d8f1..847c7a2ce 100644 --- a/data-url/src/mime.rs +++ b/data-url/src/mime.rs @@ -26,6 +26,15 @@ impl Mime { #[derive(Debug)] pub struct MimeParsingError(()); +impl fmt::Display for MimeParsingError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "invalid mime type") + } +} + +#[cfg(feature = "std")] +impl std::error::Error for MimeParsingError {} + /// impl FromStr for Mime { type Err = MimeParsingError;