macro_toolset/
base64.rs

1//! Base64 related macros
2
3pub use base64::*;
4
5#[macro_export]
6/// Encode given buffer into base64 string.
7///
8/// # Example:
9/// ```
10/// use macro_toolset::{b64_encode, base64::engine::general_purpose};
11///
12/// // Default padding: STANDARD, returns `String`.
13/// # let example =
14/// b64_encode!(b"hello world");
15/// # assert_eq!(example, "aGVsbG8gd29ybGQ=");
16/// // To given `String`.
17/// let mut string = String::new();
18/// b64_encode!(b"hello world" => &mut string);
19/// # assert_eq!(string, "aGVsbG8gd29ybGQ=");
20///
21/// // Returns `String`.
22/// // Available padding: STANDARD / STANDARD_NO_PAD / URL_SAFE / URL_SAFE_NO_PAD
23/// // No need to import!
24/// # let example =
25/// b64_encode!(URL_SAFE_NO_PAD: b"hello world");
26/// # assert_eq!(example, "aGVsbG8gd29ybGQ");
27/// // To given `String`.
28/// let mut string = String::new();
29/// b64_encode!(URL_SAFE_NO_PAD: b"hello world" => STRING: &mut string);
30/// # assert_eq!(string, "aGVsbG8gd29ybGQ");
31///
32/// // Returns `bytes::BytesMut`.
33/// # let example =
34/// b64_encode!(URL_SAFE_NO_PAD: b"hello world" => BYTES);
35/// # assert_eq!(&example[..], b"aGVsbG8gd29ybGQ");
36/// // To given `bytes::BytesMut`.
37/// let mut string = bytes::BytesMut::new();
38/// b64_encode!(URL_SAFE_NO_PAD: b"hello world" => BYTES: &mut string);
39/// # assert_eq!(&string[..], b"aGVsbG8gd29ybGQ");
40/// ```
41macro_rules! b64_encode {
42    ($data:expr) => {
43        $crate::b64_encode!(STANDARD: $data)
44    };
45    ($data:expr => $string:expr) => {
46        $crate::b64_encode!(STANDARD: $data => STRING: $string)
47    };
48    (STANDARD: $($tt:tt)+) => {
49        $crate::b64_encode!($crate::base64::engine::general_purpose::STANDARD, $($tt)+)
50    };
51    (STANDARD_NO_PAD: $($tt:tt)+) => {
52        $crate::b64_encode!($crate::base64::engine::general_purpose::STANDARD_NO_PAD, $($tt)+)
53    };
54    (URL_SAFE: $($tt:tt)+) => {
55        $crate::b64_encode!($crate::base64::engine::general_purpose::URL_SAFE, $($tt)+)
56    };
57    (URL_SAFE_NO_PAD: $($tt:tt)+) => {
58        $crate::b64_encode!($crate::base64::engine::general_purpose::URL_SAFE_NO_PAD, $($tt)+)
59    };
60    ($padding:path, $data:expr) => {{
61        let mut string = String::with_capacity($data.len() * 4 / 3 + 4 + 64);
62
63        $crate::base64::Engine::encode_string(&$padding, $data, &mut string);
64
65        string
66    }};
67    ($padding:path, $data:expr => STRING: $string:expr) => {
68        $crate::base64::Engine::encode_string(&$padding, $data, $string)
69    };
70    ($padding:path, $data:expr => BYTES) => {{
71        let target_len = $data.len() * 4 / 3 + 4 + 64;
72
73        // Allocate buffer
74        let mut bytes_buf = bytes::BytesMut::with_capacity(target_len);
75        // `target_len` is the exact length of the base64 string.
76        #[allow(unsafe_code)]
77        unsafe {
78            bytes_buf.set_len(target_len)
79        };
80
81        let bytes_written =
82            $crate::base64::Engine::encode_slice(&$padding, $data, bytes_buf.as_mut()).unwrap_or(0);
83
84        bytes_buf.truncate(bytes_written);
85        bytes_buf
86    }};
87    ($padding:path, $data:expr => BYTES: $bytes_buf:expr) => {{
88        let target_len = $data.len() * 4 / 3 + 4 + 64;
89
90        // Allocate buffer
91        $bytes_buf.reserve(target_len);
92        // `target_len` is the exact length of the base64 string.
93        #[allow(unsafe_code)]
94        unsafe {
95            $bytes_buf.set_len(target_len)
96        };
97
98        let bytes_written =
99            $crate::base64::Engine::encode_slice(&$padding, $data, $bytes_buf).unwrap_or(0);
100
101        $bytes_buf.truncate(bytes_written);
102    }};
103}
104
105#[macro_export]
106/// Decode given base64 string to bytes.
107///
108/// # Example:
109/// ```
110/// use macro_toolset::{b64_decode, base64::engine::general_purpose};
111/// // Default padding: STANDARD, returns `Vec<u8>`.
112/// # let example =
113/// b64_decode!("aGVsbG8gd29ybGQ=")  
114/// # .unwrap();
115/// # assert_eq!(example, b"hello world");
116/// // Available padding: STANDARD / STANDARD_NO_PAD / URL_SAFE / URL_SAFE_NO_PAD.
117/// // No need to import!
118/// # let example =
119/// b64_decode!(URL_SAFE_NO_PAD: "aGVsbG8gd29ybGQ")
120/// # .unwrap();
121/// # assert_eq!(example, b"hello world");
122/// // To given `Vec<u8>`
123/// # let mut example = Vec::new();
124/// b64_decode!(URL_SAFE_NO_PAD: "aGVsbG8gd29ybGQ", &mut example);
125/// # assert_eq!(&example, b"hello world");
126/// // Though not recommended, we support the full path as well.
127/// # let example =
128/// b64_decode!(general_purpose::URL_SAFE_NO_PAD, "aGVsbG8gd29ybGQ")
129/// # .unwrap();
130/// # assert_eq!(example, b"hello world");
131/// ```
132macro_rules! b64_decode {
133    ($data:expr) => {
134        $crate::b64_decode!(STANDARD: $data)
135    };
136    (STANDARD: $($tt:tt)+) => {
137        $crate::b64_decode!($crate::base64::engine::general_purpose::STANDARD, $($tt)+)
138    };
139    (STANDARD_NO_PAD: $($tt:tt)+) => {
140        $crate::b64_decode!($crate::base64::engine::general_purpose::STANDARD_NO_PAD, $($tt)+)
141    };
142    (URL_SAFE: $($tt:tt)+) => {
143        $crate::b64_decode!($crate::base64::engine::general_purpose::URL_SAFE, $($tt)+)
144    };
145    (URL_SAFE_NO_PAD: $($tt:tt)+) => {
146        $crate::b64_decode!($crate::base64::engine::general_purpose::URL_SAFE_NO_PAD, $($tt)+)
147    };
148    ($padding:path, $data:expr) => {
149        $crate::base64::Engine::decode(&$padding, $data)
150    };
151    ($padding:path, $data:expr, $buf:expr) => {
152        $crate::base64::Engine::decode_vec(&$padding, $data, $buf)
153    };
154}
155
156// ============ Deprecated ================
157
158#[deprecated(since = "0.8.0-rc.5", note = "use `b64_encode!` instead")]
159#[macro_export]
160/// Base64 encode with [`bytes::Bytes`] returned
161macro_rules! b64_encode_bytes {
162    ($data:expr) => {
163        $crate::b64_encode_bytes!($data, $crate::base64::engine::general_purpose::STANDARD)
164    };
165    ($data:expr, $padding:path) => {{
166        let data = $data.as_ref();
167        let target_len = data.len() * 4 / 3 + 4;
168        let mut bytes_buf = bytes::BytesMut::with_capacity(target_len + 64);
169        #[allow(unsafe_code)]
170        // Safety: `target_len` is the exact length of the base64 string.
171        unsafe {
172            bytes_buf.set_len(target_len)
173        };
174        let bytes_written =
175            $crate::base64::Engine::encode_slice(&$padding, $data, bytes_buf.as_mut()).unwrap_or(0);
176        bytes_buf.truncate(bytes_written);
177        bytes_buf.freeze()
178    }};
179}