Skip to content

Commit 3a474c3

Browse files
authored
Merge pull request #698 from lucacasonato/impl_error_for_data_url
fix: implement std::error::Error for data-url
2 parents 1f7dbe0 + 63f850b commit 3a474c3

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

data-url/src/forgiving_base64.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
//! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
22
33
use alloc::vec::Vec;
4+
use core::fmt;
45

56
#[derive(Debug)]
67
pub struct InvalidBase64(InvalidBase64Details);
78

9+
impl fmt::Display for InvalidBase64 {
10+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11+
match self.0 {
12+
InvalidBase64Details::UnexpectedSymbol(code_point) => {
13+
write!(f, "symbol with codepoint {} not expected", code_point)
14+
}
15+
InvalidBase64Details::AlphabetSymbolAfterPadding => {
16+
write!(f, "alphabet symbol present after padding")
17+
}
18+
InvalidBase64Details::LoneAlphabetSymbol => write!(f, "lone alphabet symbol present"),
19+
InvalidBase64Details::Padding => write!(f, "incorrect padding"),
20+
}
21+
}
22+
}
23+
824
#[derive(Debug)]
925
enum InvalidBase64Details {
1026
UnexpectedSymbol(u8),
@@ -19,6 +35,18 @@ pub enum DecodeError<E> {
1935
WriteError(E),
2036
}
2137

38+
impl<E: fmt::Display> fmt::Display for DecodeError<E> {
39+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
match self {
41+
Self::InvalidBase64(inner) => write!(f, "base64 not valid: {}", inner),
42+
Self::WriteError(err) => write!(f, "write error: {}", err),
43+
}
44+
}
45+
}
46+
47+
#[cfg(feature = "std")]
48+
impl<E: std::error::Error> std::error::Error for DecodeError<E> {}
49+
2250
impl<E> From<InvalidBase64Details> for DecodeError<E> {
2351
fn from(e: InvalidBase64Details) -> Self {
2452
DecodeError::InvalidBase64(InvalidBase64(e))

data-url/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
// For forwards compatibility
2020
#[cfg(feature = "std")]
21-
extern crate std as _;
21+
extern crate std;
2222

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

2929
use alloc::{string::String, vec::Vec};
30+
use core::fmt;
3031

3132
macro_rules! require {
3233
($condition: expr) => {
@@ -51,6 +52,21 @@ pub enum DataUrlError {
5152
NoComma,
5253
}
5354

55+
impl fmt::Display for DataUrlError {
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
match self {
58+
Self::NotADataUrl => write!(f, "not a valid data url"),
59+
Self::NoComma => write!(
60+
f,
61+
"data url is missing comma delimiting attributes and body"
62+
),
63+
}
64+
}
65+
}
66+
67+
#[cfg(feature = "std")]
68+
impl std::error::Error for DataUrlError {}
69+
5470
impl<'a> DataUrl<'a> {
5571
/// <https://fetch.spec.whatwg.org/#data-url-processor>
5672
/// but starting from a string rather than a parsed `Url`, to avoid extra string copies.

data-url/src/mime.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ impl Mime {
2626
#[derive(Debug)]
2727
pub struct MimeParsingError(());
2828

29+
impl fmt::Display for MimeParsingError {
30+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31+
write!(f, "invalid mime type")
32+
}
33+
}
34+
35+
#[cfg(feature = "std")]
36+
impl std::error::Error for MimeParsingError {}
37+
2938
/// <https://mimesniff.spec.whatwg.org/#parsing-a-mime-type>
3039
impl FromStr for Mime {
3140
type Err = MimeParsingError;

0 commit comments

Comments
 (0)