arnak 0.6.0

A Rust library for the BoardGameGeek XML API.
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use core::fmt::{self, Display};

use serde::Deserialize;

use super::{RankValue, RatingValue};
use crate::deserialize::{XmlFloatValue, XmlLink, XmlName, XmlSignedValue, XmlStringValue};

/// The type of the item. Either a board game, a board game expansion, or board game accessory.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ItemType {
    /// A board game. In many cases the underlying API will also include
    /// board game expansions under this type, unless explicitly excluded.
    BoardGame,
    /// A board game expansion.
    BoardGameExpansion,
    /// An accessory for a board game. This can include things such as playmats
    /// and miniatures.
    BoardGameAccessory,
    /// A designer of a board game.
    BoardGameDesigner,
    /// A publisher of a board game.
    BoardGamePublisher,
    /// An artist of a board game.
    BoardGameArtist,
    /// A group of games. A family of games might be all games that fall under a certain
    /// IP, or grouped by some other criteria such as game mechanic.
    BoardGameFamily,
    /// A category for a game.
    ///
    /// A category is a broad description mostly based on the theme of the game not the mechanics.
    /// Includes `Fantasy`, `Adventure`, `Animals`, and some mechanic based categories such as
    /// `Action / Dexterity`, and `Dice`.
    BoardGameCategory,
    /// A mechanic for a game.
    ///
    /// Mechanics can include `Worker Placement`, `Push Your Luck`, and `Negotiation`.
    BoardGameMechanic,
    /// A different edition of an existing game.
    BoardGameCompilation,
    /// A different implementation of an existing game.
    BoardGameImplementation,
    /// A different version of a game.
    ///
    /// Type used only in the version info for a game, and typically means translated versions
    /// of the game.
    BoardGameVersion,
    /// A language that a game supports.
    Language,
}

impl Display for ItemType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ItemType::BoardGame => write!(f, "boardgame"),
            ItemType::BoardGameExpansion => write!(f, "boardgameexpansion"),
            ItemType::BoardGameAccessory => write!(f, "boardgameaccessory"),
            ItemType::BoardGameDesigner => write!(f, "boardgamedesigner"),
            ItemType::BoardGamePublisher => write!(f, "boardgamepublisher"),
            ItemType::BoardGameArtist => write!(f, "boardgameartist"),
            ItemType::BoardGameFamily => write!(f, "boardgamefamily"),
            ItemType::BoardGameCategory => write!(f, "boardgamecategory"),
            ItemType::BoardGameMechanic => write!(f, "boardgamemechanic"),
            ItemType::BoardGameCompilation => write!(f, "boardgamecompilation"),
            ItemType::BoardGameImplementation => write!(f, "boardgameimplementation"),
            ItemType::BoardGameVersion => write!(f, "boardgameversion"),
            ItemType::Language => write!(f, "language"),
        }
    }
}

/// The type of an item that can be returned from the collections endpoint.
/// Either a board game, a board game expansion, or board game accessory, a subset ot
/// [`ItemType`].
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum CollectionItemType {
    /// A board game. In many cases the underlying API will also include
    /// board game expansions under this type, unless explicitly excluded.
    BoardGame,
    /// A board game expansion.
    BoardGameExpansion,
    /// An accessory for a board game. This can include things such as playmats
    /// and miniatures.
    BoardGameAccessory,
}

impl Display for CollectionItemType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CollectionItemType::BoardGame => write!(f, "boardgame"),
            CollectionItemType::BoardGameExpansion => write!(f, "boardgameexpansion"),
            CollectionItemType::BoardGameAccessory => write!(f, "boardgameaccessory"),
        }
    }
}

/// The type of a game.
///
/// Either [`GameType::BoardGame`] for a normal board game or [`GameType::BoardGameExpansion`]
/// for an expansion of another existing board game.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum GameType {
    /// A board game. In many cases the underlying API will also include
    /// board game expansions under this type, unless explicitly excluded.
    BoardGame,
    /// A board game expansion.
    BoardGameExpansion,
}

