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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use encoding::Encoding;
use java_properties::LineContent::{Comment, KVPair};
use java_properties::PropertiesIter;
use serde::de::{self, IntoDeserializer, MapAccess, Visitor};
use serde::forward_to_deserialize_any;
use std::fmt;
use std::io;
use std::num::{ParseFloatError, ParseIntError};
use std::str::ParseBoolError;
use crate::UTF8_ENCODING;
mod field;
pub struct Deserializer<R: io::Read> {
inner: PropertiesIter<R>,
}
impl<R: io::Read> Deserializer<R> {
pub fn from_reader(reader: R) -> Self {
Self {
inner: PropertiesIter::new(reader),
}
}
pub fn from_reader_with_encoding(reader: R, encoding: &'static dyn Encoding) -> Self {
Self {
inner: PropertiesIter::new_with_encoding(reader, encoding),
}
}
}
impl<'a> Deserializer<io::Cursor<&'a str>> {
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &'a str) -> Self {
Self::from_reader_with_encoding(io::Cursor::new(s), UTF8_ENCODING)
}
}
impl<'a> Deserializer<io::Cursor<&'a [u8]>> {
pub fn from_slice(s: &'a [u8]) -> Self {
Self::from_reader(io::Cursor::new(s))
}
pub fn from_slice_with_encoding(s: &'a [u8], encoding: &'static dyn Encoding) -> Self {
Self::from_reader_with_encoding(io::Cursor::new(s), encoding)
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Custom {
msg: String,
},
Properties(java_properties::PropertiesError),
ParseIntError(ParseIntError),
ParseFloatError(ParseFloatError),
ParseBoolError(ParseBoolError),
NotSupported,
}
impl From<java_properties::PropertiesError> for Error {
fn from(e: java_properties::PropertiesError) -> Self {
Self::Properties(e)
}
}
impl From<ParseIntError> for Error {
fn from(e: ParseIntError) -> Self {
Self::ParseIntError(e)
}
}
impl From<ParseFloatError> for Error {
fn from(e: ParseFloatError) -> Self {
Self::ParseFloatError(e)
}
}
impl From<ParseBoolError> for Error {
fn from(e: ParseBoolError) -> Self {
Self::ParseBoolError(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Custom { msg } => write!(f, "Custom: {:?}", msg),
Self::NotSupported => write!(f, "Not supported"),
Self::Properties(e) => e.fmt(f),
Self::ParseIntError(e) => e.fmt(f),
Self::ParseFloatError(e) => e.fmt(f),
Self::ParseBoolError(e) => e.fmt(f),
}
}
}
impl std::error::Error for Error {}
impl serde::de::Error for Error {
fn custom<T>(msg: T) -> Self
where
T: std::fmt::Display,
{
Self::Custom {
msg: msg.to_string(),
}
}
}
impl<'de, I: io::Read> de::Deserializer<'de> for Deserializer<I> {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: Visitor<'de>,
{
visitor.visit_map(PropertiesMapAccess {
de: self,
line_value: None,
})
}
forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
struct PropertiesMapAccess<I: io::Read> {
de: Deserializer<I>,
line_value: Option<String>,
}
impl<'de, I: io::Read> MapAccess<'de> for PropertiesMapAccess<I> {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
where
K: serde::de::DeserializeSeed<'de>,
{
while let Some(line) = self.de.inner.next().transpose()? {
match line.consume_content() {
Comment(_) => {}
KVPair(key, value) => {
self.line_value = Some(value);
return seed.deserialize(key.into_deserializer()).map(Some);
}
};
}
Ok(None)
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
where
V: serde::de::DeserializeSeed<'de>,
{
let value = self.line_value.take().unwrap();
seed.deserialize(field::FieldDeserializer(value))
}
}
#[cfg(test)]
mod tests {
use serde::Deserialize;
use crate::de::Deserializer;
#[derive(Debug, Clone, PartialEq, Deserialize)]
struct Workload {
recordcount: usize,
operationcount: usize,
workload: String,
readallfields: bool,
readproportion: f32,
updateproportion: f32,
scanproportion: f32,
insertproportion: f32,
requestdistribution: String,
}
#[test]
fn test() {
let data = "
recordcount=1000
operationcount=1000
workload=site.ycsb.workloads.CoreWorkload
readallfields=true
readproportion=0.5
updateproportion=0.5
scanproportion=0
insertproportion=0
requestdistribution=zipfian
";
let deserializer = Deserializer::from_str(data);
let workload_a = Workload::deserialize(deserializer).unwrap();
assert_eq!(
workload_a,
Workload {
recordcount: 1000,
operationcount: 1000,
workload: "site.ycsb.workloads.CoreWorkload".to_string(),
readallfields: true,
readproportion: 0.5,
updateproportion: 0.5,
scanproportion: 0.0,
insertproportion: 0.0,
requestdistribution: "zipfian".to_string(),
}
);
}
}