1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/// Decodes the `$slice` constant into a `&[u8; N]` with the encoding determined by [`$config`].
///
/// `$slice` can be a `&'static str`, `&'static [u8; N]`, or `&'static [u8]`.
///
/// # Compile-time Errors
///
/// When this macro is passed a malformed slice, it'll produce compile-time errors in the
/// same situations where [`crate::decode`](crate::decode()) would return an error.
///
/// For an example of what those look like, look [down here](#erroring-example)
///
/// # Examples
///
/// ### Base 64
///
/// ```rust
/// use const_base::{decode, Config};
///
/// {
///     const OUT: &[u8] = decode!("SGVsbG8sIHdvcmxkIQ==", Config::B64);
///     
///     assert_eq!(OUT, b"Hello, world!");
/// }
/// {
///     const BYTES: &[u8] = b"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ";
///
///     // this macro can decode non-literal constants
///     const OUT: &[u8; 26] = decode!(BYTES, Config::B64_URL_SAFE.end_padding(false));
///     
///     assert_eq!(OUT, b"Lorem ipsum dolor sit amet");
/// }
/// ```
///
/// ### Base 32
///
/// ```rust
/// # fn main(){
/// use const_base::{decode, Config};
///
/// const OUT: &[u8] = decode!("MZXCA3LBNFXCQKJAPN6Q====", Config::B32);
///     
/// assert_eq!(OUT, b"fn main() {}");
/// # }
/// ```
///
/// <div id = "erroring-example"></div>
///
///
/// ### Hexadecimal
///
/// ```rust
/// use const_base::{decode, Config};
///
/// const OUT: &[u8] = decode!("F00B", Config::HEX);
///     
/// assert_eq!(OUT, &[0xF0, 0x0B]);
/// ```
///
/// <div id = "erroring-example"></div>
///
/// ### Erroring
///
/// Malformed inputs like this
///
/// ```compile_fail
/// use const_base::{decode, Config};
/// decode!("A", Config::B64);
/// ```
/// produce compile-time errors that look like this:
/// ```text
/// error[E0308]: mismatched types
///  --> src/codec_macros.rs:39:1
///   |
/// 5 | decode!("A", Config::B64);
///   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `IsOk`, found struct `const_base::msg::InvalidInputLength`
///   |
///   = note: expected struct `IsOk`
///              found struct `const_base::msg::InvalidInputLength<length<1_usize>>`
///   = note: this error originates in the macro `$crate::__result_tuple_to_singleton` (in Nightly builds, run with -Z macro-backtrace for more info)
///
/// ```
/// This macro emulates panics using type errors like those.
///
/// In this case, the error is `InvalidInputLength`,
/// because the input string can't be `4 * n + 1` bytes long (`n` can be any positive integer).
///
///
/// [`$config`]: crate::Config
#[macro_export]
macro_rules! decode {
    ($slice:expr, $config:expr $(,)*) => {{
        const __P_NHPMWYD3NJA: $crate::__::CodecArgs =
            $crate::__::DecodeArgsFrom($slice, $config).conv();
        {
            const OUT: &$crate::__AdjacentResult<
                [$crate::__::u8; __P_NHPMWYD3NJA.out_len],
                $crate::DecodeError,
            > = &$crate::__priv_decode(__P_NHPMWYD3NJA.input, __P_NHPMWYD3NJA.cfg);

            const _: $crate::msg::IsOk =
                $crate::__result_tuple_to_singleton!($crate::msg::__decode_res_to_tuple(&OUT.err));

            &OUT.ok
        }
    }};
}

