Crate const_base[][src]

Expand description

For decoding/encoding base 64 strings at compile-time.

Examples

Encoding

use const_base::{encode, encode_as_str, Config};

{
    // the encoding macros can take both `&str` and `&[u8]` constants.
    const OUTA: &[u8; 4] = encode!("foo", Config::B64);
    const OUTB: &[u8; 4] = encode!(b"foo", Config::B64);
     
    assert_eq!(OUTA, b"Zm9v");
    assert_eq!(OUTB, b"Zm9v");
}
{
    const BYTES: &[u8] = b"hello";

    // the encoding macros can encode non-literal constants
    const OUT: &str = encode_as_str!(BYTES, Config::B64_URL_SAFE);
     
    assert_eq!(OUT, "aGVsbG8=");
}

Decoding

use const_base::{decode, Config};

{
    const OUT: &[u8] = decode!("Zm9v", Config::B64);
     
    assert_eq!(OUT, b"foo");
}
{
    const BYTES: &str = "aGVsbG8";

    // this macro can decode non-literal constants
    const OUT: &[u8] = decode!(BYTES, Config::B64_URL_SAFE.end_padding(false));
     
    assert_eq!(OUT, b"hello");
}

No-std support

const_base is #![no_std], it can be used anywhere Rust can be used.

Minimum Supported Rust Version

const_base requires Rust 1.51.0, because it uses const generics.

Re-exports

pub use crate::errors::DecodeError;
pub use crate::errors::InvalidByte;
pub use crate::errors::InvalidInputLength;
pub use crate::errors::MismatchedOutputLength;

Modules

All the errors from this crate.

Miscelaneous utility functions.

Macros

Decodes the $slice constant into a &[u8; N] with the encoding determined by $config.

Encodes the $slice constant into a &[u8; N] with the encoding determined by $config.

Encodes the $slice constant into a &str with the encoding determined by $config.

A const equivalent of Result::unwrap_or, where $else is only evaluated on error.

Structs

For configuring how a string is encoded/decoded.

Enums

Determines which characters are used for the Base64 encoding

Determines which encoding is used.

Functions

Decodes input into a [u8; OUT] with the encoding determined by config.

Computes the length of the string obtained from decoding encoded with the encoding determined by config.

Encodes input into a [u8; OUT] with the encoding determined by config.

Computes the length of the encoded string from the unencoded_length, using the encoding determined by config.