baze64 0.2.0

Encode & decode base64 from & to arbitrary byte sequences
Documentation
/// Trait for a base64 alphabet that can be used
/// to encode & decode a [`Base64String`](crate::Base64String)
pub trait Alphabet {
    /// The padding character used for the alphabet
    const PADDING: char;

    /// Returns the base64 character corresponding to a set of 6
    /// bits
    fn encode_bits(bits: u8) -> char;
    /// Decodes a base64 character into it's decoded bytes
    fn decode_char(c: char) -> u8;
}

/// The standard base64 alphabet as defined in
/// RFC 4648
#[derive(Debug, Clone, Copy)]
pub struct Standard;

/// The URL safe base64 alphabet as defined in
/// RFC 4648
#[derive(Debug, Clone, Copy)]
pub struct UrlSafe;

impl Standard {
    const ENCODE_MAP: [char; 64] = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
        'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
        '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
    ];
    const DECODE_MAP: [u8; 128] = [
        0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
        0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
        0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x3E, 0x64,
        0x64, 0x64, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x64, 0x64,
        0x64, 0x64, 0x64, 0x64, 0x64, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
        0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x19, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21,
        0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
        0x31, 0x32, 0x33, 0x64, 0x64, 0x64, 0x64, 0x64,
    ];
}

impl UrlSafe {
    const ENCODE_MAP: [char; 64] = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
        'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
        '2', '3', '4', '5', '6', '7', '8', '9', '-', '_',
    ];
    const DECODE_MAP: [u8; 128] = [
        0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
        0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64,
        0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x3E, 0x64,
        0x64, 0x64, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x64, 0x64,
        0x64, 0x64, 0x64, 0x64, 0x64, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
        0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
        0x19, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21,
        0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
        0x31, 0x32, 0x33, 0x64, 0x64, 0x64, 0x64, 0x64,
    ];
}

impl Alphabet for UrlSafe {
    const PADDING: char = '=';

    fn encode_bits(bits: u8) -> char {
        Self::ENCODE_MAP[bits as usize]
    }

    fn decode_char(c: char) -> u8 {
        if c == Self::PADDING {
            0
        } else if c == '-' {
            Self::DECODE_MAP[b'+' as usize]
        } else if c == '_' {
            Self::DECODE_MAP[b'/' as usize]
        } else {
            Self::DECODE_MAP[c as u8 as usize]
        }
    }
}

impl Alphabet for Standard {
    const PADDING: char = '=';

    fn encode_bits(bits: u8) -> char {
        Self::ENCODE_MAP[bits as usize]
    }

    fn decode_char(c: char) -> u8 {
        if c == Self::PADDING {
            0
        } else {
            Self::DECODE_MAP[c as u8 as usize]
        }
    }
}