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
extern crate base64;
pub use base64::{Config, MIME, STANDARD, STANDARD_NO_PAD, URL_SAFE, URL_SAFE_NO_PAD};
pub struct Base64 {
data: Vec<u8>,
config: Config,
}
impl Base64 {
pub fn new<T: ?Sized + AsRef<[u8]>>(input: &T) -> Self {
Base64::with_config(input, STANDARD)
}
pub fn with_config<T: ?Sized + AsRef<[u8]>>(input: &T, config: Config) -> Self {
let data = base64::encode_config(input, config.clone());
Base64 {
data: data.into_bytes(),
config,
}
}
pub fn decode(&self) -> Vec<u8> {
match base64::decode_config(&self.data, self.config.clone()) {
Ok(vec) => vec,
Err(_) => unreachable!("Decoding CANNOT fail because we only store encoded base64"),
}
}
pub fn config(&self) -> &Config {
&self.config
}
pub fn encoded(&self) -> &Vec<u8> {
&self.data
}
}
#[cfg(test)]
mod tests {
use Base64;
#[test]
fn strings() {
let string = "test string";
let b64 = Base64::new(string);
let decoded = b64.decode();
let result = String::from_utf8_lossy(&decoded[..]);
assert_eq!(result, string);
}
#[test]
fn bytes() {
let bytes = b"test bytes";
let b64 = Base64::new(bytes);
let decoded = b64.decode();
assert_eq!(&decoded[..], bytes);
}
#[test]
fn nums() {
let num = 3525623682623862362.252352242f64;
let stringify = num.to_string();
let b64 = Base64::new(&stringify);
let decoded = b64.decode();
let result = String::from_utf8_lossy(&decoded[..]);
assert_eq!(num, result.parse().unwrap());
}
}