impl Display for GameType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GameType::BoardGame => write!(f, "boardgame"),
            GameType::BoardGameExpansion => write!(f, "boardgameexpansion"),
        }
    }
}

/// The type of game, board game or expansion.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum NameType {
    /// The primary name for a game or game family.
    Primary,
    /// An alternate name for a game or game family. Often a translation or name in a different
    /// locale.
    Alternate,
}

/// A game with minimal information, only the name and ID.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct Game {
    /// The ID of the game.
    pub id: u64,
    /// The name of the game.
    #[serde(rename = "value")]
    pub name: String,
}

/// A game accessory with minimal information, only the name and ID.
///
/// More information can be retrieved from the accessory endpoint.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameAccessory {
    /// The ID of the game.
    pub id: u64,
    /// The name of the game.
    #[serde(rename = "value")]
    pub name: String,
}

/// A game accessory with minimal information, only the name and ID.
///
/// More information can be retrieved from the accessory endpoint.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameCategory {
    /// The ID of the game.
    pub id: u64,
    /// The name of the game.
    #[serde(rename = "value")]
    pub name: String,
}

/// A game accessory with minimal information, only the name and ID.
///
/// More information can be retrieved from the accessory endpoint.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameMechanic {
    /// The ID of the game.
    pub id: u64,
    /// The name of the game.
    #[serde(rename = "value")]
    pub name: String,
}

/// A name and ID of a game family.
///
/// More information about the game family can be retrieved from the
/// game family endpoint. This will include a description and a list
/// of all games that belong to this game family.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameFamilyName {
    /// The ID of the publisher.
    pub id: u64,
    /// The name of the publisher.
    #[serde(rename = "value")]
    pub name: String,
}

/// A different edition or compilation of a game.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameCompilation {
    /// The ID of the compilation.
    pub id: u64,
    /// The name of the compilation.
    #[serde(rename = "value")]
    pub name: String,
}

/// A re-implementation of a game.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameImplementation {
    /// The ID of the implementation.
    pub id: u64,
    /// The name of the implementation.
    #[serde(rename = "value")]
    pub name: String,
}

/// A designer of a game.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameDesigner {
    /// The ID of the designer.
    pub id: u64,
    /// The name of the designer.
    #[serde(rename = "value")]
    pub name: String,
}

/// A publisher of a game.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GamePublisher {
    /// The ID of the publisher.
    pub id: u64,
    /// The name of the publisher.
    #[serde(rename = "value")]
    pub name: String,
}

/// An artist for a game.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct GameArtist {
    /// The ID of the artist.
    pub id: u64,
    /// The name of the game.
    #[serde(rename = "value")]
    pub name: String,
}

/// A language, listed on versions of games that may support
/// one or more languages.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct Language {
    /// The ID of the language.
    pub id: u64,
    /// The name of the language, in English.
    #[serde(rename = "value")]
    pub name: String,
}

/// The dimensions of a game, in inches.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct Dimensions {
    /// The width of the game, in inches.
    pub width: f64,
    /// The length of the game, in inches.
    pub length: f64,
    /// The depth of the game, in inches.
    pub depth: f64,
}

/// A struct containing the game's rank within a particular type of game.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct ItemFamilyRank {
    /// ID of the game family.
    pub id: u64,
    /// Name of the game type. "boardgame" used as the generic subtype that
    /// includes all board games.
    pub name: String,
    /// User friendly name in the format "GENRE game rank" e.g. "Party Game
    /// Rank".
    #[serde(rename = "friendlyname")]
    pub friendly_name: String,
    /// The overall rank on the site within this type of game.
    pub value: RankValue,
    /// The score out of 10, as a bayesian average.
    ///
    /// This is what boardgamegeek calls a Geek Rating. It is the average rating
    /// that the users have given it along with a few thousand 5.5 ratings added
    /// in too.
    #[serde(rename = "bayesaverage")]
    pub bayesian_average: RatingValue,
}

