forked from mirror/Riven
1
0
Fork 0

Revert "Remove serde_string! macro in favor of #[derive(Serialize, Deserialize)]"

This reverts commit 038e5eb493.

This is to fix handling of `#[strum(default)]` variants.
MingweiSamuel-patch-1
Mingwei Samuel 2022-06-20 09:40:28 -07:00
parent 20863c5bcc
commit 104db04d9d
8 changed files with 36 additions and 15 deletions

View File

@ -1,7 +1,6 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use num_enum::{ IntoPrimitive, TryFromPrimitive }; use num_enum::{ IntoPrimitive, TryFromPrimitive };
use serde::{ Serialize, Deserialize };
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
@ -16,7 +15,6 @@ use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] #[derive(EnumString, Display, AsRefStr, IntoStaticStr)]
#[derive(IntoPrimitive, TryFromPrimitive)] #[derive(IntoPrimitive, TryFromPrimitive)]
#[derive(Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub enum Division { pub enum Division {
/// Division 1, the best/highest division in a [`Tier`](crate::consts::Tier), or the only division in /// Division 1, the best/highest division in a [`Tier`](crate::consts::Tier), or the only division in
@ -33,6 +31,8 @@ pub enum Division {
V = 5, V = 5,
} }
serde_string!(Division);
/// Returns a DoubleEndedIterator of I, II, III, IV. /// Returns a DoubleEndedIterator of I, II, III, IV.
/// Ordered from high rank (I) to low (IV). /// Ordered from high rank (I) to low (IV).
/// Excludes V, which is deprecated. /// Excludes V, which is deprecated.

View File

@ -6,7 +6,6 @@
// // // //
/////////////////////////////////////////////// ///////////////////////////////////////////////
use serde::{ Serialize, Deserialize };
use strum_macros::{ EnumString, IntoStaticStr }; use strum_macros::{ EnumString, IntoStaticStr };
/// League of Legends game mode, such as Classic, /// League of Legends game mode, such as Classic,
@ -15,7 +14,6 @@ use strum_macros::{ EnumString, IntoStaticStr };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, IntoStaticStr)] #[derive(EnumString, IntoStaticStr)]
#[derive(Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub enum GameMode { pub enum GameMode {
/// Catch-all variant for new, unknown game modes. /// Catch-all variant for new, unknown game modes.
@ -73,3 +71,4 @@ pub enum GameMode {
} }
string_enum_str!(GameMode); string_enum_str!(GameMode);
serde_string!(GameMode);

View File

@ -6,14 +6,12 @@
// // // //
/////////////////////////////////////////////// ///////////////////////////////////////////////
use serde::{ Serialize, Deserialize };
use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
/// League of Legends game type: matched game, custom game, or tutorial game. /// League of Legends game type: matched game, custom game, or tutorial game.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] #[derive(EnumString, Display, AsRefStr, IntoStaticStr)]
#[derive(Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub enum GameType { pub enum GameType {
/// Custom games /// Custom games
@ -23,3 +21,5 @@ pub enum GameType {
/// Tutorial games /// Tutorial games
TUTORIAL_GAME, TUTORIAL_GAME,
} }
serde_string!(GameType);

View File

@ -1,5 +1,28 @@
#![macro_use] #![macro_use]
macro_rules! serde_string {
( $name:ident ) => {
impl<'de> serde::de::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>
{
let s = String::deserialize(deserializer)?;
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
S: serde::ser::Serializer,
{
serializer.serialize_str(self.as_ref())
}
}
};
}
macro_rules! string_enum_str { macro_rules! string_enum_str {
( $name:ident ) => { ( $name:ident ) => {
impl AsRef<str> for $name { impl AsRef<str> for $name {

View File

@ -1,4 +1,3 @@
use serde::{ Serialize, Deserialize };
use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
/// LoL or TFT ranked queue types. /// LoL or TFT ranked queue types.
@ -6,7 +5,6 @@ use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] #[derive(EnumString, Display, AsRefStr, IntoStaticStr)]
#[derive(Serialize, Deserialize)]
pub enum QueueType { pub enum QueueType {
/// Catch-all variant for new, unknown queue types. /// Catch-all variant for new, unknown queue types.
#[strum(default)] #[strum(default)]
@ -30,6 +28,8 @@ pub enum QueueType {
RANKED_TFT_PAIRS, RANKED_TFT_PAIRS,
} }
serde_string!(QueueType);
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -1,6 +1,5 @@
use num_enum::{ IntoPrimitive, TryFromPrimitive };
use serde::{ Serialize, Deserialize };
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use num_enum::{ IntoPrimitive, TryFromPrimitive };
use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
/// LoL and TFT ranked tiers, such as gold, diamond, challenger, etc. /// LoL and TFT ranked tiers, such as gold, diamond, challenger, etc.
@ -14,7 +13,6 @@ use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)]
#[derive(IntoPrimitive, TryFromPrimitive)] #[derive(IntoPrimitive, TryFromPrimitive)]
#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] #[derive(EnumString, Display, AsRefStr, IntoStaticStr)]
#[derive(Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub enum Tier { pub enum Tier {
/// Challenger, the highest tier, an apex tier. Repr: `220_u8`. /// Challenger, the highest tier, an apex tier. Repr: `220_u8`.
@ -42,6 +40,8 @@ pub enum Tier {
UNRANKED = 0, UNRANKED = 0,
} }
serde_string!(Tier);
impl Tier { impl Tier {
/// If this tier is an apex tier: [`Self::MASTER`], [`Self::GRANDMASTER`], /// If this tier is an apex tier: [`Self::MASTER`], [`Self::GRANDMASTER`],
/// or [`Self::CHALLENGER`]. Returns false for [`Self::UNRANKED`]. /// or [`Self::CHALLENGER`]. Returns false for [`Self::UNRANKED`].

View File

@ -3,7 +3,6 @@
const gameModes = require('./.gameModes.json'); const gameModes = require('./.gameModes.json');
}}{{= dotUtils.preamble() }} }}{{= dotUtils.preamble() }}
use serde::{ Serialize, Deserialize };
use strum_macros::{ EnumString, IntoStaticStr }; use strum_macros::{ EnumString, IntoStaticStr };
/// League of Legends game mode, such as Classic, /// League of Legends game mode, such as Classic,
@ -12,7 +11,6 @@ use strum_macros::{ EnumString, IntoStaticStr };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, IntoStaticStr)] #[derive(EnumString, IntoStaticStr)]
#[derive(Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub enum GameMode { pub enum GameMode {
/// Catch-all variant for new, unknown game modes. /// Catch-all variant for new, unknown game modes.
@ -33,3 +31,4 @@ pub enum GameMode {
} }
string_enum_str!(GameMode); string_enum_str!(GameMode);
serde_string!(GameMode);

View File

@ -3,14 +3,12 @@
const gameTypes = require('./.gameTypes.json'); const gameTypes = require('./.gameTypes.json');
}}{{= dotUtils.preamble() }} }}{{= dotUtils.preamble() }}
use serde::{ Serialize, Deserialize };
use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr }; use strum_macros::{ EnumString, Display, AsRefStr, IntoStaticStr };
/// League of Legends game type: matched game, custom game, or tutorial game. /// League of Legends game type: matched game, custom game, or tutorial game.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr, IntoStaticStr)] #[derive(EnumString, Display, AsRefStr, IntoStaticStr)]
#[derive(Serialize, Deserialize)]
#[repr(u8)] #[repr(u8)]
pub enum GameType { pub enum GameType {
{{ {{
@ -25,3 +23,5 @@ pub enum GameType {
} }
}} }}
} }
serde_string!(GameType);