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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use core::fmt;
use chrono::NaiveDate;
use serde::Deserialize;
use crate::deserialize::{XmlDateValue, XmlIntValue, XmlSignedValue, XmlStringValue};
/// A user's information.
#[derive(Clone, Debug, PartialEq)]
pub struct User {
/// The ID of the user.
pub id: u64,
/// The username of the user.
pub username: String,
/// The first name of the user.
pub first_name: String,
/// The last name of the user.
pub last_name: String,
/// A link to the user's avatar.
pub avatar_link: Option<String>,
/// The year that the user registered on `BoardGameGeek`.
pub year_registered: i64,
/// The date that the user last logged in.
pub last_login: NaiveDate,
/// State or province that the user lives in.
pub state_or_province: Option<String>,
/// Country the user lives in.
pub country: Option<String>,
/// User's website.
pub web_address: Option<String>,
/// User's XBOX gamertag.
pub xbox_account: Option<String>,
/// User's Wii account username.
pub wii_account: Option<String>,
/// User's PSN username.
pub psn_account: Option<String>,
/// User's battle.net username.
pub battlenet_account: Option<String>,
/// User's steam username.
pub steam_account: Option<String>,
/// A rating, starting a 0, gained by performing board game trades with other users.
pub trade_rating: u64,
/// The user's top 10 list.
pub top_list: Vec<ListItem>,
/// The user's hot 10 list.
pub hot_list: Vec<ListItem>,
/// A list of the users that this user is buddies with.
pub buddies: BuddyList,
/// A list of the guilds that this user belongs to.
pub guilds: GuildList,
}
#[derive(Clone, Debug, Deserialize, PartialEq)]
struct ItemList {
#[serde(default, rename = "item")]
pub(crate) items: Vec<ListItem>,
}
/// An item in a user made top 10 or hot 10 list. A brief representation of an item which
/// contains a name type and rank.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct ListItem {
/// The ID of this item.
pub id: u64,
/// The name of the game or person. It can also be a name of a mechanic such as worker
/// placement.
pub name: String,
/// A number 1 through 10, with 1 being top of the list.
pub rank: u64,
/// The type of item, which may be a game, person, event.
#[serde(rename = "type")]
pub item_type: ListItemType,
}
/// A type of item in a user's top 10, or hot 10 list on their profile.
/// Note that when choosing items on the website
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ListItemType {
/// A board game, board game expansion, or board game accessory.
Thing,
/// A person who is a board game designer, publisher, or artist.
Person,
/// A company designers, or publishes, games.
Company,
/// A certain family of games, can be a certain category, a certain component, or theme. Can
/// also be an accessory family.
Family,
/// A mechanic or category for a game. Mechanics include dice rolling, worker placement, and
/// cooperative game. Categories include word game, and party game.
Property,
/// A board game event or convention.
Event,
}
/// A single page of guilds the user belongs to, also includes the total number of guilds the
/// user belongs to.
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct GuildList {
/// The total number of guilds this user belongs to.
pub total: u64,
/// The page number for the guilds in the list.
pub page: u64,
/// The list of guilds.
#[serde(default, rename = "guild")]
pub guilds: Vec<GuildBrief>,
}
/// A guild a user belongs to, including only the name and ID.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct GuildBrief {
/// The ID of the guild.
pub id: u64,
/// The name of the guild that the user belongs to.
pub name: String,
}
/// A single page of buddies that the users has, also includes the total number of buddies
/// the user has.
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct BuddyList {
/// The total number of buddies this user has.
pub total: u64,
/// The page number for the buddies in the list.
pub page: u64,
/// The list of buddies.
#[serde(default, rename = "buddy")]
pub buddies: Vec<Buddy>,
}
/// A user that this user is buddies with.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct Buddy {
/// The ID of the user.
pub id: u64,
/// The user's username.
pub name: String,
}
impl<'de> Deserialize<'de> for User {
fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum Field {
TermsOfUse,
Id,
Name,
FirstName,
LastName,
AvatarLink,
YearRegistered,
LastLogin,
StateOrProvince,
Country,
WebAddress,
XboxAccount,
WiiAccount,
PsnAccount,
BattlenetAccount,
SteamAccount,
TradeRating,
Top,
Hot,
Guilds,
Buddies,
}
struct UserVisitor;
impl<'de> serde::de::Visitor<'de> for UserVisitor {
type Value = User;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string containing the XML for a user.")
}
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 username = None;
let mut first_name = None;
let mut last_name = None;
let mut avatar_link = None;
let mut year_registered = None;
let mut last_login = None;
let mut state_or_province = None;
let mut country = None;
let mut web_address = None;
let mut xbox_account = None;
let mut wii_account = None;
let mut psn_account = None;
let mut battlenet_account = None;
let mut steam_account = None;
let mut trade_rating = None;
let mut top_list = None;
let mut hot_list = None;
let mut guild_list = None;
let mut buddy_list = None;
while let Some(key) = map.next_key()? {
match key {
Field::TermsOfUse => {
// Ignore
let _: String = map.next_value()?;
},
Field::Id => {
if id.is_some() {
return Err(serde::de::Error::duplicate_field("id"));
}
id = Some(map.next_value()?);
},
Field::Name => {
if username.is_some() {
return Err(serde::de::Error::duplicate_field("name"));
}
username = Some(map.next_value()?);
},
Field::FirstName => {
if first_name.is_some() {
return Err(serde::de::Error::duplicate_field("firstname"));
}
let first_name_xml: XmlStringValue = map.next_value()?;
first_name = Some(first_name_xml.value);
},
Field::LastName => {
if last_name.is_some() {
return Err(serde::de::Error::duplicate_field("lastname"));
}
let last_name_xml: XmlStringValue = map.next_value()?;
last_name = Some(last_name_xml.value);
},
Field::AvatarLink => {
if avatar_link.is_some() {
return Err(serde::de::Error::duplicate_field("avatarlink"));
}
let avatar_link_xml: XmlStringValue = map.next_value()?;
avatar_link = if avatar_link_xml.value == "N/A" {
None
} else {
Some(avatar_link_xml.value)
}
},
Field::YearRegistered => {
if year_registered.is_some() {
return Err(serde::de::Error::duplicate_field("yearregistered"));
}
let year_registered_xml: XmlSignedValue = map.next_value()?;
year_registered = Some(year_registered_xml.value);
},
Field::LastLogin => {
if last_login.is_some() {
return Err(serde::de::Error::duplicate_field("lastlogin"));
}
let last_login_xml: XmlDateValue = map.next_value()?;
last_login = Some(last_login_xml.value);
},
Field::StateOrProvince => {
if state_or_province.is_some() {
return Err(serde::de::Error::duplicate_field("stateorprovince"));
}
let state_or_province_xml: XmlStringValue = map.next_value()?;
state_or_province = Some(state_or_province_xml.value);
},
Field::Country => {
if country.is_some() {
return Err(serde::de::Error::duplicate_field("country"));
}
let country_xml: XmlStringValue = map.next_value()?;
country = Some(country_xml.value);
},
Field::WebAddress => {
if web_address.is_some() {
return Err(serde::de::Error::duplicate_field("webaddress"));
}
let web_address_xml: XmlStringValue = map.next_value()?;
web_address = Some(web_address_xml.value);
},
Field::XboxAccount => {
if xbox_account.is_some() {
return Err(serde::de::Error::duplicate_field("xboxaccount"));
}
let xbox_account_xml: XmlStringValue = map.next_value()?;
xbox_account = Some(xbox_account_xml.value);
},
Field::WiiAccount => {
if wii_account.is_some() {
return Err(serde::de::Error::duplicate_field("wiiaccount"));
}
let wii_account_xml: XmlStringValue = map.next_value()?;
wii_account = Some(wii_account_xml.value);
},
Field::PsnAccount => {
if psn_account.is_some() {
return Err(serde::de::Error::duplicate_field("psnaccount"));
}
let psn_account_xml: XmlStringValue = map.next_value()?;
psn_account = Some(psn_account_xml.value);
},
Field::BattlenetAccount => {
if battlenet_account.is_some() {
return Err(serde::de::Error::duplicate_field("battlenetaccount"));
}
let battlenet_account_xml: XmlStringValue = map.next_value()?;
battlenet_account = Some(battlenet_account_xml.value);
},
Field::SteamAccount => {
if steam_account.is_some() {
return Err(serde::de::Error::duplicate_field("steamaccount"));
}
let steam_account_xml: XmlStringValue = map.next_value()?;
steam_account = Some(steam_account_xml.value);
},
Field::TradeRating => {
if trade_rating.is_some() {
return Err(serde::de::Error::duplicate_field("traderating"));
}
let trade_rating_xml: XmlIntValue = map.next_value()?;
trade_rating = Some(trade_rating_xml.value);
},
Field::Top => {
if top_list.is_some() {
return Err(serde::de::Error::duplicate_field("top"));
}
let top_list_xml: ItemList = map.next_value()?;
top_list = Some(top_list_xml.items);
},
Field::Hot => {
if hot_list.is_some() {
return Err(serde::de::Error::duplicate_field("hot"));
}
let hot_list_xml: ItemList = map.next_value()?;
hot_list = Some(hot_list_xml.items);
},
Field::Guilds => {
if guild_list.is_some() {
return Err(serde::de::Error::duplicate_field("guilds"));
}
guild_list = Some(map.next_value()?);
},
Field::Buddies => {
if buddy_list.is_some() {
return Err(serde::de::Error::duplicate_field("buddies"));
}
buddy_list = Some(map.next_value()?);
},
}
}
let id = id.ok_or_else(|| serde::de::Error::missing_field("id"))?;
let username = username.ok_or_else(|| serde::de::Error::missing_field("name"))?;
let first_name =
first_name.ok_or_else(|| serde::de::Error::missing_field("firstname"))?;
let last_name =
last_name.ok_or_else(|| serde::de::Error::missing_field("lastname"))?;
let year_registered = year_registered
.ok_or_else(|| serde::de::Error::missing_field("yearregistered"))?;
let last_login =
last_login.ok_or_else(|| serde::de::Error::missing_field("lastlogin"))?;
let trade_rating =
trade_rating.ok_or_else(|| serde::de::Error::missing_field("traderating"))?;
// Even if set to blank, empty string still included in response, so we set these to
// none in this case
if state_or_province.as_ref().is_some_and(String::is_empty) {
state_or_province = None;
}
if country.as_ref().is_some_and(String::is_empty) {
country = None;
}
if web_address.as_ref().is_some_and(String::is_empty) {
web_address = None;
}
if xbox_account.as_ref().is_some_and(String::is_empty) {
xbox_account = None;
}
if psn_account.as_ref().is_some_and(String::is_empty) {
psn_account = None;
}
if wii_account.as_ref().is_some_and(String::is_empty) {
wii_account = None;
}
if battlenet_account.as_ref().is_some_and(String::is_empty) {
battlenet_account = None;
}
if steam_account.as_ref().is_some_and(String::is_empty) {
steam_account = None;
}
Ok(Self::Value {
id,
username,
first_name,
last_name,
avatar_link,
year_registered,
last_login,
state_or_province,
country,
web_address,
xbox_account,
wii_account,
psn_account,
battlenet_account,
steam_account,
trade_rating,
top_list: top_list.unwrap_or(vec![]),
hot_list: hot_list.unwrap_or(vec![]),
buddies: buddy_list.unwrap_or(BuddyList::default()),
guilds: guild_list.unwrap_or(GuildList::default()),
})
}
}
deserializer.deserialize_any(UserVisitor)
}
}