Skip to content

Commit

Permalink
Further padding consts
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallpierce committed Jan 19, 2025
1 parent b58dcac commit 142c3bd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
30 changes: 26 additions & 4 deletions src/engine/general_purpose/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 142c3bd

Please sign in to comment.