Function const_base::decode

source ·
pub const fn decode<const OUT: usize>(
    input: &[u8],
    config: Config
) -> Result<[u8; OUT], DecodeError>
Expand description

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

Errors

This function returns these errors:

Example

Base 64

use const_base::{Config, DecodeError, decode};

{
    const OUT: [u8; 5] = DecodeError::unwrap(decode(b"cm9ja28=", Config::B64));

    assert_eq!(OUT, *b"rocko");
}
{
    const OUT: Result<[u8; 4], DecodeError> =
         decode(b"bGlmZQ", Config::B64.end_padding(false));

    assert_eq!(OUT, Ok(*b"life"));
}
{
    const DECODED_A: Result<[u8; 4], DecodeError> = decode(b"bGl!ZQ", Config::B64);
    const DECODED_B: Result<[u8; 8], DecodeError> = decode(b"AAAAAA", Config::B64);
    const DECODED_C: Result<[u8; 6], DecodeError> = decode(b"AAAAA", Config::B64);
     
    assert!(matches!(DECODED_A, Err(DecodeError::InvalidByte(_))));
    assert!(matches!(DECODED_B, Err(DecodeError::WrongOutputLength(_))));
    assert!(matches!(DECODED_C, Err(DecodeError::WrongInputLength(_))));
}

Base 32

use const_base::{ArrayStr, Config, DecodeError, decode};

{
    const OUT: [u8; 3] = DecodeError::unwrap(decode(b"MNQXI===", Config::B32));

    assert_eq!(OUT, *b"cat");
}
{
    const OUT: [u8; 3] =
        DecodeError::unwrap(decode(b"MNQXI", Config::B32.end_padding(false)));

    assert_eq!(OUT, *b"cat");
}

Hexadecimal

use const_base::{Config, DecodeError, decode};

const OUT: [u8; 4] = DecodeError::unwrap(decode(b"f09f918d", Config::HEX));
assert_eq!(OUT, "👍".as_bytes());