@@ -11,6 +11,7 @@ use simdutf8::basic::from_utf8;
11
11
/// Supported codecs for the decode and encode functions.
12
12
#[ derive( Debug , Clone , Copy , Serialize , Deserialize , PartialEq , Eq , Hash ) ]
13
13
pub enum Codec {
14
+ Base64 ,
14
15
Deflate ,
15
16
Gzip ,
16
17
Utf8 ,
@@ -20,6 +21,7 @@ pub enum Codec {
20
21
impl Display for Codec {
21
22
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
22
23
f. write_str ( match self {
24
+ Self :: Base64 => "base64" ,
23
25
Self :: Deflate => "deflate" ,
24
26
Self :: Gzip => "gzip" ,
25
27
Self :: Utf8 => "utf8" ,
@@ -61,6 +63,7 @@ pub(crate) type Transform = fn(input: &[u8]) -> DaftResult<Vec<u8>>;
61
63
impl Codec {
62
64
pub ( crate ) fn encoder ( & self ) -> Transform {
63
65
match self {
66
+ Self :: Base64 => base64_encoder,
64
67
Self :: Deflate => deflate_encoder,
65
68
Self :: Gzip => gzip_encoder,
66
69
Self :: Utf8 => utf8_encoder,
@@ -70,6 +73,7 @@ impl Codec {
70
73
71
74
pub ( crate ) fn decoder ( & self ) -> Transform {
72
75
match self {
76
+ Self :: Base64 => base64_decoder,
73
77
Self :: Deflate => deflate_decoder,
74
78
Self :: Gzip => gzip_decoder,
75
79
Self :: Utf8 => utf8_decoder,
@@ -79,6 +83,7 @@ impl Codec {
79
83
80
84
pub ( crate ) fn kind ( & self ) -> CodecKind {
81
85
match self {
86
+ Self :: Base64 => CodecKind :: Binary ,
82
87
Self :: Deflate => CodecKind :: Binary ,
83
88
Self :: Gzip => CodecKind :: Binary ,
84
89
Self :: Utf8 => CodecKind :: Text ,
@@ -99,6 +104,7 @@ impl FromStr for Codec {
99
104
100
105
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
101
106
match s. to_lowercase ( ) . as_str ( ) {
107
+ "base64" => Ok ( Self :: Base64 ) ,
102
108
"deflate" => Ok ( Self :: Deflate ) ,
103
109
"gzip" | "gz" => Ok ( Self :: Gzip ) ,
104
110
"zlib" => Ok ( Self :: Zlib ) ,
@@ -115,6 +121,13 @@ impl FromStr for Codec {
115
121
// ENCODERS
116
122
//
117
123
124
+ #[ inline]
125
+ fn base64_encoder ( input : & [ u8 ] ) -> DaftResult < Vec < u8 > > {
126
+ use base64:: { Engine , engine:: general_purpose:: STANDARD } ;
127
+
128
+ Ok ( STANDARD . encode ( input) . into_bytes ( ) )
129
+ }
130
+
118
131
#[ inline]
119
132
fn deflate_encoder ( input : & [ u8 ] ) -> DaftResult < Vec < u8 > > {
120
133
use std:: io:: Write ;
@@ -160,6 +173,14 @@ fn zlib_encoder(input: &[u8]) -> DaftResult<Vec<u8>> {
160
173
// DECODERS
161
174
//
162
175
176
+ #[ inline]
177
+ fn base64_decoder ( input : & [ u8 ] ) -> DaftResult < Vec < u8 > > {
178
+ use base64:: { Engine , engine:: general_purpose:: STANDARD } ;
179
+ STANDARD
180
+ . decode ( input)
181
+ . map_err ( |e| DaftError :: ValueError ( format ! ( "Invalid base64 input: {}" , e) ) )
182
+ }
183
+
163
184
#[ inline]
164
185
fn deflate_decoder ( input : & [ u8 ] ) -> DaftResult < Vec < u8 > > {
165
186
use std:: io:: Read ;
@@ -223,6 +244,8 @@ mod tests {
223
244
assert_eq ! ( "zlib" . parse:: <Codec >( ) . unwrap( ) , Codec :: Zlib ) ;
224
245
assert_eq ! ( "ZLIB" . parse:: <Codec >( ) . unwrap( ) , Codec :: Zlib ) ;
225
246
assert_eq ! ( "ZlIb" . parse:: <Codec >( ) . unwrap( ) , Codec :: Zlib ) ;
247
+ assert_eq ! ( "base64" . parse:: <Codec >( ) . unwrap( ) , Codec :: Base64 ) ;
248
+ assert_eq ! ( "BASE64" . parse:: <Codec >( ) . unwrap( ) , Codec :: Base64 ) ;
226
249
assert ! ( "unknown" . parse:: <Codec >( ) . is_err( ) ) ;
227
250
}
228
251
}
0 commit comments