1
1
//! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
2
2
3
3
use alloc:: vec:: Vec ;
4
+ use core:: fmt;
4
5
5
6
#[ derive( Debug ) ]
6
7
pub struct InvalidBase64 ( InvalidBase64Details ) ;
7
8
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
+
8
24
#[ derive( Debug ) ]
9
25
enum InvalidBase64Details {
10
26
UnexpectedSymbol ( u8 ) ,
@@ -19,6 +35,18 @@ pub enum DecodeError<E> {
19
35
WriteError ( E ) ,
20
36
}
21
37
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
+
22
50
impl < E > From < InvalidBase64Details > for DecodeError < E > {
23
51
fn from ( e : InvalidBase64Details ) -> Self {
24
52
DecodeError :: InvalidBase64 ( InvalidBase64 ( e) )
0 commit comments