From 142c3bdda0295737d56ab88b1bbadfa6fe762192 Mon Sep 17 00:00:00 2001 From: Marshall Pierce <575695+marshallpierce@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:12:51 -0700 Subject: [PATCH] Further padding consts --- RELEASE-NOTES.md | 4 ++++ src/engine/general_purpose/mod.rs | 30 ++++++++++++++++++++++++++---- src/lib.rs | 2 ++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 91b68a6..558d2e5 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -1,3 +1,7 @@ +# Next + +- Added more consts for preconfigured configs and engines + # 0.22.1 - Correct the symbols used for the predefined `alphabet::BIN_HEX`. diff --git a/src/engine/general_purpose/mod.rs b/src/engine/general_purpose/mod.rs index 48dfb54..72a02de 100644 --- a/src/engine/general_purpose/mod.rs +++ b/src/engine/general_purpose/mod.rs @@ -1,4 +1,6 @@ //! Provides the [`GeneralPurpose`] engine and associated config types. +//! +//! See preconfigured engines like [`STANDARD_NO_PAD`] or [`STANDARD_NO_PAD_INDIFFERENT`]. use crate::{ alphabet, alphabet::Alphabet, @@ -333,22 +335,42 @@ impl Config for GeneralPurposeConfig { } } -/// A [`GeneralPurpose`] engine using the [`alphabet::STANDARD`] base64 alphabet and [PAD] config. +/// A [`GeneralPurpose`] engine using the [`alphabet::STANDARD`] base64 alphabet and [`PAD`] config. pub const STANDARD: GeneralPurpose = GeneralPurpose::new(&alphabet::STANDARD, PAD); +/// A [`GeneralPurpose`] engine using the [`alphabet::STANDARD`] base64 alphabet and +/// [`PAD_INDIFFERENT`] config. +pub const STANDARD_PAD_INDIFFERENT: GeneralPurpose = + GeneralPurpose::new(&alphabet::STANDARD, PAD_INDIFFERENT); + /// A [`GeneralPurpose`] engine using the [`alphabet::STANDARD`] base64 alphabet and [`NO_PAD`] config. pub const STANDARD_NO_PAD: GeneralPurpose = GeneralPurpose::new(&alphabet::STANDARD, NO_PAD); -/// A [`GeneralPurpose`] engine using the [`alphabet::URL_SAFE`] base64 alphabet and [PAD] config. +/// A [`GeneralPurpose`] engine using the [`alphabet::STANDARD`] base64 alphabet and +/// [`NO_PAD_INDIFFERENT`] config. +pub const STANDARD_NO_PAD_INDIFFERENT: GeneralPurpose = + GeneralPurpose::new(&alphabet::STANDARD, NO_PAD_INDIFFERENT); + +/// A [`GeneralPurpose`] engine using the [`alphabet::URL_SAFE`] base64 alphabet and [`PAD`] config. pub const URL_SAFE: GeneralPurpose = GeneralPurpose::new(&alphabet::URL_SAFE, PAD); +/// A [`GeneralPurpose`] engine using the [`alphabet::URL_SAFE`] base64 alphabet and +/// [`PAD_INDIFFERENT`] config. +pub const URL_SAFE_PAD_INDIFFERENT: GeneralPurpose = + GeneralPurpose::new(&alphabet::URL_SAFE, PAD_INDIFFERENT); + /// A [`GeneralPurpose`] engine using the [`alphabet::URL_SAFE`] base64 alphabet and [`NO_PAD`] config. pub const URL_SAFE_NO_PAD: GeneralPurpose = GeneralPurpose::new(&alphabet::URL_SAFE, NO_PAD); +/// A [`GeneralPurpose`] engine using the [`alphabet::URL_SAFE`] base64 alphabet and +/// [`NO_PAD_INDIFFERENT`] config. +pub const URL_SAFE_NO_PAD_INDIFFERENT: GeneralPurpose = + GeneralPurpose::new(&alphabet::URL_SAFE, NO_PAD_INDIFFERENT); + /// Include padding bytes when encoding, and require that they be present when decoding. /// -/// This is the standard per the base64 RFC, but consider using [`NO_PAD_INDIFFERENT`] instead as padding serves -/// little purpose in practice. +/// This is the standard per the base64 RFC, but consider using [`NO_PAD`] or [`NO_PAD_INDIFFERENT`] +/// instead as padding serves little purpose in practice. pub const PAD: GeneralPurposeConfig = GeneralPurposeConfig::new(); /// Include padding bytes when encoding, but allow input with or without padding when decoding. diff --git a/src/lib.rs b/src/lib.rs index 50dac7a..cc21abb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,6 +114,8 @@ //! assert_eq!(STANDARD_NO_PAD.decode(b"Zm8="), Err(base64::DecodeError::InvalidPadding)); //! ``` //! +//! Padding serves no practical purpose, so where possible, encode without padding. +//! //! ### Further customization //! //! Decoding and encoding behavior can be customized by creating an