Add unknown variant to GameMode

This commit is contained in:
Mingwei Samuel 2021-06-30 19:35:08 -07:00
parent 5daeab990a
commit 88124ecb3a
3 changed files with 38 additions and 8 deletions

View file

@ -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);

View file

@ -11,6 +11,7 @@ macro_rules! serde_string {
s.parse().map_err(serde::de::Error::custom)
}
}
impl serde::ser::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -22,6 +23,25 @@ macro_rules! serde_string {
};
}
macro_rules! string_enum_str {
( $name:ident ) => {
impl AsRef<str> 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,

View file

@ -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);