multi-base 1.0.2

multibase in rust
Documentation
// SPDX-License-Identifier: MIT

use crate::impls::BaseCodec;

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};

/// Generates the `Base` enum and its implementation with encode/decode methods.
///
/// This macro creates a comprehensive base encoding enum with:
/// - Enum variants for each base type with documentation
/// - `from_code()` method to convert character codes to base types
/// - `code()` method to get the character code for a base
/// - `encode()` method to encode bytes to a base string
/// - `decode()` method to decode base strings back to bytes
///
/// # Macro Hygiene
///
/// This macro uses `$crate::` prefixes to ensure proper hygiene when invoked
/// from other modules or crates.
///
/// # Parameters
///
/// The macro accepts a comma-separated list of base definitions in the form:
/// ```text
/// #[doc = "Documentation"] 'code' => VariantName,
/// ```
///
/// Where:
/// - `#[doc = "..."]` - Documentation attribute for the enum variant
/// - `'code'` - The single character or emoji that identifies this base encoding
/// - `VariantName` - The `PascalCase` name for the enum variant
///
/// # Generated Code
///
/// For each base definition, the macro generates:
/// - An enum variant in the `Base` enum
/// - A match arm in `from_code()` that maps the character code to the variant
/// - A match arm in `code()` that returns the character code
/// - A match arm in `encode()` that delegates to the base's encode function
/// - A match arm in `decode()` that delegates to the base's decode function
///
/// # Example
///
/// ```ignore
/// build_base_enum! {
///     /// Base64 encoding
///     'm' => Base64,
///     /// Base58 Bitcoin encoding
///     'z' => Base58Btc,
/// }
/// ```
///
/// This generates a `Base` enum with `Base64` and `Base58Btc` variants,
/// and implements all required methods.
///
/// # Error Handling
///
/// The `from_code()` method returns `Result<Base>` and will return
/// `Error::UnknownBase` if given an unrecognized character code.
macro_rules! build_base_enum {
    ( $(#[$attr:meta] $code:expr => $base:ident,)* ) => {
        /// List of types currently supported in the multibase spec.
        ///
        /// Not all base types are supported by this library.
        #[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
        pub enum Base {
            $( #[$attr] $base, )*
        }

        impl Base {
            /// Convert a character code to the matching base algorithm.
            ///
            /// Returns `Error::UnknownBase` if the code doesn't match any supported base.
            ///
            /// # Errors
            ///
            /// Returns [`Error::UnknownBase`](crate::error::Error::UnknownBase) if `code`
            /// does not correspond to any supported base encoding.
            #[inline]
            pub const fn from_code(code: char) -> $crate::error::Result<Self> {
        	    match code {
                    $( $code => Ok(Self::$base), )*
            	    _ => Err($crate::error::Error::UnknownBase { code }),
        	    }
            }

            /// Get the character code corresponding to the base algorithm.
            ///
            /// Each base encoding has a unique single-character (or emoji) prefix
            /// that identifies it in multibase strings.
            #[inline]
            pub const fn code(&self) -> char {
                match self {
                    $( Self::$base => $code, )*
                }
            }

            /// Encode the given byte slice to a base string (without prefix).
            ///
            /// This method returns only the encoded data, without the base code prefix.
            /// Use the public `encode()` function to get the full multibase string.
            #[inline]
            pub fn encode<I: AsRef<[u8]>>(&self, input: I) -> String {
                match self {
                    $( Self::$base => $crate::impls::$base::encode(input), )*
                }
            }

            /// Decode the base string (without prefix) back to bytes.
            ///
            /// The `strict` parameter controls whether to accept case-insensitive
            /// input for applicable bases.
            ///
            /// # Parameters
            ///
            /// * `input` - The encoded string (without the multibase prefix)
            /// * `strict` - If true, enforce strict decoding rules (case-sensitive, etc.)
            ///
            /// # Errors
            ///
            /// Returns an error if the input contains invalid characters for this base
            /// or if the input is malformed.
            #[inline]
            pub fn decode<I: AsRef<str>>(&self, input: I, strict: bool) -> $crate::error::Result<Vec<u8>> {
                match self {
                    $( Self::$base => $crate::impls::$base::decode(input, strict), )*
                }
            }

            #[inline]
            pub(crate) fn decode_into<I: AsRef<str>>(&self, input: I, strict: bool, buffer: &mut Vec<u8>) -> $crate::error::Result<()> {
                match self {
                    $( Self::$base => $crate::impls::$base::decode_into(input, strict, buffer), )*
                }
            }
        }
    }
}

build_base_enum! {
    /// 8-bit binary (encoder and decoder keeps data unmodified).
    '\x00' => Identity,
    /// Base2 (alphabet: 01).
    '0' => Base2,
    /// Base8 (alphabet: 01234567).
    '7' => Base8,
    /// Base10 (alphabet: 0123456789).
    '9' => Base10,
    /// Base16 lower hexadecimal (alphabet: 0123456789abcdef).
    'f' => Base16Lower,
    /// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF).
    'F' => Base16Upper,
     /// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
    'b' => Base32Lower,
    /// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
    'B' => Base32Upper,
    /// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
    'c' => Base32PadLower,
    /// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
    'C' => Base32PadUpper,
    /// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
    'v' => Base32HexLower,
    /// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
    'V' => Base32HexUpper,
    /// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
    't' => Base32HexPadLower,
    /// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
    'T' => Base32HexPadUpper,
    /// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769).
    'h' => Base32Z,
    /// Base36, [0-9a-z] no padding (alphabet: 0123456789abcdefghijklmnopqrstuvwxyz).
    'k' => Base36Lower,
    /// Base36, [0-9A-Z] no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ).
    'K' => Base36Upper,
    /// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ).
    'Z' => Base58Flickr,
    /// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz).
    'z' => Base58Btc,
    /// Base64, `rfc4648` no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).
    'm' => Base64,
    /// Base64, `rfc4648` with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).
    'M' => Base64Pad,
    /// Base64 url, `rfc4648` no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_).
    'u' => Base64Url,
    /// Base64 url, `rfc4648` with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_).
    'U' => Base64UrlPad,
    /// Base256Emoji (alphabet: ๐Ÿš€๐Ÿชโ˜„๐Ÿ›ฐ๐ŸŒŒ๐ŸŒ‘๐ŸŒ’๐ŸŒ“๐ŸŒ”๐ŸŒ•๐ŸŒ–๐ŸŒ—๐ŸŒ˜๐ŸŒ๐ŸŒ๐ŸŒŽ๐Ÿ‰โ˜€๐Ÿ’ป๐Ÿ–ฅ๐Ÿ’พ๐Ÿ’ฟ๐Ÿ˜‚โค๐Ÿ˜๐Ÿคฃ๐Ÿ˜Š๐Ÿ™๐Ÿ’•๐Ÿ˜ญ๐Ÿ˜˜๐Ÿ‘๐Ÿ˜…๐Ÿ‘๐Ÿ˜๐Ÿ”ฅ๐Ÿฅฐ๐Ÿ’”๐Ÿ’–๐Ÿ’™๐Ÿ˜ข๐Ÿค”๐Ÿ˜†๐Ÿ™„๐Ÿ’ช๐Ÿ˜‰โ˜บ๐Ÿ‘Œ๐Ÿค—๐Ÿ’œ๐Ÿ˜”๐Ÿ˜Ž๐Ÿ˜‡๐ŸŒน๐Ÿคฆ๐ŸŽ‰๐Ÿ’žโœŒโœจ๐Ÿคท๐Ÿ˜ฑ๐Ÿ˜Œ๐ŸŒธ๐Ÿ™Œ๐Ÿ˜‹๐Ÿ’—๐Ÿ’š๐Ÿ˜๐Ÿ’›๐Ÿ™‚๐Ÿ’“๐Ÿคฉ๐Ÿ˜„๐Ÿ˜€๐Ÿ–ค๐Ÿ˜ƒ๐Ÿ’ฏ๐Ÿ™ˆ๐Ÿ‘‡๐ŸŽถ๐Ÿ˜’๐Ÿคญโฃ๐Ÿ˜œ๐Ÿ’‹๐Ÿ‘€๐Ÿ˜ช๐Ÿ˜‘๐Ÿ’ฅ๐Ÿ™‹๐Ÿ˜ž๐Ÿ˜ฉ๐Ÿ˜ก๐Ÿคช๐Ÿ‘Š๐Ÿฅณ๐Ÿ˜ฅ๐Ÿคค๐Ÿ‘‰๐Ÿ’ƒ๐Ÿ˜ณโœ‹๐Ÿ˜š๐Ÿ˜๐Ÿ˜ด๐ŸŒŸ๐Ÿ˜ฌ๐Ÿ™ƒ๐Ÿ€๐ŸŒท๐Ÿ˜ป๐Ÿ˜“โญโœ…๐Ÿฅบ๐ŸŒˆ๐Ÿ˜ˆ๐Ÿค˜๐Ÿ’ฆโœ”๐Ÿ˜ฃ๐Ÿƒ๐Ÿ’โ˜น๐ŸŽŠ๐Ÿ’˜๐Ÿ˜ โ˜๐Ÿ˜•๐ŸŒบ๐ŸŽ‚๐ŸŒป๐Ÿ˜๐Ÿ–•๐Ÿ’๐Ÿ™Š๐Ÿ˜น๐Ÿ—ฃ๐Ÿ’ซ๐Ÿ’€๐Ÿ‘‘๐ŸŽต๐Ÿคž๐Ÿ˜›๐Ÿ”ด๐Ÿ˜ค๐ŸŒผ๐Ÿ˜ซโšฝ๐Ÿค™โ˜•๐Ÿ†๐Ÿคซ๐Ÿ‘ˆ๐Ÿ˜ฎ๐Ÿ™†๐Ÿป๐Ÿƒ๐Ÿถ๐Ÿ’๐Ÿ˜ฒ๐ŸŒฟ๐Ÿงก๐ŸŽโšก๐ŸŒž๐ŸŽˆโŒโœŠ๐Ÿ‘‹๐Ÿ˜ฐ๐Ÿคจ๐Ÿ˜ถ๐Ÿค๐Ÿšถ๐Ÿ’ฐ๐Ÿ“๐Ÿ’ข๐ŸคŸ๐Ÿ™๐Ÿšจ๐Ÿ’จ๐Ÿคฌโœˆ๐ŸŽ€๐Ÿบ๐Ÿค“๐Ÿ˜™๐Ÿ’Ÿ๐ŸŒฑ๐Ÿ˜–๐Ÿ‘ถ๐Ÿฅดโ–ถโžกโ“๐Ÿ’Ž๐Ÿ’ธโฌ‡๐Ÿ˜จ๐ŸŒš๐Ÿฆ‹๐Ÿ˜ท๐Ÿ•บโš ๐Ÿ™…๐Ÿ˜Ÿ๐Ÿ˜ต๐Ÿ‘Ž๐Ÿคฒ๐Ÿค ๐Ÿคง๐Ÿ“Œ๐Ÿ”ต๐Ÿ’…๐Ÿง๐Ÿพ๐Ÿ’๐Ÿ˜—๐Ÿค‘๐ŸŒŠ๐Ÿคฏ๐Ÿทโ˜Ž๐Ÿ’ง๐Ÿ˜ฏ๐Ÿ’†๐Ÿ‘†๐ŸŽค๐Ÿ™‡๐Ÿ‘โ„๐ŸŒด๐Ÿ’ฃ๐Ÿธ๐Ÿ’Œ๐Ÿ“๐Ÿฅ€๐Ÿคข๐Ÿ‘…๐Ÿ’ก๐Ÿ’ฉ๐Ÿ‘๐Ÿ“ธ๐Ÿ‘ป๐Ÿค๐Ÿคฎ๐ŸŽผ๐Ÿฅต๐Ÿšฉ๐ŸŽ๐ŸŠ๐Ÿ‘ผ๐Ÿ’๐Ÿ“ฃ๐Ÿฅ‚)
    '๐Ÿš€' => Base256Emoji,
}