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
|
use std::convert::Infallible;
use std::error::Error;
use std::fmt::Debug;
use xml_stinks::deserializer::buffered::Buffered as BufferedDeserializer;
use xml_stinks::deserializer::{Deserializer, Error as DeserializerError, IgnoreEnd};
use xml_stinks::tagged::TagStart;
use xml_stinks::DeserializeTagged;
fn main() -> Result<(), Box<dyn Error>>
{
let mut deserializer = BufferedDeserializer::new(
concat!("<food><name>Banana</name><kind>Fruit</kind></food>",).as_bytes(),
);
let food = deserializer.de_tag::<Food>("food", IgnoreEnd::No)?;
println!("Food {} with kind {:?}", food.name, food.kind);
Ok(())
}
#[derive(Debug)]
struct Food
{
name: String,
kind: FoodKind,
}
impl DeserializeTagged for Food
{
type Error = FoodError;
fn deserialize<TDeserializer: Deserializer>(
_start: &TagStart,
deserializer: &mut TDeserializer,
) -> Result<Self, Self::Error>
{
let name =
deserializer.de_tag_with("name", IgnoreEnd::No, |_, deserializer| {
deserializer.de_text()
})?;
let kind =
deserializer.de_tag_with("kind", IgnoreEnd::No, |_, deserializer| {
match deserializer.de_text()?.as_str() {
"Fruit" => Ok(FoodKind::Fruit),
"Vegetable" => Ok(FoodKind::Vegetable),
unknown_kind => {
Err(FoodError::UnknownFoodKind(unknown_kind.to_string()))
}
}
})?;
Ok(Self { name, kind })
}
}
#[derive(Debug)]
enum FoodKind
{
Fruit,
Vegetable,
}
#[derive(Debug, thiserror::Error)]
enum FoodError
{
#[error("Unknown food kind '{0}'")]
UnknownFoodKind(String),
#[error(transparent)]
DeserializeFailed(#[from] DeserializerError<Infallible>),
}
impl<DeError: Into<Self>> From<DeserializerError<DeError>> for FoodError
{
fn from(err: DeserializerError<DeError>) -> Self
{
if let DeserializerError::DeserializeFailed(de_err) = err {
return de_err.into();
}
err.into_never_de_err().into()
}
}
#[cfg(test)]
mod tests
{
use mockall::mock;
use mockall::predicate::{always, eq};
use xml_stinks::deserializer::{Error, MaybeStatic};
use super::*;
mock! {
pub Deserializer {}
impl Deserializer for Deserializer
{
fn de_tag<De: DeserializeTagged>(
&mut self,
tag_name: &str,
ignore_end: IgnoreEnd,
) -> Result<De, Error<De::Error>>;
fn de_tag_with<TOutput, Err, Func>(
&mut self,
tag_name: &str,
ignore_end: IgnoreEnd,
deserialize: Func,
) -> Result<TOutput, Error<Err>>
where
TOutput: MaybeStatic,
Err: std::error::Error + Send + Sync + 'static,
Func: FnOnce(&TagStart, &mut MockDeserializer) -> Result<TOutput, Err> + MaybeStatic;
fn de_tag_list<De, TagName>(
&mut self,
tag_name: Option<TagName>
) -> Result<Vec<De>, Error<De::Error>>
where
De: DeserializeTagged,
TagName: AsRef<str> + MaybeStatic;
fn de_text(&mut self) -> Result<String, Error<Infallible>>;
fn skip_to_tag_start(&mut self, tag_name: &str) -> Result<(), Error<Infallible>>;
fn skip_to_tag_end(&mut self, tag_name: &str) -> Result<(), Error<Infallible>>;
}
}
#[test]
fn deserialize_food_works()
{
let mut mock_deserializer = MockDeserializer::new();
mock_deserializer
.expect_de_tag_with::<String, DeserializerError<Infallible>>()
.with(eq("name"), eq(IgnoreEnd::No), always())
.returning(|tag_name, _, func| {
let mut deserializer = MockDeserializer::new();
deserializer
.expect_de_text()
.returning(|| Ok("Carrot".to_string()))
.once();
Ok(func(&TagStart::new(tag_name), &mut deserializer)?)
})
.once();
mock_deserializer
.expect_de_tag_with::<FoodKind, FoodError>()
.with(eq("kind"), eq(IgnoreEnd::No), always())
.returning(|tag_name, _, func| {
let mut deserializer = MockDeserializer::new();
deserializer
.expect_de_text()
.returning(|| Ok("Vegetable".to_string()))
.once();
Ok(func(&TagStart::new(tag_name), &mut deserializer)?)
})
.once();
let food = Food::deserialize(&TagStart::new("food"), &mut mock_deserializer)
.expect("Expected Ok");
assert_eq!(food.name, "Carrot");
assert!(matches!(food.kind, FoodKind::Vegetable));
}
}
|