11use std:: borrow:: Cow ;
22use std:: str:: FromStr ;
33
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 } ;
67use pyo3:: types:: { PyDict , PyString } ;
78use pyo3:: { intern, prelude:: * } ;
89
@@ -11,6 +12,15 @@ use crate::input::EitherBytes;
1112use crate :: serializers:: BytesMode ;
1213use crate :: tools:: SchemaDict ;
1314
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+
1424#[ derive( Default , Debug , Clone , Copy , PartialEq , Eq ) ]
1525pub struct ValBytesMode {
1626 pub ser : BytesMode ,
@@ -29,10 +39,10 @@ impl ValBytesMode {
2939 pub fn deserialize_string < ' py > ( self , s : & str ) -> Result < EitherBytes < ' _ , ' py > , ErrorType > {
3040 match self . ser {
3141 BytesMode :: Utf8 => Ok ( EitherBytes :: Cow ( Cow :: Borrowed ( s. as_bytes ( ) ) ) ) ,
32- BytesMode :: Base64 => URL_SAFE
42+ BytesMode :: Base64 => URL_SAFE_OPTIONAL_PADDING
3343 . decode ( s)
3444 . or_else ( |err| match err {
35- DecodeError :: InvalidByte ( _, b'/' | b'+' ) => STANDARD . decode ( s) ,
45+ DecodeError :: InvalidByte ( _, b'/' | b'+' ) => STANDARD_OPTIONAL_PADDING . decode ( s) ,
3646 _ => Err ( err) ,
3747 } )
3848 . map ( EitherBytes :: from)
0 commit comments