From 44b12c2f32a06b253edea0057733112a2380e3b7 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Fri, 25 Oct 2019 13:37:23 -0700 Subject: [PATCH] Add division enum, update enums with strum macros, serde_repr --- Cargo.toml | 3 +++ src/consts/division.rs | 19 +++++++++++++++++++ src/consts/mod.rs | 19 +++++++++++-------- src/consts/queue.rs | 7 +++++++ src/consts/queue_type.rs | 39 ++++++++++++++++++++++++++------------- srcgen/consts/queue.rs.dt | 7 +++++++ 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 src/consts/division.rs diff --git a/Cargo.toml b/Cargo.toml index fb1a02d..77bf0cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,9 @@ parking_lot = { version = "0.9", features = [ "nightly" ] } reqwest = { version = "0.10.0-alpha.1", features = [ "gzip", "json" ] } scan_fmt = "0.2" serde = { version = "1.0", features = [ "derive" ] } +serde_repr = "0.1" +strum = "0.16" +strum_macros = "0.16" tokio-timer = "0.3.0-alpha.5" url = "2.1" diff --git a/src/consts/division.rs b/src/consts/division.rs new file mode 100644 index 0000000..8e74577 --- /dev/null +++ b/src/consts/division.rs @@ -0,0 +1,19 @@ +#![allow(deprecated)] + +use std::fmt::Debug; + +use strum_macros::{ EnumString, Display, AsRefStr }; + +#[derive(Debug, Copy, Clone)] +#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] +#[derive(EnumString, Display, AsRefStr)] +#[repr(u8)] +pub enum Division { + I = 1, + II = 2, + III = 3, + IV = 4, + + #[deprecated(note="Removed for 2019.")] + V = 5, +} diff --git a/src/consts/mod.rs b/src/consts/mod.rs index 33d9400..4793745 100644 --- a/src/consts/mod.rs +++ b/src/consts/mod.rs @@ -1,19 +1,22 @@ //! Constant data and Enums relevant to the Riot Games API. -mod region; -pub use region::*; - mod champion; pub use champion::*; -mod season; -pub use season::*; - -mod queue; -pub use queue::*; +mod division; +pub use division::*; mod queue_type; pub use queue_type::*; +mod queue; +pub use queue::*; + +mod region; +pub use region::*; + +mod season; +pub use season::*; + mod team; pub use team::*; diff --git a/src/consts/queue.rs b/src/consts/queue.rs index 9e12575..df1e86a 100644 --- a/src/consts/queue.rs +++ b/src/consts/queue.rs @@ -1,7 +1,14 @@ // This file is automatically generated. // Do not directly edit. +#![allow(deprecated)] + +use serde_repr::{ Serialize_repr, Deserialize_repr }; /// League of Legends matchmaking queue. +#[derive(Debug, Copy, Clone)] +#[derive(Eq, PartialEq)] +#[derive(Serialize_repr, Deserialize_repr)] +#[repr(u16)] pub enum Queue { /// Custom games. diff --git a/src/consts/queue_type.rs b/src/consts/queue_type.rs index 7b9697e..036fb7a 100644 --- a/src/consts/queue_type.rs +++ b/src/consts/queue_type.rs @@ -1,28 +1,41 @@ -use std::fmt; +use std::fmt::Debug; -#[derive(fmt::Debug, Copy, Clone)] +use strum_macros::{ EnumString, Display, AsRefStr }; + +#[derive(Debug, Copy, Clone)] +#[derive(Eq, PartialEq, Hash)] +#[derive(EnumString, Display, AsRefStr)] pub enum QueueType { // League of Legends, Summoner's Rift (5v5), Ranked Solo Queue. + #[strum(to_string="RANKED_SOLO_5x5")] RankedSolo5x5, // League of Legends, Summoner's Rift (5v5), Flex Queue. + #[strum(to_string="RANKED_FLEX_SR")] RankedFlexSr, // League of Legends, Twisted Treeline (3v3), Flex Queue. + #[strum(to_string="RANKED_FLEX_TT")] RankedFlexTt, // Ranked Teamfight Tactics. + #[strum(to_string="RANKED_TFT")] RankedTft, } -impl QueueType { - const NAMES: [&'static str; 4] = [ - "RANKED_SOLO_5x5", - "RANKED_FLEX_SR", - "RANKED_FLEX_TT", - "RANKED_TFT", - ]; -} +#[cfg(test)] +mod test { + use super::*; -impl fmt::Display for QueueType { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", Self::NAMES[*self as usize]) + #[test] + fn check_as_ref() { + assert_eq!("RANKED_SOLO_5x5", QueueType::RankedSolo5x5.as_ref()); + } + + #[test] + fn check_to_string() { + assert_eq!("RANKED_SOLO_5x5", QueueType::RankedSolo5x5.to_string()); + } + + #[test] + fn check_from_string() { + assert_eq!(Some(QueueType::RankedSolo5x5), "RANKED_SOLO_5x5".parse().ok()); } } diff --git a/srcgen/consts/queue.rs.dt b/srcgen/consts/queue.rs.dt index fbf2ee7..5ba0578 100644 --- a/srcgen/consts/queue.rs.dt +++ b/srcgen/consts/queue.rs.dt @@ -8,8 +8,15 @@ return name; }); }}{{= dotUtils.preamble() }} +#![allow(deprecated)] + +use serde_repr::{ Serialize_repr, Deserialize_repr }; /// League of Legends matchmaking queue. +#[derive(Debug, Copy, Clone)] +#[derive(Eq, PartialEq)] +#[derive(Serialize_repr, Deserialize_repr)] +#[repr(u16)] pub enum Queue { {{ for (let [ groupName, colQueues ] of groupedQueues) {