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