From 2d80f97170640021938aaafd76ff5cfcc7b0568e Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Wed, 30 Jun 2021 19:40:48 -0700 Subject: [PATCH] Convert Map from enum into newtype_enum --- src/consts/macros.rs | 2 +- src/consts/map.rs | 107 +++++++++++++++++++--------------------- srcgen/consts/map.rs.dt | 21 ++++---- 3 files changed, 62 insertions(+), 68 deletions(-) diff --git a/src/consts/macros.rs b/src/consts/macros.rs index 764dddb..550dbf8 100644 --- a/src/consts/macros.rs +++ b/src/consts/macros.rs @@ -64,7 +64,7 @@ macro_rules! newtype_enum { } => { $( #[$attr] )* #[derive(Copy, Clone)] - #[derive(PartialEq, Eq, PartialOrd, Ord)] + #[derive(Hash, PartialEq, Eq, PartialOrd, Ord)] #[repr(transparent)] $v struct $name($v $repr); impl $name { diff --git a/src/consts/map.rs b/src/consts/map.rs index 7ef6cab..b782f2e 100644 --- a/src/consts/map.rs +++ b/src/consts/map.rs @@ -6,60 +6,57 @@ // // /////////////////////////////////////////////// -use serde_repr::{ Serialize_repr, Deserialize_repr }; -use num_enum::{ IntoPrimitive, TryFromPrimitive }; +use serde::{ Serialize, Deserialize }; -/// League of Legends maps. -#[non_exhaustive] -#[derive(Debug, Copy, Clone)] -#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] -#[derive(Serialize_repr, Deserialize_repr)] -#[derive(IntoPrimitive, TryFromPrimitive)] -#[repr(u8)] -pub enum Map { - /// Summoner's Rift - /// Original Summer variant - SUMMONERS_RIFT_ORIGINAL_SUMMER_VARIANT = 1, - /// Summoner's Rift - /// Original Autumn variant - SUMMONERS_RIFT_ORIGINAL_AUTUMN_VARIANT = 2, - /// The Proving Grounds - /// Tutorial Map - THE_PROVING_GROUNDS = 3, - /// Twisted Treeline - /// Original Version - TWISTED_TREELINE_ORIGINAL_VERSION = 4, - /// The Crystal Scar - /// Dominion map - THE_CRYSTAL_SCAR = 8, - /// Twisted Treeline - /// Last TT map - TWISTED_TREELINE = 10, - /// Summoner's Rift - /// Current Version - SUMMONERS_RIFT = 11, - /// Howling Abyss - /// ARAM map - HOWLING_ABYSS = 12, - /// Butcher's Bridge - /// Alternate ARAM map - BUTCHERS_BRIDGE = 14, - /// Cosmic Ruins - /// Dark Star: Singularity map - COSMIC_RUINS = 16, - /// Valoran City Park - /// Star Guardian Invasion map - VALORAN_CITY_PARK = 18, - /// Substructure 43 - /// PROJECT: Hunters map - SUBSTRUCTURE_43 = 19, - /// Crash Site - /// Odyssey: Extraction map - CRASH_SITE = 20, - /// Nexus Blitz - /// Nexus Blitz map - NEXUS_BLITZ = 21, - /// Convergence - /// Teamfight Tactics map - CONVERGENCE = 22, +newtype_enum! { + /// A League of Legends map. + #[derive(Serialize, Deserialize)] + #[serde(transparent)] + pub newtype_enum Map(u8) { + /// Summoner's Rift + /// Original Summer variant + SUMMONERS_RIFT_ORIGINAL_SUMMER_VARIANT = 1, + /// Summoner's Rift + /// Original Autumn variant + SUMMONERS_RIFT_ORIGINAL_AUTUMN_VARIANT = 2, + /// The Proving Grounds + /// Tutorial Map + THE_PROVING_GROUNDS = 3, + /// Twisted Treeline + /// Original Version + TWISTED_TREELINE_ORIGINAL_VERSION = 4, + /// The Crystal Scar + /// Dominion map + THE_CRYSTAL_SCAR = 8, + /// Twisted Treeline + /// Last TT map + TWISTED_TREELINE = 10, + /// Summoner's Rift + /// Current Version + SUMMONERS_RIFT = 11, + /// Howling Abyss + /// ARAM map + HOWLING_ABYSS = 12, + /// Butcher's Bridge + /// Alternate ARAM map + BUTCHERS_BRIDGE = 14, + /// Cosmic Ruins + /// Dark Star: Singularity map + COSMIC_RUINS = 16, + /// Valoran City Park + /// Star Guardian Invasion map + VALORAN_CITY_PARK = 18, + /// Substructure 43 + /// PROJECT: Hunters map + SUBSTRUCTURE_43 = 19, + /// Crash Site + /// Odyssey: Extraction map + CRASH_SITE = 20, + /// Nexus Blitz + /// Nexus Blitz map + NEXUS_BLITZ = 21, + /// Convergence + /// Teamfight Tactics map + CONVERGENCE = 22, + } } diff --git a/srcgen/consts/map.rs.dt b/srcgen/consts/map.rs.dt index 5c7f42c..c52a854 100644 --- a/srcgen/consts/map.rs.dt +++ b/srcgen/consts/map.rs.dt @@ -3,26 +3,23 @@ const maps = require('./.maps.json'); }}{{= dotUtils.preamble() }} -use serde_repr::{ Serialize_repr, Deserialize_repr }; -use num_enum::{ IntoPrimitive, TryFromPrimitive }; +use serde::{ Serialize, Deserialize }; -/// League of Legends maps. -#[non_exhaustive] -#[derive(Debug, Copy, Clone)] -#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] -#[derive(Serialize_repr, Deserialize_repr)] -#[derive(IntoPrimitive, TryFromPrimitive)] -#[repr(u8)] -pub enum Map { +newtype_enum! { + /// A League of Legends map. + #[derive(Serialize, Deserialize)] + #[serde(transparent)] + pub newtype_enum Map(u8) { {{ for (const e of maps) { const desc = e['x-desc'] ? e['x-desc'].split('\n') : []; }} {{~ desc :line }} - /// {{= line }} + /// {{= line }} {{~}} - {{= e['x-name'] }} = {{= e['x-value'] }}, + {{= e['x-name'] }} = {{= e['x-value'] }}, {{ } }} + } }