use crate::encoding;
use crate::error::Result;
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
macro_rules! derive_base_encoding {
( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => {
$(
#[$doc]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct $type;
impl BaseCodec for $type {
fn encode<I: AsRef<[u8]>>(input: I) -> String {
$encoding.encode(input.as_ref())
}
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
Ok($encoding.decode(input.as_ref().as_bytes())?)
}
}
)*
};
}
macro_rules! derive_base_x {
( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => {
$(
#[$doc]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct $type;
impl BaseCodec for $type {
fn encode<I: AsRef<[u8]>>(input: I) -> String {
base_x::encode($encoding, input.as_ref())
}
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
Ok(base_x::decode($encoding, input.as_ref())?)
}
}
)*
};
}
pub(crate) trait BaseCodec {
fn encode<I: AsRef<[u8]>>(input: I) -> String;
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>>;
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Identity;
impl BaseCodec for Identity {
fn encode<I: AsRef<[u8]>>(input: I) -> String {
String::from_utf8(input.as_ref().to_vec()).expect("input must be valid UTF-8 bytes")
}
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
Ok(input.as_ref().as_bytes().to_vec())
}
}
derive_base_encoding! {
Base2, encoding::BASE2;
Base8, encoding::BASE8;
Base16Lower, encoding::BASE16_LOWER;
Base16Upper, encoding::BASE16_UPPER;
Base32Lower, encoding::BASE32_NOPAD_LOWER;
Base32Upper, encoding::BASE32_NOPAD_UPPER;
Base32PadLower, encoding::BASE32_PAD_LOWER;
Base32PadUpper, encoding::BASE32_PAD_UPPER;
Base32HexLower, encoding::BASE32HEX_NOPAD_LOWER;
Base32HexUpper, encoding::BASE32HEX_NOPAD_UPPER;
Base32HexPadLower, encoding::BASE32HEX_PAD_LOWER;
Base32HexPadUpper, encoding::BASE32HEX_PAD_UPPER;
Base32Z, encoding::BASE32Z;
Base64, encoding::BASE64_NOPAD;
Base64Pad, encoding::BASE64_PAD;
Base64Url, encoding::BASE64URL_NOPAD;
Base64UrlPad, encoding::BASE64URL_PAD;
}
derive_base_x! {
Base10, encoding::BASE10;
Base58Flickr, encoding::BASE58_FLICKR;
Base58Btc, encoding::BASE58_BITCOIN;
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base36Lower;
impl BaseCodec for Base36Lower {
fn encode<I: AsRef<[u8]>>(input: I) -> String {
base_x::encode(encoding::BASE36_LOWER, input.as_ref())
}
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
let lowercased = input.as_ref().to_ascii_lowercase();
Ok(base_x::decode(encoding::BASE36_LOWER, &lowercased)?)
}
}
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) struct Base36Upper;
impl BaseCodec for Base36Upper {
fn encode<I: AsRef<[u8]>>(input: I) -> String {
base_x::encode(encoding::BASE36_UPPER, input.as_ref())
}
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
let uppercased = input.as_ref().to_ascii_uppercase();
Ok(base_x::decode(encoding::BASE36_UPPER, &uppercased)?)
}
}