/// Encodes the `$slice` constant into a `&[u8; N]` with the encoding determined by [`$config`].
///
/// `$slice` slice can be a `&'static str`, `&'static [u8; N]`, or `&'static [u8]`.
///
/// # Examples
///
/// ### Base 64
///
/// ```rust
/// use const_base::{encode, Config};
///
/// {
///     const OUT: &[u8; 4] = encode!("bar", Config::B64);
///     
///     assert_eq!(OUT, b"YmFy");
/// }
/// {
///     const BYTES: &[u8] = b"world";
///
///     // this macro can encode non-literal constants
///     const OUT: &[u8] = encode!(BYTES, Config::B64_URL_SAFE);
///     
///     assert_eq!(OUT, b"d29ybGQ=");
/// }
/// ```
///
/// ### Base 32
///
/// ```rust
/// use const_base::{encode, Config};
///
/// const OUT: &[u8] = encode!(&[3, 5, 8], Config::B32);
///     
/// assert_eq!(OUT, b"AMCQQ===");
/// ```
///
/// ### Base 32
///
/// ```rust
/// use const_base::{encode, Config};
///
/// const OUT: &[u8] = encode!(&[3, 5, 8], Config::B32);
///     
/// assert_eq!(OUT, b"AMCQQ===");
/// ```
///
/// ### Hexadecimal
///
/// ```rust
/// use const_base::{encode, Config};
///
/// const LOWER: &[u8] = encode!(&[0xB0, 0x01], Config::HEX_LOWER);
/// const UPPER: &[u8] = encode!(&[0xB0, 0x01], Config::HEX);
///     
/// assert_eq!(LOWER, b"b001");
/// assert_eq!(UPPER, b"B001");
/// ```
///
///
///
/// [`$config`]: crate::Config
#[macro_export]
macro_rules! encode {
    ($slice:expr, $config:expr $(,)*) => {{
        const __P_NHPMWYD3NJA: $crate::__::CodecArgs =
            $crate::__::EncodeArgsFrom($slice, $config).conv();

        {
            const OUT: &[$crate::__::u8; __P_NHPMWYD3NJA.out_len] =
                &$crate::__priv_encode(__P_NHPMWYD3NJA.input, __P_NHPMWYD3NJA.cfg).ok;

            OUT
        }
    }};
}

/// Encodes the `$slice` constant into a `&str` with the encoding determined by [`$config`].
///
/// `$slice` can be a `&'static str`, `&'static [u8; N]`, or `&'static [u8]`.
///
/// # Examples
///
/// ### Base 64
///
/// ```rust
/// use const_base::{encode_as_str, Config};
///
/// {
///     const OUT: &str = encode_as_str!("qux", Config::B64);
///     
///     assert_eq!(OUT, "cXV4");
/// }
/// {
///     const BYTES: &[u8] = b"goodbye";
///
///     // this macro can encode non-literal constants
///     const OUT: &str = encode_as_str!(BYTES, Config::B64_URL_SAFE);
///     
///     assert_eq!(OUT, "Z29vZGJ5ZQ==");
/// }
/// ```
///
/// ### Base 32
///
/// ```rust
/// use const_base::{encode_as_str, Config};
///
/// const OUT: &str = encode_as_str!(&[13, 21, 34], Config::B32);
///     
/// assert_eq!(OUT, "BUKSE===");
/// ```
///
/// ### Hexadecimal
///
/// ```rust
/// use const_base::{encode_as_str, Config};
///
/// const LOWER: &str = encode_as_str!(&[0xB1, 0x00, 0x0d], Config::HEX_LOWER);
/// const UPPER: &str = encode_as_str!(&[0xB1, 0x00, 0x0d], Config::HEX);
///     
/// assert_eq!(LOWER, "b1000d");
/// assert_eq!(UPPER, "B1000D");
/// ```
///
///
/// [`$config`]: crate::Config
#[macro_export]
macro_rules! encode_as_str {
    ($slice:expr, $config:expr $(,)*) => {{
        const OUT_NHPMWYD3NJA: &$crate::__::str =
            unsafe { $crate::__priv_transmute_bytes_to_str!($crate::encode!($slice, $config)) };
        OUT_NHPMWYD3NJA
    }};
}

#[macro_export]
#[doc(hidden)]
macro_rules! __priv_transmute_bytes_to_str {
    ($bytes:expr) => {{
        let bytes: &'static [$crate::__::u8] = $bytes;
        let string: &'static $crate::__::str = {
            $crate::__priv_utils::PtrToRef {
                ptr: bytes as *const [$crate::__::u8] as *const $crate::__::str,
            }
            .reff
        };
        string
    }};
}