@@ -176,8 +176,8 @@ impl super::Engine for FastPortable {
176176 )
177177 }
178178
179- fn config ( & self ) -> Self :: Config {
180- self . config
179+ fn config ( & self ) -> & Self :: Config {
180+ & self . config
181181 }
182182}
183183
@@ -238,43 +238,61 @@ fn read_u64(s: &[u8]) -> u64 {
238238 u64:: from_be_bytes ( s[ ..8 ] . try_into ( ) . unwrap ( ) )
239239}
240240
241- /// Contains miscellaneous configuration parameters for base64 encoding and decoding.
241+ /// Contains configuration parameters for base64 encoding and decoding.
242+ ///
243+ /// ```
244+ /// # use base64::engine::fast_portable::FastPortableConfig;
245+ /// let config = FastPortableConfig::new()
246+ /// .with_encode_padding(false);
247+ /// // further customize using `.with_*` methods as needed
248+ /// ```
249+ ///
250+ /// The constants [PAD] and [NO_PAD] cover most use cases.
242251///
243252/// To specify the characters used, see [crate::alphabet::Alphabet].
244253#[ derive( Clone , Copy , Debug ) ]
245254pub struct FastPortableConfig {
246- /// `true` to pad output with `=` characters
247- padding : bool ,
248- /// `true` to ignore excess nonzero bits in the last few symbols, otherwise an error is returned
255+ encode_padding : bool ,
249256 decode_allow_trailing_bits : bool ,
250257}
251258
252259impl FastPortableConfig {
253- /// Create a new config.
260+ /// Create a new config with `padding` = `true` and `decode_allow_trailing_bits` = `false` .
254261 ///
255- /// - `padding`: if `true`, encoding will append `=` padding characters to produce an
256- /// output whose length is a multiple of 4. Padding is not needed for decoding and
257- /// only serves to waste bytes but it's in the spec. For new applications, consider
258- /// not using padding.
259- /// - `decode_allow_trailing_bits`: If unsure, use `false`.
260- /// Useful if you need to decode base64 produced by a buggy encoder that
261- /// has bits set in the unused space on the last base64 character as per
262- /// [forgiving-base64 decode](https://infra.spec.whatwg.org/#forgiving-base64-decode).
263- /// If invalid trailing bits are present and this `true`, those bits will
264- /// be silently ignored, else `DecodeError::InvalidLastSymbol` will be emitted.
265- pub const fn from ( padding : bool , decode_allow_trailing_bits : bool ) -> FastPortableConfig {
262+ /// This probably matches most people's expectations, but consider disabling padding to save
263+ /// a few bytes unless you specifically need it for compatibility with some legacy system.
264+ pub const fn new ( ) -> FastPortableConfig {
266265 FastPortableConfig {
267- padding,
268- decode_allow_trailing_bits,
266+ // RFC states that padding must be applied by default
267+ encode_padding : true ,
268+ decode_allow_trailing_bits : false ,
269269 }
270270 }
271271
272- /// Create a new `Config` based on `self` with an updated `padding` parameter.
273- pub const fn with_padding ( self , padding : bool ) -> FastPortableConfig {
274- FastPortableConfig { padding, ..self }
272+ /// Create a new config based on `self` with an updated `padding` parameter.
273+ ///
274+ /// If `true`, encoding will append either 1 or 2 `=` padding characters to produce an
275+ /// output whose length is a multiple of 4.
276+ ///
277+ /// Padding is not needed for correct decoding and only serves to waste bytes, but it's in the
278+ /// [spec](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2).
279+ ///
280+ /// For new applications, consider not using padding if the decoders you're using don't require
281+ /// padding to be present.
282+ pub const fn with_encode_padding ( self , padding : bool ) -> FastPortableConfig {
283+ FastPortableConfig {
284+ encode_padding : padding,
285+ ..self
286+ }
275287 }
276288
277- /// Create a new `Config` based on `self` with an updated `decode_allow_trailing_bits` parameter.
289+ /// Create a new config based on `self` with an updated `decode_allow_trailing_bits` parameter.
290+ ///
291+ /// Most users will not need to configure this. It's useful if you need to decode base64
292+ /// produced by a buggy encoder that has bits set in the unused space on the last base64
293+ /// character as per [forgiving-base64 decode](https://infra.spec.whatwg.org/#forgiving-base64-decode).
294+ /// If invalid trailing bits are present and this is `true`, those bits will
295+ /// be silently ignored, else `DecodeError::InvalidLastSymbol` will be emitted.
278296 pub const fn with_decode_allow_trailing_bits ( self , allow : bool ) -> FastPortableConfig {
279297 FastPortableConfig {
280298 decode_allow_trailing_bits : allow,
@@ -283,20 +301,21 @@ impl FastPortableConfig {
283301 }
284302}
285303
304+ impl Default for FastPortableConfig {
305+ /// Delegates to [FastPortableConfig::new].
306+ fn default ( ) -> Self {
307+ FastPortableConfig :: new ( )
308+ }
309+ }
310+
286311impl Config for FastPortableConfig {
287- fn padding ( & self ) -> bool {
288- self . padding
312+ fn encode_padding ( & self ) -> bool {
313+ self . encode_padding
289314 }
290315}
291316
292317/// Include padding bytes when encoding.
293- pub const PAD : FastPortableConfig = FastPortableConfig {
294- padding : true ,
295- decode_allow_trailing_bits : false ,
296- } ;
318+ pub const PAD : FastPortableConfig = FastPortableConfig :: new ( ) ;
297319
298320/// Don't add padding when encoding.
299- pub const NO_PAD : FastPortableConfig = FastPortableConfig {
300- padding : false ,
301- decode_allow_trailing_bits : false ,
302- } ;
321+ pub const NO_PAD : FastPortableConfig = FastPortableConfig :: new ( ) . with_encode_padding ( false ) ;
0 commit comments