-
Notifications
You must be signed in to change notification settings - Fork 226
/
bytes.rs
163 lines (142 loc) · 5.23 KB
/
bytes.rs
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
//! Serialize/deserialize bytes (`Vec<u8>`) type
/// Serialize into hexstring, deserialize from hexstring
pub mod hexstring {
use serde::{Deserialize, Deserializer, Serializer};
use subtle_encoding::hex;
use crate::prelude::*;
/// Deserialize a hex-encoded string into `Vec<u8>`
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let string = Option::<String>::deserialize(deserializer)?.unwrap_or_default();
hex::decode_upper(&string)
.or_else(|_| hex::decode(&string))
.map_err(serde::de::Error::custom)
}
/// Serialize from a byte slice into a hex-encoded string.
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
let hex_bytes = hex::encode_upper(value.as_ref());
let hex_string = String::from_utf8(hex_bytes).map_err(serde::ser::Error::custom)?;
serializer.serialize_str(&hex_string)
}
}
/// Serialize into base64string, deserialize from base64string
pub mod base64string {
use serde::{Deserialize, Deserializer, Serializer};
use subtle_encoding::base64;
use crate::prelude::*;
/// Deserialize base64string into `Vec<u8>`
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
Vec<u8>: Into<T>,
{
let s = Option::<String>::deserialize(deserializer)?.unwrap_or_default();
let v = base64::decode(s).map_err(serde::de::Error::custom)?;
Ok(v.into())
}
/// Deserialize base64string into String
pub fn deserialize_to_string<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let s = Option::<String>::deserialize(deserializer)?.unwrap_or_default();
String::from_utf8(base64::decode(s).map_err(serde::de::Error::custom)?)
.map_err(serde::de::Error::custom)
}
/// Serialize from T into base64string
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
let base64_bytes = base64::encode(value.as_ref());
let base64_string = String::from_utf8(base64_bytes).map_err(serde::ser::Error::custom)?;
serializer.serialize_str(&base64_string)
}
}
/// Serialize into and deserialize from a sequence of _base64string_.
pub mod vec_base64string {
use serde::{Deserialize, Deserializer, Serializer};
use subtle_encoding::base64;
use crate::prelude::*;
/// Deserialize array into `Vec<Vec<u8>>`
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Vec<u8>>, D::Error>
where
D: Deserializer<'de>,
{
Option::<Vec<String>>::deserialize(deserializer)?
.unwrap_or_default()
.into_iter()
.map(|s| base64::decode(s).map_err(serde::de::Error::custom))
.collect()
}
/// Serialize from `Vec<T>` into `Vec<base64string>`
pub fn serialize<S, T>(value: &[T], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
let base64_strings = value
.iter()
.map(|v| {
String::from_utf8(base64::encode(v.as_ref())).map_err(serde::ser::Error::custom)
})
.collect::<Result<Vec<String>, S::Error>>()?;
serializer.collect_seq(base64_strings)
}
}
/// Serialize into and deserialize from an optional _base64string_.
pub mod option_base64string {
use serde::{Deserialize, Deserializer, Serializer};
use subtle_encoding::base64;
use crate::prelude::*;
/// Deserialize `Option<base64string>` into `Vec<u8>` or null
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let s = Option::<String>::deserialize(deserializer)?.unwrap_or_default();
base64::decode(s).map_err(serde::de::Error::custom)
}
/// Serialize from `T` into `Option<base64string>`
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
let base64_bytes = base64::encode(value.as_ref());
let base64_string = String::from_utf8(base64_bytes).map_err(serde::ser::Error::custom)?;
serializer.serialize_str(&base64_string)
}
}
/// Serialize into string, deserialize from string
pub mod string {
use serde::{Deserialize, Deserializer, Serializer};
use crate::prelude::*;
/// Deserialize string into `Vec<u8>`
#[allow(dead_code)]
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
let string = Option::<String>::deserialize(deserializer)?.unwrap_or_default();
Ok(string.as_bytes().to_vec())
}
/// Serialize from `T` into string
#[allow(dead_code)]
pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<[u8]>,
{
let string =
String::from_utf8(value.as_ref().to_vec()).map_err(serde::ser::Error::custom)?;
serializer.serialize_str(&string)
}
}