@@ -33,10 +33,10 @@ pub enum Alphabet {
33
33
/// RFC4648 encoding.
34
34
RFC4648 {
35
35
/// Whether to use padding.
36
- padding : bool
36
+ padding : bool ,
37
37
} ,
38
38
/// Zbase32 encoding.
39
- ZBase32
39
+ ZBase32 ,
40
40
}
41
41
42
42
impl Alphabet {
@@ -60,9 +60,7 @@ impl Alphabet {
60
60
}
61
61
ret
62
62
} ,
63
- Self :: ZBase32 => {
64
- Self :: encode_data ( data, ZBASE_ALPHABET )
65
- } ,
63
+ Self :: ZBase32 => Self :: encode_data ( data, ZBASE_ALPHABET ) ,
66
64
} ;
67
65
ret. truncate ( output_length) ;
68
66
@@ -79,7 +77,9 @@ impl Alphabet {
79
77
Self :: RFC4648 { padding } => {
80
78
let mut unpadded_data_length = data. len ( ) ;
81
79
if * padding {
82
- if data. len ( ) % 8 != 0 { return Err ( ( ) ) ; }
80
+ if data. len ( ) % 8 != 0 {
81
+ return Err ( ( ) ) ;
82
+ }
83
83
data. iter ( ) . rev ( ) . take ( 6 ) . for_each ( |& c| {
84
84
if c == b'=' {
85
85
unpadded_data_length -= 1 ;
@@ -88,13 +88,14 @@ impl Alphabet {
88
88
}
89
89
( & data[ ..unpadded_data_length] , RFC4648_INV_ALPHABET )
90
90
} ,
91
- Self :: ZBase32 => {
92
- ( data, ZBASE_INV_ALPHABET )
93
- }
91
+ Self :: ZBase32 => ( data, ZBASE_INV_ALPHABET ) ,
94
92
} ;
95
93
// If the string has more characters than are required to alphabet_encode the number of bytes
96
94
// decodable, treat the string as invalid.
97
- match data. len ( ) % 8 { 1 |3 |6 => return Err ( ( ) ) , _ => { } }
95
+ match data. len ( ) % 8 {
96
+ 1 | 3 | 6 => return Err ( ( ) ) ,
97
+ _ => { } ,
98
+ }
98
99
Ok ( Self :: decode_data ( data, alphabet) ?)
99
100
}
100
101
@@ -175,9 +176,13 @@ mod tests {
175
176
( "6n9hq" , & [ 0xf0 , 0xbf , 0xc7 ] ) ,
176
177
( "4t7ye" , & [ 0xd4 , 0x7a , 0x04 ] ) ,
177
178
( "6im5sdy" , & [ 0xf5 , 0x57 , 0xbb , 0x0c ] ) ,
178
- ( "ybndrfg8ejkmcpqxot1uwisza345h769" , & [ 0x00 , 0x44 , 0x32 , 0x14 , 0xc7 , 0x42 , 0x54 , 0xb6 ,
179
- 0x35 , 0xcf , 0x84 , 0x65 , 0x3a , 0x56 , 0xd7 , 0xc6 ,
180
- 0x75 , 0xbe , 0x77 , 0xdf ] )
179
+ (
180
+ "ybndrfg8ejkmcpqxot1uwisza345h769" ,
181
+ & [
182
+ 0x00 , 0x44 , 0x32 , 0x14 , 0xc7 , 0x42 , 0x54 , 0xb6 , 0x35 , 0xcf , 0x84 , 0x65 , 0x3a , 0x56 ,
183
+ 0xd7 , 0xc6 , 0x75 , 0xbe , 0x77 , 0xdf ,
184
+ ] ,
185
+ ) ,
181
186
] ;
182
187
183
188
#[ test]
@@ -242,7 +247,9 @@ mod tests {
242
247
}
243
248
244
249
for ( input, encoded) in RFC4648_NON_PADDED_TEST_VECTORS {
245
- let res = & Alphabet :: RFC4648 { padding : false } . decode ( std:: str:: from_utf8 ( encoded) . unwrap ( ) ) . unwrap ( ) ;
250
+ let res = & Alphabet :: RFC4648 { padding : false }
251
+ . decode ( std:: str:: from_utf8 ( encoded) . unwrap ( ) )
252
+ . unwrap ( ) ;
246
253
assert_eq ! ( & res[ ..] , & input[ ..] ) ;
247
254
}
248
255
}
@@ -251,9 +258,8 @@ mod tests {
251
258
fn padding ( ) {
252
259
let num_padding = [ 0 , 6 , 4 , 3 , 1 ] ;
253
260
for i in 1 ..6 {
254
- let encoded = Alphabet :: RFC4648 { padding : true } . encode (
255
- ( 0 ..( i as u8 ) ) . collect :: < Vec < u8 > > ( ) . as_ref ( )
256
- ) ;
261
+ let encoded = Alphabet :: RFC4648 { padding : true }
262
+ . encode ( ( 0 ..( i as u8 ) ) . collect :: < Vec < u8 > > ( ) . as_ref ( ) ) ;
257
263
assert_eq ! ( encoded. len( ) , 8 ) ;
258
264
for j in 0 ..( num_padding[ i % 5 ] ) {
259
265
assert_eq ! ( encoded. as_bytes( ) [ encoded. len( ) - j - 1 ] , b'=' ) ;
0 commit comments