Encoder

Struct Encoder 

Source
pub struct Encoder { /* private fields */ }
Expand description

BASE64 encoder struct

Implementations§

Source§

impl Encoder

Source

pub const fn new() -> Self

Creates a new instance of the encoder using the default base64 alphabet

§Examples
use irelia_encoder::Encoder;

const ENCODER: Encoder = Encoder::new();
Source

pub const fn with_encode_table(encode_table: [u8; 64]) -> Self

Creates a new instance of the encoder using a specified alphabet

§Example:
use irelia_encoder::Encoder;

const ALPHABET: [u8; 64] = [
    b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N',
    b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b',
    b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p',
    b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'0', b'1', b'2', b'3',
    b'4', b'5', b'6', b'7', b'8', b'9', b'+', b'/',
];

const ENCODER: Encoder = Encoder::with_encode_table(ALPHABET);
Source

pub fn encode<T>(&self, bytes: T) -> String
where T: AsRef<[u8]>,

Converts the bytes to BASE64

§Examples
use irelia_encoder::Encoder;
const ENCODER: Encoder = Encoder::new();

let base64_encoded = ENCODER.encode("Hello, World!");
§Panics

This panics if the buffer does not produce valid utf-8, this should never happen if the default alphabet is in use

Source

pub fn encode_with_ascii_check<T>(&self, bytes: T) -> String
where T: AsRef<[u8]>,

Converts the bytes to BASE64, and validates that the BASE64 is all ASCII

§Examples
use irelia_encoder::Encoder;
const ENCODER: Encoder = Encoder::new();

let base64_encoded = ENCODER.encode_with_ascii_check("Hello, World!");
§Panics

This function panics if the buffer produced is not valid ASCII This should not happen if the default alphabet is in use

Source

pub unsafe fn encode_unchecked<T>(&self, bytes: T) -> String
where T: AsRef<[u8]>,

Converts the bytes to BASE64, but doesn’t check if the output is valid UTF-8

§Example:
use irelia_encoder::Encoder;
const ENCODER: Encoder = Encoder::new();

let base64_encoded = unsafe { ENCODER.encode_unchecked("Hello, World!") };
§Safety

The characters used for the encode table need to be valid UTF-8 This is true with the default table, but might not be for custom ones.

Source

pub fn encode_without_padding<T>(&self, bytes: T) -> String
where T: AsRef<[u8]>,

Converts the bytes to BASE64 without padding

§Examples
use irelia_encoder::Encoder;
const ENCODER: Encoder = Encoder::new();

let base64_encoded = ENCODER.encode_without_padding("Hello, World!");
§Panics

This function will panic if the buffer does not produce valid UTF-8, which should never happen

Source

pub unsafe fn encode_unchecked_without_padding<T>(&self, bytes: T) -> String
where T: AsRef<[u8]>,

Converts the bytes to BASE64 without padding, but doesn’t check if the output is valid UTF-8

§Example:
use irelia_encoder::Encoder;
const ENCODER: Encoder = Encoder::new();

let base64_encoded = unsafe { ENCODER.encode_unchecked("Hello, World!") };
§Safety

The characters used for the encode table need to be valid UTF-8 This is true with the default table, but might not be for custom ones.

Trait Implementations§

Source§

impl Default for Encoder

Source§

fn default() -> Self

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

Auto Trait Implementations§

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> 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, 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.