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
extern crate base64;
extern crate serde;
#[doc(hidden)]
pub use base64::{decode_config, encode_config};
#[doc(hidden)]
pub use serde::{de, Deserializer, Serializer};
#[macro_export]
macro_rules! base64_serde_type {
($typename:ident, $config:expr) => {
enum $typename {}
base64_serde_type!(impl_only, $typename, $config);
};
(pub $typename:ident, $config:expr) => {
pub enum $typename {}
base64_serde_type!(impl_only, $typename, $config);
};
(impl_only, $typename:ident, $config:expr) => {
impl $typename {
pub fn serialize<S, Input>(
bytes: Input,
serializer: S,
) -> ::std::result::Result<S::Ok, S::Error>
where
S: $crate::Serializer,
Input: AsRef<[u8]>,
{
serializer.serialize_str(&$crate::encode_config(bytes.as_ref(), $config))
}
pub fn deserialize<'de, D, Output>(
deserializer: D,
) -> ::std::result::Result<Output, D::Error>
where
D: $crate::Deserializer<'de>,
Output: From<Vec<u8>>,
{
struct Base64Visitor;
impl<'de> $crate::de::Visitor<'de> for Base64Visitor {
type Value = Vec<u8>;
fn expecting(
&self,
formatter: &mut ::std::fmt::Formatter,
) -> ::std::fmt::Result {
write!(formatter, "base64 ASCII text")
}
fn visit_str<E>(self, v: &str) -> ::std::result::Result<Self::Value, E>
where
E: $crate::de::Error,
{
$crate::decode_config(v, $config).map_err($crate::de::Error::custom)
}
}
deserializer
.deserialize_str(Base64Visitor)
.map(|vec| Output::from(vec))
}
}
};
}