Struct Alphabet
pub struct Alphabet { /* private fields */ }
An alphabet defines the 64 ASCII characters (symbols) used for base64.
Common alphabets are provided as constants, and custom alphabets
can be made via from_str or the TryFrom<str> implementation.
Examples
Building and using a custom Alphabet:
let custom = base64::alphabet::Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").unwrap();
let engine = base64::engine::GeneralPurpose::new(
&custom,
base64::engine::general_purpose::PAD);
Building a const:
use base64::alphabet::Alphabet;
static CUSTOM: Alphabet = {
// Result::unwrap() isn't const yet, but panic!() is OK
match Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/") {
Ok(x) => x,
Err(_) => panic!("creation of alphabet failed"),
}
};
Building lazily:
use base64::alphabet::Alphabet;
use std::sync::LazyLock;
static CUSTOM: LazyLock<Alphabet> = LazyLock::new(||
Alphabet::new("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").unwrap()
);
Implementations
impl Alphabet
const fn new(alphabet: &str) -> Result<Self, ParseAlphabetError>Create an
Alphabetfrom a string of 64 unique printable ASCII bytes with=as the padding symbol.The padding symbol
=is not allowed in the alphabet.See
Self::new_with_paddingif a non-default padding symbol is needed.const fn new_with_padding(alphabet: &str, padding: Symbol) -> Result<Self, ParseAlphabetError>Create an
Alphabetfrom a string of 64 unique printable ASCII bytes, with a custom padding symbol.The padding symbol must not appear in the alphabet.
This is meant for strange alphabets that don't use
=as the padding symbol.fn as_str(&self) -> &strA
&strcontaining the symbols in theAlphabet(excluding padding)fn symbols(&self) -> [Symbol; 64]The 64 symbols of the alphabet (excluding padding).
fn padding(&self) -> SymbolThe symbol used for padding.
Trait Implementations
impl Clone for Alphabet
fn clone(&self) -> Self
impl Debug for Alphabet
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Alphabet
impl PartialEq for Alphabet
fn eq(&self, other: &Self) -> bool
impl StructuralPartialEq for Alphabet
impl TryFrom<&str> for Alphabet
type Error = ParseAlphabetError;fn try_from(value: &str) -> Result<Self, Self::Error>