// A user's collection on boardgamegeek.
#[derive(Clone, Debug, Deserialize, PartialEq)]
pub(crate) struct XmlVersions {
    // List of versions, each in an XML tag called `item`, within an outer
    // `version`. We use this intermediary type to get out just the first,
    // since we only expect 1.
    #[serde(rename = "item")]
    pub(crate) versions: Vec<GameVersion>,
}

/// Information about a game which is a version or re-implementation of another game, including the
/// link to the original.
///
/// Often this may be a translated version of a game. It is not the same as an expansion for a game.
#[derive(Clone, Debug, PartialEq)]
pub struct GameVersion {
    /// The ID of this game.
    pub id: u64,
    /// The name of the game.
    pub name: String,
    /// A list of alternate names for the game.
    pub alternate_names: Vec<String>,
    /// The year the game was first published.
    pub year_published: i64,
    /// A link to a jpg image for the game.
    pub image: Option<String>,
    /// A link to a jpg thumbnail image for the game.
    pub thumbnail: Option<String>,
    /// The name and ID of the game this version is based off of.
    pub original_game: Game,
    /// List of publishers for this game.
    pub publishers: Vec<GamePublisher>,
    /// List of game artists.
    pub artists: Vec<GameArtist>,
    /// Lists of languages that this version of the game supports.
    pub languages: Vec<Language>,
    /// The dimensions of the game, in inches, if included.
    pub dimensions: Option<Dimensions>,
    /// The weight of the game, in pounds, if included.
    pub weight: Option<f64>,
    /// Product code for the game, if included.
    pub product_code: Option<String>,
}

impl<'de> Deserialize<'de> for GameVersion {
    fn deserialize<D: serde::de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field {
            Id,
            Name,
            Image,
            Thumbnail,
            YearPublished,
            ProductCode,
            Width,
            Length,
            Depth,
            Weight,
            // Game original version, publisher, artist, are each in an individual XML tag called
            // `link`
            Link,
            Type,
        }

        struct GameVersionVisitor;

