Skip to main content

Profile

Struct Profile 

Source
pub struct Profile<A, const PAD: bool> { /* private fields */ }
Expand description

A named Base64 profile with an engine and optional strict line wrapping.

Profiles are convenience values for protocol-shaped Base64. They keep the same strict alphabet, padding, canonical-bit, and output-buffer rules as Engine, while carrying the wrapping policy for MIME/PEM-like formats.

Implementations§

Source§

impl<A, const PAD: bool> Profile<A, PAD>
where A: Alphabet,

Source

pub const fn new(engine: Engine<A, PAD>, wrap: Option<LineWrap>) -> Self

Creates a profile from an engine and optional strict line wrapping.

Source

pub const fn checked_new( engine: Engine<A, PAD>, wrap: Option<LineWrap>, ) -> Option<Self>

Creates a profile, returning None when the wrapping policy is invalid.

This is useful when a profile is assembled from configuration or other untrusted metadata. Use Self::new for compile-time constants where the wrapping policy is known to be valid.

Source

pub const fn is_valid(&self) -> bool

Returns whether this profile can be used by encoders and decoders.

Source

pub const fn engine(&self) -> Engine<A, PAD>

Returns the underlying engine.

Source

pub const fn is_padded(&self) -> bool

Returns whether this profile uses padded Base64.

Source

pub const fn is_wrapped(&self) -> bool

Returns whether this profile carries a strict line-wrapping policy.

Source

pub const fn line_wrap(&self) -> Option<LineWrap>

Returns the strict wrapping policy carried by this profile, if any.

Source

pub const fn line_len(&self) -> Option<usize>

Returns the encoded line length for wrapped profiles.

Source

pub const fn line_ending(&self) -> Option<LineEnding>

Returns the line ending for wrapped profiles.

Source

pub const fn encoded_len(&self, input_len: usize) -> Result<usize, EncodeError>

Returns the encoded length for this profile.

Source

pub const fn checked_encoded_len(&self, input_len: usize) -> Option<usize>

Returns the encoded length for this profile, or None on overflow or invalid line wrapping.

Source

pub fn decoded_len(&self, input: &[u8]) -> Result<usize, DecodeError>

Returns the exact decoded length for this profile.

Source

pub fn validate_result(&self, input: &[u8]) -> Result<(), DecodeError>

Validates input according to this profile without writing decoded bytes.

Source

pub fn validate(&self, input: &[u8]) -> bool

Returns whether input is valid for this profile.

Source

pub fn encode_slice( &self, input: &[u8], output: &mut [u8], ) -> Result<usize, EncodeError>

Encodes input into output according to this profile.

Source

pub fn encode_slice_clear_tail( &self, input: &[u8], output: &mut [u8], ) -> Result<usize, EncodeError>

Encodes input into output and clears all bytes after the encoded prefix.

Source

pub fn encode_buffer<const CAP: usize>( &self, input: &[u8], ) -> Result<EncodedBuffer<CAP>, EncodeError>

Encodes input into a stack-backed buffer.

This is useful for short values where heap allocation is unnecessary. If encoding fails, the internal backing array is cleared before the error is returned.

Source

pub fn decode_slice( &self, input: &[u8], output: &mut [u8], ) -> Result<usize, DecodeError>

Decodes input into output according to this profile.

Source

pub fn decode_slice_clear_tail( &self, input: &[u8], output: &mut [u8], ) -> Result<usize, DecodeError>

Decodes input into output and clears all bytes after the decoded prefix.

Source

pub fn decode_buffer<const CAP: usize>( &self, input: &[u8], ) -> Result<DecodedBuffer<CAP>, DecodeError>

Decodes input into a stack-backed buffer according to this profile.

This is useful for short decoded values where heap allocation is unnecessary. If decoding fails, the internal backing array is cleared before the error is returned.

Source

