1
1
use std:: borrow:: Cow ;
2
2
use std:: str:: FromStr ;
3
3
4
- use base64:: engine:: general_purpose:: { STANDARD , URL_SAFE } ;
5
- use base64:: { DecodeError , Engine } ;
4
+ use base64:: engine:: general_purpose:: GeneralPurpose ;
5
+ use base64:: engine:: { DecodePaddingMode , GeneralPurposeConfig } ;
6
+ use base64:: { alphabet, DecodeError , Engine } ;
6
7
use pyo3:: types:: { PyDict , PyString } ;
7
8
use pyo3:: { intern, prelude:: * } ;
8
9
@@ -11,6 +12,15 @@ use crate::input::EitherBytes;
11
12
use crate :: serializers:: BytesMode ;
12
13
use crate :: tools:: SchemaDict ;
13
14
15
+ const URL_SAFE_OPTIONAL_PADDING : GeneralPurpose = GeneralPurpose :: new (
16
+ & alphabet:: URL_SAFE ,
17
+ GeneralPurposeConfig :: new ( ) . with_decode_padding_mode ( DecodePaddingMode :: Indifferent ) ,
18
+ ) ;
19
+ const STANDARD_OPTIONAL_PADDING : GeneralPurpose = GeneralPurpose :: new (
20
+ & alphabet:: STANDARD ,
21
+ GeneralPurposeConfig :: new ( ) . with_decode_padding_mode ( DecodePaddingMode :: Indifferent ) ,
22
+ ) ;
23
+
14
24
#[ derive( Default , Debug , Clone , Copy , PartialEq , Eq ) ]
15
25
pub struct ValBytesMode {
16
26
pub ser : BytesMode ,
@@ -29,10 +39,10 @@ impl ValBytesMode {
29
39
pub fn deserialize_string < ' py > ( self , s : & str ) -> Result < EitherBytes < ' _ , ' py > , ErrorType > {
30
40
match self . ser {
31
41
BytesMode :: Utf8 => Ok ( EitherBytes :: Cow ( Cow :: Borrowed ( s. as_bytes ( ) ) ) ) ,
32
- BytesMode :: Base64 => URL_SAFE
42
+ BytesMode :: Base64 => URL_SAFE_OPTIONAL_PADDING
33
43
. decode ( s)
34
44
. or_else ( |err| match err {
35
- DecodeError :: InvalidByte ( _, b'/' | b'+' ) => STANDARD . decode ( s) ,
45
+ DecodeError :: InvalidByte ( _, b'/' | b'+' ) => STANDARD_OPTIONAL_PADDING . decode ( s) ,
36
46
_ => Err ( err) ,
37
47
} )
38
48
. map ( EitherBytes :: from)
0 commit comments