        impl<'de> serde::de::Visitor<'de> for GameVersionVisitor {
            type Value = GameVersion;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a string containing the XML for game version information.")
            }

            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 name = None;
                let mut alternate_names = vec![];
                let mut publishers = vec![];
                let mut artists = vec![];
                let mut languages = vec![];
                let mut year_published = None;
                let mut image = None;
                let mut thumbnail = None;
                let mut original_game = None;
                let mut width = None;
                let mut length = None;
                let mut depth = None;
                let mut weight = None;
                let mut product_code = 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::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::Image => {
                            if image.is_some() {
                                return Err(serde::de::Error::duplicate_field("image"));
                            }
                            image = Some(map.next_value()?);
                        },
                        Field::Thumbnail => {
                            if thumbnail.is_some() {
                                return Err(serde::de::Error::duplicate_field("thumbnail"));
                            }
                            thumbnail = Some(map.next_value()?);
                        },
                        Field::Link => {
                            let link: XmlLink = map.next_value()?;
                            match link.link_type {
                                ItemType::BoardGameVersion => {
                                    if original_game.is_some() {
                                        return Err(serde::de::Error::duplicate_field(
                                            "link with type \"boardgameversion\"",
                                        ));
                                    }
                                    original_game = Some(Game {
                                        id: link.id,
                                        name: link.value,
                                    });
                                },
                                ItemType::BoardGamePublisher => {
                                    publishers.push(GamePublisher {
                                        id: link.id,
                                        name: link.value,
                                    });
                                },
                                ItemType::BoardGameArtist => {
                                    artists.push(GameArtist {
                                        id: link.id,
                                        name: link.value,
                                    });
                                },
                                ItemType::Language => {
                                    languages.push(Language {
                                        id: link.id,
                                        name: link.value,
                                    });
                                },
                                link_type => {
                                    return Err(serde::de::Error::custom(format!(
                                        "found unexpected \"{link_type:?}\" link in version",
                                    )));
                                },
                            }
                        },
                        Field::Width => {
                            if width.is_some() {
                                return Err(serde::de::Error::duplicate_field("width"));
                            }
                            let width_xml: XmlFloatValue = map.next_value()?;
                            if width_xml.value == 0.0 {
                                width = Some(None);
                            } else {
                                width = Some(Some(width_xml.value));
                            }
                        },
                        Field::Depth => {
                            if depth.is_some() {
                                return Err(serde::de::Error::duplicate_field("depth"));
                            }
                            let depth_xml: XmlFloatValue = map.next_value()?;
                            if depth_xml.value == 0.0 {
                                depth = Some(None);
                            } else {
                                depth = Some(Some(depth_xml.value));
                            }
                        },
                        Field::Length => {
                            if length.is_some() {
                                return Err(serde::de::Error::duplicate_field("length"));
                            }
                            let length_xml: XmlFloatValue = map.next_value()?;
                            if length_xml.value == 0.0 {
                                length = Some(None);
                            } else {
                                length = Some(Some(length_xml.value));
                            }
                        },
                        Field::Weight => {
                            if weight.is_some() {
                                return Err(serde::de::Error::duplicate_field("weight"));
                            }
                            let weight_xml: XmlFloatValue = map.next_value()?;
                            if weight_xml.value == 0.0 {
                                weight = Some(None);
                            } else {
                                weight = Some(Some(weight_xml.value));
                            }
                        },
                        Field::ProductCode => {
                            if product_code.is_some() {
                                return Err(serde::de::Error::duplicate_field("product_code"));
                            }
                            let product_code_xml: XmlStringValue = map.next_value()?;
                            if product_code_xml.value.is_empty() {
                                product_code = Some(None);
                            } else {
                                product_code = Some(Some(product_code_xml.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::Type => {
                            // Type is fixed at "boardgameversion", even for the list of games
                            // contained so we don't add it. But we need
                            // to consume the value.
                            let _: String = map.next_value()?;
                        },
                    }
                }
                let id = id.ok_or_else(|| serde::de::Error::missing_field("id"))?;
                let name = name.ok_or_else(|| serde::de::Error::missing_field("name"))?;
                let year_published = year_published
                    .ok_or_else(|| serde::de::Error::missing_field("yearpublished"))?;
                let image = image.ok_or_else(|| serde::de::Error::missing_field("image"))?;
                let thumbnail =
                    thumbnail.ok_or_else(|| serde::de::Error::missing_field("thumbnail"))?;
                let original_game = original_game.ok_or_else(|| {
                    serde::de::Error::missing_field("link with type \"boardgameversion\"")
                })?;
                let width = width.ok_or_else(|| serde::de::Error::missing_field("width"))?;
                let length = length.ok_or_else(|| serde::de::Error::missing_field("length"))?;
                let depth = depth.ok_or_else(|| serde::de::Error::missing_field("depth"))?;
                let weight = weight.ok_or_else(|| serde::de::Error::missing_field("weight"))?;
                let product_code =
                    product_code.ok_or_else(|| serde::de::Error::missing_field("productcode"))?;

                let dimensions;
                if let (Some(width), Some(length), Some(depth)) = (width, length, depth) {
                    dimensions = Some(Dimensions {
                        width,
                        length,
                        depth,
                    });
                } else if let (None, None, None) = (width, depth, length) {
                    dimensions = None;
                } else {
                    return Err(serde::de::Error::custom("Invalid game dimensions, some but not all of width, length, depth, were set."));
                }

                Ok(Self::Value {
                    id,
                    name,
                    alternate_names,
                    year_published,
                    image,
                    thumbnail,
                    original_game,
                    publishers,
                    artists,
                    languages,
                    dimensions,
                    weight,
                    product_code,
                })
            }
        }
        deserializer.deserialize_any(GameVersionVisitor)
    }
}

/// A user's username and ID.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct User {
    /// ID for the user.
    pub user_id: u64,
    /// Username, used to request collection information.
    pub username: String,
}