pub fn decode_in_place<'a>( &self, buffer: &'a mut [u8], ) -> Result<&'a mut [u8], DecodeError>

Decodes buffer in place according to this profile.

For wrapped profiles, configured line endings are compacted out before decoding. If validation fails, the buffer contents are unspecified.

§Examples
use base64_ng::{LineEnding, LineWrap, Profile, STANDARD};

let profile = Profile::new(STANDARD, Some(LineWrap::new(4, LineEnding::Lf)));
let mut buffer = *b"aGVs\nbG8=";
let decoded = profile.decode_in_place(&mut buffer).unwrap();

assert_eq!(decoded, b"hello");
Source

pub fn decode_in_place_clear_tail<'a>( &self, buffer: &'a mut [u8], ) -> Result<&'a mut [u8], DecodeError>

Decodes buffer in place according to this profile and clears all bytes after the decoded prefix.

If validation or decoding fails, the entire buffer is cleared before the error is returned.

§Examples
use base64_ng::{LineEnding, LineWrap, Profile, STANDARD};

let profile = Profile::new(STANDARD, Some(LineWrap::new(4, LineEnding::Lf)));
let mut buffer = *b"aGVs\nbG8=";
let len = profile.decode_in_place_clear_tail(&mut buffer).unwrap().len();

assert_eq!(&buffer[..len], b"hello");
assert!(buffer[len..].iter().all(|byte| *byte == 0));
Source

pub fn encode_vec(&self, input: &[u8]) -> Result<Vec<u8>, EncodeError>

Encodes input into a newly allocated byte vector.

Source

pub fn encode_secret(&self, input: &[u8]) -> Result<SecretBuffer, EncodeError>

Encodes input into a redacted owned secret buffer.

Source

pub fn encode_string(&self, input: &[u8]) -> Result<String, EncodeError>

Encodes input into a newly allocated UTF-8 string.

Source

pub fn decode_vec(&self, input: &[u8]) -> Result<Vec<u8>, DecodeError>

Decodes input into a newly allocated byte vector.

Source

pub fn decode_secret(&self, input: &[u8]) -> Result<SecretBuffer, DecodeError>

Decodes input into a redacted owned secret buffer.

Trait Implementations§

Source§

impl<A: Clone, const PAD: bool> Clone for Profile<A, PAD>

Source§

fn clone(&self) -> Profile<A, PAD>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug, const PAD: bool> Debug for Profile<A, PAD>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, const PAD: bool> Default for Profile<A, PAD>
where A: Alphabet,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A, const PAD: bool> Display for Profile<A, PAD>
where A: Alphabet,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, const PAD: bool> From<Engine<A, PAD>> for Profile<A, PAD>
where A: Alphabet,

Source§

fn from(engine: Engine<A, PAD>) -> Self

Converts to this type from the input type.
Source§

impl<A: PartialEq, const PAD: bool> PartialEq for Profile<A, PAD>

Source§

fn eq(&self, other: &Profile<A, PAD>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: Copy, const PAD: bool> Copy for Profile<A, PAD>

Source§

impl<A: Eq, const PAD: bool> Eq for Profile<A, PAD>

Source§

impl<A, const PAD: bool> StructuralPartialEq for Profile<A, PAD>

Auto Trait Implementations§

§

impl<A, const PAD: bool> Freeze for Profile<A, PAD>

§

impl<A, const PAD: bool> RefUnwindSafe for Profile<A, PAD>
where A: RefUnwindSafe,

§

impl<A, const PAD: bool> Send for Profile<A, PAD>
where A: Send,

§

impl<A, const PAD: bool> Sync for Profile<A, PAD>
where A: Sync,

§

impl<A, const PAD: bool> Unpin for Profile<A, PAD>
where A: Unpin,

§

impl<A, const PAD: bool> UnsafeUnpin for Profile<A, PAD>

§

impl<A, const PAD: bool> UnwindSafe for Profile<A, PAD>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.