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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use core::fmt;
use serde::Deserialize;
use crate::deserialize::{XmlLink, XmlName, XmlSignedValue, XmlStringValue};
use crate::{
Game, GameArtist, GameDesigner, GamePublisher, MarketplaceListing, NameType, RatingCommentPage,
XmlMarketplaceListings,
};
// A struct containing the list of requested accessories with the full details.
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct Accessories {
// List of accessories.
#[serde(default, rename = "item")]
pub(crate) accessories: Vec<AccessoryDetails>,
}
/// A game, or expansion, with full details.
///
/// A game returned from the game endpoint which includes all details about a game.
/// This includes the name, description, image and thumbnail. As well as lists of all
/// the alternate names, expansions, artists and publishers.
///
/// Some information, such as version info, comments, and marketplace data is only
/// optionally included if requested.
#[derive(Clone, Debug, PartialEq)]
pub struct AccessoryDetails {
/// The ID of the accessory.
pub id: u64,
/// The name of the accessory.
pub name: String,
/// A list of alternate names for the accessory, usually translations of the primary name.
pub alternate_names: Vec<String>,
/// A brief description of the accessory.
pub description: String,
/// A link to a jpg image for the accessory. Can by empty.
pub image: Option<String>,
/// A link to a jpg thumbnail image for the accessory. Can by empty.
pub thumbnail: Option<String>,
/// The year the accessory was first published.
pub year_published: i64,
/// A list of games that this is an accessory for.
pub accessory_for: Vec<Game>,
/// The designer of this accessory.
pub designers: Vec<GameDesigner>,
/// A list of artists for this game.
pub artists: Vec<GameArtist>,
/// The list of publishers for this accessory.
pub publishers: Vec<GamePublisher>,
/// Information for the various versions of the accessory.
pub versions: Vec<AccessoryVersion>,
/// Information of where to buy the accessory and for how much.
pub marketplace_listings: Vec<MarketplaceListing>,
/// List of comments and ratings users have given to the accessory.
///
/// Each comment may have a rating but no comment, a comment but no rating, or both. However
/// the underlying API will only return all comments, whether or not they have ratings,
/// with the `include_comments` query parameter. Or all ratings, whether or not they have
/// comments. However the same tag is used to return them so there is no way to return all
/// comments and ratings together.
///
/// Page number and page size can be controlled via query parameters.
pub rating_comments: Option<RatingCommentPage>,
}
// Intermediary struct representing the list of versions in XML, so we can extract just a vector to
// return on the accessory details type.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub(crate) struct XmlAccessoryVersions {
#[serde(rename = "item")]
pub(crate) versions: Vec<AccessoryVersion>,
}
/// Information about a version of this accessory
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct AccessoryVersion {
/// The ID of this accessory.
pub id: u64,
/// The name of the accessory.
#[serde(
deserialize_with = "deserialize_accessory_version_name",
rename = "canonicalname"
)]
pub name: String,
/// A link to a jpg image for the accessory.
pub image: Option<String>,
/// A link to a jpg thumbnail image for the accessory.
pub thumbnail: Option<String>,
}
fn deserialize_accessory_version_name<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let name_value_xml = XmlStringValue::deserialize(deserializer)?;
Ok(name_value_xml.value)
}
impl<'de> Deserialize<'de> for AccessoryDetails {
fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum Field {
Id,
Type,
Thumbnail,
Image,
Name,
Description,
YearPublished,
Link,
Versions,
MarketPlaceListings,
Comments,
}
struct AccessoryDetailsVisitor;
impl<'de> serde::de::Visitor<'de> for AccessoryDetailsVisitor {
type Value = AccessoryDetails;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an XML object for a board game accessory returned by the `thing` endpoint from boardgamegeek")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: serde::de::MapAccess<'de>,
{
let mut id = None;
let mut thumbnail = None;
let mut image = None;
let mut name = None;
let mut alternate_names = vec![];
let mut description = None;
let mut year_published = None;
// Link tags
let mut accessory_for = vec![];
let mut designers = vec![];
let mut artists = vec![];
let mut publishers = vec![];
// Other
let mut versions = None;
let mut marketplace_listings = None;
let mut rating_comments = None;
while let Some(key) = map.next_key()? {
match key {
Field::Id => {
if id.is_some() {
return Err(serde::de::Error::duplicate_field("id"));
}
id = Some(map.next_value()?);
},
Field::Type => {
// Ignore, it's always the same thing
let _: String = map.next_value()?;
},
Field::Thumbnail => {
if thumbnail.is_some() {
return Err(serde::de::Error::duplicate_field("thumbnail"));
}
thumbnail = Some(map.next_value()?);
},
Field::Image => {
if image.is_some() {
return Err(serde::de::Error::duplicate_field("image"));
}
image = Some(map.next_value()?);
},
Field::Name => {
let name_xml: XmlName = map.next_value()?;
match name_xml.name_type {
NameType::Primary => {
if name.is_some() {
return Err(serde::de::Error::duplicate_field(
"name type=\"primary\"",
));
}
name = Some(name_xml.value);
},
NameType::Alternate => {
alternate_names.push(name_xml.value);
},
}
},
Field::Description => {
if description.is_some() {
return Err(serde::de::Error::duplicate_field("description"));
}
description = Some(map.next_value()?);
},
Field::YearPublished => {
if year_published.is_some() {
return Err(serde::de::Error::duplicate_field("yearpublished"));
}
let year_published_xml: XmlSignedValue = map.next_value()?;
year_published = Some(year_published_xml.value);
},
Field::Link => {
let link: XmlLink = map.next_value()?;
match link.link_type {
crate::ItemType::BoardGameAccessory => {
// The type "boardgameaccessory" with "inbound=true" is used to
// list games that this is an accessory for.
accessory_for.push(Game {
id: link.id,
name: link.value,
});
},
crate::ItemType::BoardGameDesigner => {
designers.push(GameDesigner {
id: link.id,
name: link.value,
});
},
crate::ItemType::BoardGameArtist => {
artists.push(GameArtist {
id: link.id,
name: link.value,
});
},
crate::ItemType::BoardGamePublisher => {
publishers.push(GamePublisher {
id: link.id,
name: link.value,
});
},
link_type => {
return Err(serde::de::Error::custom(format!(
"found unexpected \"{link_type:?}\" link in game info",
)));
},
}
},
Field::Versions => {
if versions.is_some() {
return Err(serde::de::Error::duplicate_field("versions"));
}
let versions_xml: XmlAccessoryVersions = map.next_value()?;
versions = Some(versions_xml.versions);
},
Field::MarketPlaceListings => {
if marketplace_listings.is_some() {
return Err(serde::de::Error::duplicate_field(
"marketplacelistings",
));
}
let marketplace_listings_xml: XmlMarketplaceListings =
map.next_value()?;
marketplace_listings = Some(marketplace_listings_xml.listings);
},
Field::Comments => {
if rating_comments.is_some() {
return Err(serde::de::Error::duplicate_field("comments"));
}
rating_comments = Some(map.next_value()?);
},
}
}
let id = id.ok_or_else(|| serde::de::Error::missing_field("id"))?;
let thumbnail =
thumbnail.ok_or_else(|| serde::de::Error::missing_field("thumbnail"))?;
let image = image.ok_or_else(|| serde::de::Error::missing_field("image"))?;
let name = name.ok_or_else(|| serde::de::Error::missing_field("name"))?;
let description =
description.ok_or_else(|| serde::de::Error::missing_field("description"))?;
let year_published = year_published
.ok_or_else(|| serde::de::Error::missing_field("yearpublished"))?;
let versions = versions.unwrap_or_default();
let marketplace_listings = marketplace_listings.unwrap_or_default();
Ok(Self::Value {
id,
name,
alternate_names,
description,
image,
thumbnail,
year_published,
accessory_for,
designers,
artists,
publishers,
versions,
marketplace_listings,
rating_comments,
})
}
}
deserializer.deserialize_any(AccessoryDetailsVisitor)
}
}