From 88124ecb3aa7efeaf5386b85eaf991458411995a Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Wed, 30 Jun 2021 19:35:08 -0700 Subject: [PATCH] Add unknown variant to GameMode --- src/consts/game_mode.rs | 11 ++++++++--- src/consts/macros.rs | 24 ++++++++++++++++++++++-- srcgen/consts/game_mode.rs.dt | 11 ++++++++--- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/consts/game_mode.rs b/src/consts/game_mode.rs index e32c729..d893fdc 100644 --- a/src/consts/game_mode.rs +++ b/src/consts/game_mode.rs @@ -6,16 +6,20 @@ // // /////////////////////////////////////////////// -use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; +use strum_macros::{ EnumString, IntoStaticStr }; /// League of Legends game mode, such as Classic, /// ARAM, URF, One For All, Ascension, etc. #[non_exhaustive] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] #[derive(Eq, PartialEq, Hash)] -#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] +#[derive(EnumString, IntoStaticStr)] #[repr(u8)] pub enum GameMode { + // Catch-all variant for new, unknown game modes. + #[strum(default)] + UNKNOWN(String), + /// ARAM games ARAM, /// All Random Summoner's Rift games @@ -62,4 +66,5 @@ pub enum GameMode { URF, } +string_enum_str!(GameMode); serde_string!(GameMode); diff --git a/src/consts/macros.rs b/src/consts/macros.rs index f2196d5..764dddb 100644 --- a/src/consts/macros.rs +++ b/src/consts/macros.rs @@ -11,6 +11,7 @@ macro_rules! serde_string { s.parse().map_err(serde::de::Error::custom) } } + impl serde::ser::Serialize for $name { fn serialize(&self, serializer: S) -> Result where @@ -22,6 +23,25 @@ macro_rules! serde_string { }; } +macro_rules! string_enum_str { + ( $name:ident ) => { + impl AsRef for $name { + fn as_ref(&self) -> &str { + match self { + Self::UNKNOWN(string) => &*string, + known => known.into(), + } + } + } + + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + self.as_ref().fmt(f) + } + } + } +} + macro_rules! arr { ( $( #[$attr:meta] )* @@ -35,7 +55,7 @@ macro_rules! arr { macro_rules! newtype_enum { { $( #[$attr:meta] )* - $v:vis newtype_enum $name:ident($repr:ident) { + $v:vis newtype_enum $name:ident($repr:ty) { $( $( #[$var_attr:meta] )* $var_name:ident = $var_val:expr, @@ -50,7 +70,7 @@ macro_rules! newtype_enum { impl $name { $( $( #[$var_attr] )* - $v const $var_name: Self = Self( $var_val ); + $v const $var_name: Self = Self($var_val); )* } diff --git a/srcgen/consts/game_mode.rs.dt b/srcgen/consts/game_mode.rs.dt index 18f563a..8a4ad43 100644 --- a/srcgen/consts/game_mode.rs.dt +++ b/srcgen/consts/game_mode.rs.dt @@ -3,16 +3,20 @@ const gameModes = require('./.gameModes.json'); }}{{= dotUtils.preamble() }} -use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; +use strum_macros::{ EnumString, IntoStaticStr }; /// League of Legends game mode, such as Classic, /// ARAM, URF, One For All, Ascension, etc. #[non_exhaustive] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Clone)] #[derive(Eq, PartialEq, Hash)] -#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] +#[derive(EnumString, IntoStaticStr)] #[repr(u8)] pub enum GameMode { + // Catch-all variant for new, unknown game modes. + #[strum(default)] + UNKNOWN(String), + {{ for (const e of gameModes) { const desc = e['x-desc'] ? e['x-desc'].split('\n') : []; @@ -26,4 +30,5 @@ pub enum GameMode { }} } +string_enum_str!(GameMode); serde_string!(GameMode);