multibase/
base.rs

1use crate::error::{Error, Result};
2use crate::impls::*;
3
4#[cfg(not(feature = "std"))]
5use alloc::{string::String, vec::Vec};
6
7macro_rules! build_base_enum {
8    ( $(#[$attr:meta] $code:expr => $base:ident,)* ) => {
9        /// List of types currently supported in the multibase spec.
10        ///
11        /// Not all base types are supported by this library.
12        #[derive(PartialEq, Eq, Clone, Copy, Debug)]
13        pub enum Base {
14            $( #[$attr] $base, )*
15        }
16
17        impl Base {
18            /// Convert a number to the matching base algorithm, or `Error` if no algorithm is matching.
19            pub fn from_code(code: char) -> Result<Self> {
20        	    match code {
21                    $( $code => Ok(Self::$base), )*
22            	    _ => Err(Error::UnknownBase(code)),
23        	    }
24            }
25
26            /// Get the code corresponding to the base algorithm.
27            pub fn code(&self) -> char {
28                match self {
29                    $( Self::$base => $code, )*
30                }
31            }
32
33            /// Encode the given byte slice to base string.
34            pub fn encode<I: AsRef<[u8]>>(&self, input: I) -> String {
35                match self {
36                    $( Self::$base => $base::encode(input), )*
37                }
38            }
39
40            /// Decode the base string.
41            pub fn decode<I: AsRef<str>>(&self, input: I) -> Result<Vec<u8>> {
42                match self {
43                    $( Self::$base => $base::decode(input), )*
44                }
45            }
46        }
47    }
48}
49
50build_base_enum! {
51    /// 8-bit binary (encoder and decoder keeps data unmodified).
52    '\x00' => Identity,
53    /// Base2 (alphabet: 01).
54    '0' => Base2,
55    /// Base8 (alphabet: 01234567).
56    '7' => Base8,
57    /// Base10 (alphabet: 0123456789).
58    '9' => Base10,
59    /// Base16 lower hexadecimal (alphabet: 0123456789abcdef).
60    'f' => Base16Lower,
61    /// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF).
62    'F' => Base16Upper,
63     /// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
64    'b' => Base32Lower,
65    /// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
66    'B' => Base32Upper,
67    /// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567).
68    'c' => Base32PadLower,
69    /// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567).
70    'C' => Base32PadUpper,
71    /// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
72    'v' => Base32HexLower,
73    /// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
74    'V' => Base32HexUpper,
75    /// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv).
76    't' => Base32HexPadLower,
77    /// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV).
78    'T' => Base32HexPadUpper,
79    /// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769).
80    'h' => Base32Z,
81    /// Base36, [0-9a-z] no padding (alphabet: 0123456789abcdefghijklmnopqrstuvwxyz).
82    'k' => Base36Lower,
83    /// Base36, [0-9A-Z] no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ).
84    'K' => Base36Upper,
85    /// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ).
86    'Z' => Base58Flickr,
87    /// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz).
88    'z' => Base58Btc,
89    /// Base64, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).
90    'm' => Base64,
91    /// Base64, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).
92    'M' => Base64Pad,
93    /// Base64 url, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_).
94    'u' => Base64Url,
95    /// Base64 url, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_).
96    'U' => Base64UrlPad,
97}