From 383948c6e6975ab38cc9d27ea2d00d1d164efdd2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 30 Jan 2023 00:28:31 +0100 Subject: [PATCH] Implement supertypes enum (#11) --- src/data/setbundle/card.rs | 4 +-- src/data/setbundle/supertype.rs | 43 ++++++++++++++++++++++++++++----- src/telegram/display.rs | 10 +++++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/data/setbundle/card.rs b/src/data/setbundle/card.rs index 2ee77f8..78dac98 100644 --- a/src/data/setbundle/card.rs +++ b/src/data/setbundle/card.rs @@ -142,7 +142,7 @@ pub struct Card { /// The subtypes the card belongs to, such as *Poro* or *Yordle*. pub subtypes: Vec, - /// The supertype the card belongs to, such as *Champion*. + /// The supertype the card belongs to, such as [`Champion`](CardSupertype::Champion) or [`None`](CardSupertype::None). pub supertype: CardSupertype, } @@ -271,7 +271,7 @@ mod tests { localized_flavor_text: String::from("The priestess' pupils were blown wide, and her hand trembled with nervous excitement. She was ready. This was the single moment Evelynn craved more than any other. She grinned, and slowly shed her visage. Then, as always, the screaming began."), artist_name: String::from("Kudos Productions"), subtypes: vec![], - supertype: String::from("Champion"), + supertype: CardSupertype::Champion, } ) } diff --git a/src/data/setbundle/supertype.rs b/src/data/setbundle/supertype.rs index 41c9a2b..d7cc02d 100644 --- a/src/data/setbundle/supertype.rs +++ b/src/data/setbundle/supertype.rs @@ -1,8 +1,39 @@ //! Module defining [CardSupertype]. -/// A supertype of a [Card](super::card::Card), such as *Champion*. -/// -/// Capitalization of the various supertypes is inconsistent. -/// -/// TODO: As soon as all supertypes are known, make this a enum. -pub type CardSupertype = String; +/// A supertype of a [`Card`](super::card::Card), such as *Champion*. +#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "UPPERCASE")] +pub enum CardSupertype { + /// No supertype, like most cards in the game. + #[serde(rename = "")] + None, + /// A [Champion](super::rarity::CardRarity::Champion). + #[serde(alias = "Champion")] + Champion, + /// A supertype of an unknown type. + #[serde(other)] + Unsupported, +} + + +#[cfg(test)] +mod tests { + use super::CardSupertype; + + macro_rules! test_deserialization { + ( $id:ident, $src:literal, $res:expr ) => { + #[test] + fn $id() { + assert_eq!( + serde_json::de::from_str::<'static, CardSupertype>($src).unwrap(), + $res + ); + } + }; + } + + test_deserialization!(deserialize_none, r#""""#, CardSupertype::None); + test_deserialization!(deserialize_champion_uppercase, r#""CHAMPION""#, CardSupertype::Champion); + test_deserialization!(deserialize_champion_titlecase, r#""Champion""#, CardSupertype::Champion); + test_deserialization!(deserialize_unsupported, r#""sUs""#, CardSupertype::Unsupported); +} diff --git a/src/telegram/display.rs b/src/telegram/display.rs index 03645be..9fac47f 100644 --- a/src/telegram/display.rs +++ b/src/telegram/display.rs @@ -96,9 +96,13 @@ fn display_regions(regions: &[CardRegion], hm: &LocalizedCardRegionIndex) -> Str fn display_types(r#type: &CardType, supertype: &CardSupertype, subtypes: &[CardSubtype]) -> String { let mut result = String::new(); - if supertype != "" { - result.push_str(&*format!("{} › ", escape(&supertype),)); - }; + result.push_str( + match supertype { + CardSupertype::Champion => "Champion › ", + CardSupertype::Unsupported => "Unknown › ", + _ => "", + } + ); result.push_str(&*format!("{}", escape(&*String::from(r#type)),));