Expand description
For decoding/encoding base 64/32/16 strings at compile-time.
§Examples
§Encoding
use const_base::{encode_as_str, Config};
{
// the encoding macros can take both `&str` and `&[u8]` constants.
const OUTA: &str = encode_as_str!("foo", Config::B64);
const OUTB: &str = encode_as_str!(b"foo", Config::B64);
assert_eq!(OUTA, "Zm9v");
assert_eq!(OUTB, "Zm9v");
}
{
const BYTES: &[u8] = b"hello";
// the encoding macros can encode_as_str 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!("MZXW6===", Config::B32);
assert_eq!(OUT, b"foo");
}
{
const BYTES: &[u8] = b"f000";
// this macro can decode non-literal constants
const OUT: &[u8] = decode!(BYTES, Config::HEX);
assert_eq!(OUT, &[0xF0, 0x00]);
}§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.64.0.
Re-exports§
pub use crate::errors::DecodeError;pub use crate::errors::ExcessBits;pub use crate::errors::InvalidByte;pub use crate::errors::WrongInputLength;pub use crate::errors::WrongOutputLength;
Modules§
Macros§
- decode
- Decodes the
$sliceconstant into a&[u8; N]with the encoding determined by$config. - encode
- Encodes the
$sliceconstant into a&'static ArrayStr<LEN>, with the encoding determined by$config. - encode_
as_ str - Encodes the
$sliceconstant into a&'static str, with the encoding determined by$config.
Structs§
Enums§
- B32Char
Set - Determines which characters are used for the Base32 encoding
- B64Char
Set - Determines which characters are used for the Base64 encoding
- Encoding
- Determines which encoding is used.
- HexChar
Set - Determines which characters are used for the Hexadecimal encoding
Functions§
- decode
- Decodes
inputinto a[u8; OUT]with the encoding determined byconfig. - decoded_
len - Computes the length of the string obtained from decoding
encodedwith the encoding determined byconfig. - encode
- Encodes
inputinto anArrayStr<OUT>with the encoding determined byconfig. - encoded_
len - Computes the length of the encoded string from the
unencoded_length, using the encoding determined byconfig.