Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement std::error::Error for data-url #698

Merged
merged 1 commit into from Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions data-url/src/forgiving_base64.rs
@@ -1,10 +1,26 @@
//! <https://infra.spec.whatwg.org/#forgiving-base64-decode>

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)

Check warning on line 13 in data-url/src/forgiving_base64.rs

View check run for this annotation

Codecov / codecov/patch

data-url/src/forgiving_base64.rs#L10-L13

Added lines #L10 - L13 were not covered by tests
}
InvalidBase64Details::AlphabetSymbolAfterPadding => {
write!(f, "alphabet symbol present after padding")

Check warning on line 16 in data-url/src/forgiving_base64.rs

View check run for this annotation

Codecov / codecov/patch

data-url/src/forgiving_base64.rs#L16

Added line #L16 was not covered by tests
}
InvalidBase64Details::LoneAlphabetSymbol => write!(f, "lone alphabet symbol present"),
InvalidBase64Details::Padding => write!(f, "incorrect padding"),

Check warning on line 19 in data-url/src/forgiving_base64.rs

View check run for this annotation

Codecov / codecov/patch

data-url/src/forgiving_base64.rs#L18-L19

Added lines #L18 - L19 were not covered by tests
}
}
}

#[derive(Debug)]
enum InvalidBase64Details {
UnexpectedSymbol(u8),
Expand All @@ -19,6 +35,18 @@
WriteError(E),
}

impl<E: fmt::Display> fmt::Display for DecodeError<E> {
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),

Check warning on line 42 in data-url/src/forgiving_base64.rs

View check run for this annotation

Codecov / codecov/patch

data-url/src/forgiving_base64.rs#L39-L42

Added lines #L39 - L42 were not covered by tests
}
}
}

#[cfg(feature = "std")]
impl<E: std::error::Error> std::error::Error for DecodeError<E> {}

impl<E> From<InvalidBase64Details> for DecodeError<E> {
fn from(e: InvalidBase64Details) -> Self {
DecodeError::InvalidBase64(InvalidBase64(e))
Expand Down
18 changes: 17 additions & 1 deletion data-url/src/lib.rs
Expand Up @@ -18,7 +18,7 @@

// For forwards compatibility
#[cfg(feature = "std")]
extern crate std as _;
extern crate std;

#[macro_use]
extern crate alloc;
Expand All @@ -27,6 +27,7 @@
compile_error!("the `alloc` feature must be enabled");

use alloc::{string::String, vec::Vec};
use core::fmt;

macro_rules! require {
($condition: expr) => {
Expand All @@ -51,6 +52,21 @@
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!(

Check warning on line 59 in data-url/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

data-url/src/lib.rs#L56-L59

Added lines #L56 - L59 were not covered by tests
f,
"data url is missing comma delimiting attributes and body"
),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for DataUrlError {}

impl<'a> DataUrl<'a> {
/// <https://fetch.spec.whatwg.org/#data-url-processor>
/// but starting from a string rather than a parsed `Url`, to avoid extra string copies.
Expand Down
9 changes: 9 additions & 0 deletions data-url/src/mime.rs
Expand Up @@ -26,6 +26,15 @@
#[derive(Debug)]
pub struct MimeParsingError(());

impl fmt::Display for MimeParsingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid mime type")

Check warning on line 31 in data-url/src/mime.rs

View check run for this annotation

Codecov / codecov/patch

data-url/src/mime.rs#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}

#[cfg(feature = "std")]
impl std::error::Error for MimeParsingError {}

/// <https://mimesniff.spec.whatwg.org/#parsing-a-mime-type>
impl FromStr for Mime {
type Err = MimeParsingError;
Expand Down