11//! <https://infra.spec.whatwg.org/#forgiving-base64-decode>
22
33use alloc:: vec:: Vec ;
4+ use core:: fmt;
45
56#[ derive( Debug ) ]
67pub 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 ) ]
925enum 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+
2250impl < E > From < InvalidBase64Details > for DecodeError < E > {
2351 fn from ( e : InvalidBase64Details ) -> Self {
2452 DecodeError :: InvalidBase64 ( InvalidBase64 ( e) )
0 commit comments