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
use base64_simd::Base64;
use std::error::Error;
#[derive(Debug)]
pub struct DecodeError(base64_simd::Error);
impl Error for DecodeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.0)
}
}
impl std::fmt::Display for DecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "failed to decode base64")
}
}
pub fn decode(input: impl AsRef<str>) -> Result<Vec<u8>, DecodeError> {
Base64::STANDARD
.decode_to_boxed_bytes(input.as_ref().as_bytes())
.map(|bytes| bytes.into_vec())
.map_err(DecodeError)
}
pub fn encode(input: impl AsRef<[u8]>) -> String {
Base64::STANDARD
.encode_to_boxed_str(input.as_ref())
.into_string()
}
pub fn encoded_length(length: usize) -> usize {
Base64::STANDARD.encoded_length(length)
}