From 58cf643e13ca5584b5981574bb614846ea380f2b Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Fri, 25 Oct 2019 22:19:00 -0700 Subject: [PATCH] adding enums to serialization --- src/consts/champion.rs | 21 ++++++-- src/consts/division.rs | 2 + src/consts/game_mode.rs | 11 ++++- src/consts/game_type.rs | 11 ++++- src/consts/macro_serde_string.rs | 24 +++++++++ src/consts/map.rs | 12 ++++- src/consts/mod.rs | 2 + src/consts/queue.rs | 9 +++- src/consts/queue_type.rs | 2 + src/consts/season.rs | 12 ++++- src/consts/team.rs | 6 ++- src/consts/tier.rs | 2 + src/endpoints.rs | 21 +++++--- src/endpoints/dto.rs | 83 +++++++++++++++++--------------- srcgen/consts/champion.rs.dt | 12 ++++- srcgen/consts/game_mode.rs.dt | 2 + srcgen/consts/game_type.rs.dt | 2 + srcgen/consts/map.rs.dt | 3 ++ srcgen/consts/season.rs.dt | 3 ++ srcgen/dotUtils.js | 20 +++++--- 20 files changed, 188 insertions(+), 72 deletions(-) create mode 100644 src/consts/macro_serde_string.rs diff --git a/src/consts/champion.rs b/src/consts/champion.rs index efa56dd..1b397c5 100644 --- a/src/consts/champion.rs +++ b/src/consts/champion.rs @@ -1,16 +1,27 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// use std::fmt; -use num_derive; +use num_derive::{ FromPrimitive, ToPrimitive }; +use serde_repr::{ Serialize_repr, Deserialize_repr }; /// League of Legend's champions. /// /// The documentation of each variant specifies:
/// NAME (`IDENTIFIER`, ID). #[derive(Debug, Copy, Clone)] -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive)] +#[derive(FromPrimitive, ToPrimitive)] +#[derive(Serialize_repr, Deserialize_repr)] +#[repr(i16)] pub enum Champion { + /// A champion that doesn't exist. Used in TeamBans when no ban occured. + None = -1, + /** Aatrox (`Aatrox`, 266). */ Aatrox = 266, /** Ahri (`Ahri`, 103). */ Ahri = 103, /** Akali (`Akali`, 84). */ Akali = 84, @@ -161,6 +172,7 @@ pub enum Champion { impl Champion { pub fn name(self) -> &'static str { match self { + Self::None => "None", Self::Aatrox => "Aatrox", Self::Ahri => "Ahri", Self::Akali => "Akali", @@ -311,6 +323,7 @@ impl Champion { pub fn identifier(self) -> &'static str { match self { + Self::None => "None", Self::Aatrox => "Aatrox", Self::Ahri => "Ahri", Self::Akali => "Akali", diff --git a/src/consts/division.rs b/src/consts/division.rs index 28a4764..4a33634 100644 --- a/src/consts/division.rs +++ b/src/consts/division.rs @@ -1,10 +1,12 @@ #![allow(deprecated)] use strum_macros::{ EnumString, Display, AsRefStr }; +use serde_repr::{ Serialize_repr, Deserialize_repr }; #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(EnumString, Display, AsRefStr)] +#[derive(Serialize_repr, Deserialize_repr)] #[repr(u8)] pub enum Division { I = 1, diff --git a/src/consts/game_mode.rs b/src/consts/game_mode.rs index 19d061f..d71d103 100644 --- a/src/consts/game_mode.rs +++ b/src/consts/game_mode.rs @@ -1,5 +1,10 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// use strum_macros::{ EnumString, Display, AsRefStr }; @@ -65,3 +70,5 @@ pub enum GameMode { #[strum(to_string="ODYSSEY")] Odyssey, } + +serde_string!(GameMode); diff --git a/src/consts/game_type.rs b/src/consts/game_type.rs index 0b0f219..1ab286b 100644 --- a/src/consts/game_type.rs +++ b/src/consts/game_type.rs @@ -1,5 +1,10 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// use strum_macros::{ EnumString, Display, AsRefStr }; @@ -19,3 +24,5 @@ pub enum GameType { #[strum(to_string="MATCHED_GAME")] MatchedGame, } + +serde_string!(GameType); diff --git a/src/consts/macro_serde_string.rs b/src/consts/macro_serde_string.rs new file mode 100644 index 0000000..2d3762a --- /dev/null +++ b/src/consts/macro_serde_string.rs @@ -0,0 +1,24 @@ +#![macro_use] +#[macro_export] +macro_rules! serde_string { + ( $x:ty ) => { + impl<'de> serde::de::Deserialize<'de> for $x + { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de> + { + let s = String::deserialize(deserializer)?; + s.parse().map_err(serde::de::Error::custom) + } + } + impl serde::ser::Serialize for $x { + fn serialize(&self, serializer: S) -> Result + where + S: serde::ser::Serializer, + { + serializer.serialize_str(self.as_ref()) + } + } + }; +} diff --git a/src/consts/map.rs b/src/consts/map.rs index 992408a..cb46e4f 100644 --- a/src/consts/map.rs +++ b/src/consts/map.rs @@ -1,9 +1,17 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// + +use serde_repr::{ Serialize_repr, Deserialize_repr }; /// League of Legends maps. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] +#[derive(Serialize_repr, Deserialize_repr)] #[repr(u8)] pub enum Map { diff --git a/src/consts/mod.rs b/src/consts/mod.rs index 5b8826b..35c3853 100644 --- a/src/consts/mod.rs +++ b/src/consts/mod.rs @@ -1,5 +1,7 @@ //! Constant data and Enums relevant to the Riot Games API. +mod macro_serde_string; + mod champion; pub use champion::*; diff --git a/src/consts/queue.rs b/src/consts/queue.rs index df1e86a..1202b7e 100644 --- a/src/consts/queue.rs +++ b/src/consts/queue.rs @@ -1,5 +1,10 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// #![allow(deprecated)] use serde_repr::{ Serialize_repr, Deserialize_repr }; diff --git a/src/consts/queue_type.rs b/src/consts/queue_type.rs index 163ae2f..2d45172 100644 --- a/src/consts/queue_type.rs +++ b/src/consts/queue_type.rs @@ -18,6 +18,8 @@ pub enum QueueType { RankedTft, } +serde_string!(QueueType); + #[cfg(test)] mod test { use super::*; diff --git a/src/consts/season.rs b/src/consts/season.rs index 256429c..ef8f61d 100644 --- a/src/consts/season.rs +++ b/src/consts/season.rs @@ -1,9 +1,17 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// + +use serde_repr::{ Serialize_repr, Deserialize_repr }; /// League of Legends matchmaking seasons. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] +#[derive(Serialize_repr, Deserialize_repr)] #[repr(u8)] pub enum Season { Preseason3 = 0, diff --git a/src/consts/team.rs b/src/consts/team.rs index 92b806b..73dcc0d 100644 --- a/src/consts/team.rs +++ b/src/consts/team.rs @@ -1,6 +1,10 @@ +use serde_repr::{ Serialize_repr, Deserialize_repr }; + +/// League of Legends team. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, Ord, PartialOrd)] -/// League of Legends team. +#[derive(Serialize_repr, Deserialize_repr)] +#[repr(u8)] pub enum Team { /// Blue team (bottom left on Summoner's Rift). Blue = 100, diff --git a/src/consts/tier.rs b/src/consts/tier.rs index 2450455..c6b67ca 100644 --- a/src/consts/tier.rs +++ b/src/consts/tier.rs @@ -18,6 +18,8 @@ pub enum Tier { #[strum(to_string="CHALLENGER")] Challenger = 240, } +serde_string!(Tier); + impl Tier { /// If this tier is "standard". /// Standard means non-apex (not master+), and not unranked. diff --git a/src/endpoints.rs b/src/endpoints.rs index 5cc1078..473a72d 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -1,5 +1,10 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// // http://www.mingweisamuel.com/riotapi-schema/tool/ // Version b2fb0513c3cdb6baa0ba78bd2a50b43670161980 @@ -125,7 +130,7 @@ impl<'a> ChampionMasteryV4<'a> { /// * `region` - Region to query. /// * `championId` - Champion ID to retrieve Champion Mastery for /// * `encryptedSummonerId` - Summoner ID associated with the player - pub fn get_champion_mastery(&self, region: Region, encrypted_summoner_id: &str, champion_id: i64) + pub fn get_champion_mastery(&self, region: Region, encrypted_summoner_id: &str, champion_id: crate::consts::Champion) -> impl Future>> + 'a { let path_string = format!("/lol/champion-mastery/v4/champion-masteries/by-summoner/{}/by-champion/{}", encrypted_summoner_id, champion_id); @@ -184,7 +189,7 @@ impl<'a> LeagueExpV4<'a> { /// * `tier` /// * `division` /// * `page` (optional) - Starts with page 1. - pub fn get_league_entries(&self, region: Region, division: &str, tier: &str, queue: &str, page: Option) + pub fn get_league_entries(&self, region: Region, division: crate::consts::Division, tier: crate::consts::Tier, queue: crate::consts::QueueType, page: Option) -> impl Future>>> + 'a { let mut query_params = Serializer::new(String::new()); @@ -209,7 +214,7 @@ impl<'a> LeagueV4<'a> { /// # Parameters /// * `region` - Region to query. /// * `queue` - pub fn get_challenger_league(&self, region: Region, queue: &str) + pub fn get_challenger_league(&self, region: Region, queue: crate::consts::QueueType) -> impl Future>> + 'a { let path_string = format!("/lol/league/v4/challengerleagues/by-queue/{}", queue); @@ -238,7 +243,7 @@ impl<'a> LeagueV4<'a> { /// * `tier` /// * `queue` - Note that the queue value must be a valid ranked queue. /// * `page` (optional) - Starts with page 1. - pub fn get_league_entries(&self, region: Region, queue: &str, tier: &str, division: &str, page: Option) + pub fn get_league_entries(&self, region: Region, queue: crate::consts::QueueType, tier: crate::consts::Tier, division: crate::consts::Division, page: Option) -> impl Future>>> + 'a { let mut query_params = Serializer::new(String::new()); @@ -254,7 +259,7 @@ impl<'a> LeagueV4<'a> { /// # Parameters /// * `region` - Region to query. /// * `queue` - pub fn get_grandmaster_league(&self, region: Region, queue: &str) + pub fn get_grandmaster_league(&self, region: Region, queue: crate::consts::QueueType) -> impl Future>> + 'a { let path_string = format!("/lol/league/v4/grandmasterleagues/by-queue/{}", queue); @@ -280,7 +285,7 @@ impl<'a> LeagueV4<'a> { /// # Parameters /// * `region` - Region to query. /// * `queue` - pub fn get_master_league(&self, region: Region, queue: &str) + pub fn get_master_league(&self, region: Region, queue: crate::consts::QueueType) -> impl Future>> + 'a { let path_string = format!("/lol/league/v4/masterleagues/by-queue/{}", queue); diff --git a/src/endpoints/dto.rs b/src/endpoints/dto.rs index 5c4863d..99abcc9 100644 --- a/src/endpoints/dto.rs +++ b/src/endpoints/dto.rs @@ -1,5 +1,10 @@ -// This file is automatically generated. -// Do not directly edit. +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +/////////////////////////////////////////////// // http://www.mingweisamuel.com/riotapi-schema/tool/ // Version b2fb0513c3cdb6baa0ba78bd2a50b43670161980 @@ -24,7 +29,7 @@ pub mod champion_mastery_v4 { pub champion_points: i32, /// Champion ID for this entry. #[serde(rename = "championId")] - pub champion_id: i64, + pub champion_id: crate::consts::Champion, /// Number of points needed to achieve next level. Zero if player reached maximum champion level for this champion. #[serde(rename = "championPointsUntilNextLevel")] pub champion_points_until_next_level: i64, @@ -67,7 +72,7 @@ pub mod league_exp_v4 { #[derive(serde::Serialize, serde::Deserialize)] pub struct LeagueEntry { #[serde(rename = "queueType")] - pub queue_type: String, + pub queue_type: crate::consts::QueueType, #[serde(rename = "summonerName")] pub summoner_name: String, #[serde(rename = "hotStreak")] @@ -83,7 +88,7 @@ pub mod league_exp_v4 { #[serde(rename = "losses")] pub losses: i32, #[serde(rename = "rank")] - pub rank: String, + pub rank: crate::consts::Division, #[serde(rename = "leagueId")] pub league_id: String, #[serde(rename = "inactive")] @@ -91,7 +96,7 @@ pub mod league_exp_v4 { #[serde(rename = "freshBlood")] pub fresh_blood: bool, #[serde(rename = "tier")] - pub tier: String, + pub tier: crate::consts::Tier, /// Player's summonerId (Encrypted) #[serde(rename = "summonerId")] pub summoner_id: String, @@ -123,11 +128,11 @@ pub mod league_v4 { #[serde(rename = "leagueId")] pub league_id: String, #[serde(rename = "tier")] - pub tier: String, + pub tier: crate::consts::Tier, #[serde(rename = "entries")] pub entries: std::vec::Vec, #[serde(rename = "queue")] - pub queue: String, + pub queue: crate::consts::QueueType, #[serde(rename = "name")] pub name: String, } @@ -154,7 +159,7 @@ pub mod league_v4 { #[serde(rename = "inactive")] pub inactive: bool, #[serde(rename = "rank")] - pub rank: String, + pub rank: crate::consts::Division, /// Player's summonerId (Encrypted) #[serde(rename = "summonerId")] pub summoner_id: String, @@ -179,7 +184,7 @@ pub mod league_v4 { #[derive(serde::Serialize, serde::Deserialize)] pub struct LeagueEntry { #[serde(rename = "queueType")] - pub queue_type: String, + pub queue_type: crate::consts::QueueType, #[serde(rename = "summonerName")] pub summoner_name: String, #[serde(rename = "hotStreak")] @@ -195,7 +200,7 @@ pub mod league_v4 { #[serde(rename = "losses")] pub losses: i32, #[serde(rename = "rank")] - pub rank: String, + pub rank: crate::consts::Division, #[serde(rename = "leagueId")] pub league_id: String, #[serde(rename = "inactive")] @@ -203,7 +208,7 @@ pub mod league_v4 { #[serde(rename = "freshBlood")] pub fresh_blood: bool, #[serde(rename = "tier")] - pub tier: String, + pub tier: crate::consts::Tier, /// Player's summonerId (Encrypted) #[serde(rename = "summonerId")] pub summoner_id: String, @@ -299,10 +304,10 @@ pub mod match_v4 { pub struct Match { /// Please refer to the Game Constants documentation. #[serde(rename = "seasonId")] - pub season_id: i32, + pub season_id: crate::consts::Season, /// Please refer to the Game Constants documentation. #[serde(rename = "queueId")] - pub queue_id: i32, + pub queue_id: crate::consts::Queue, #[serde(rename = "gameId")] pub game_id: i64, /// Participant identity information. @@ -316,13 +321,13 @@ pub mod match_v4 { pub platform_id: String, /// Please refer to the Game Constants documentation. #[serde(rename = "gameMode")] - pub game_mode: String, + pub game_mode: crate::consts::GameMode, /// Please refer to the Game Constants documentation. #[serde(rename = "mapId")] - pub map_id: i32, + pub map_id: crate::consts::Map, /// Please refer to the Game Constants documentation. #[serde(rename = "gameType")] - pub game_type: String, + pub game_type: crate::consts::GameType, /// Team information. #[serde(rename = "teams")] pub teams: std::vec::Vec, @@ -401,7 +406,7 @@ pub mod match_v4 { pub first_blood: bool, /// 100 for blue side. 200 for red side. #[serde(rename = "teamId")] - pub team_id: i32, + pub team_id: crate::consts::Team, /// Flag indicating whether or not the team destroyed the first tower. #[serde(rename = "firstTower")] pub first_tower: bool, @@ -434,7 +439,7 @@ pub mod match_v4 { pub pick_turn: i32, /// Banned championId. #[serde(rename = "championId")] - pub champion_id: i32, + pub champion_id: crate::consts::Champion, } /// Participant data object. This struct is automatically generated. #[derive(Debug)] @@ -453,7 +458,7 @@ pub mod match_v4 { pub timeline: ParticipantTimeline, /// 100 for blue side. 200 for red side. #[serde(rename = "teamId")] - pub team_id: i32, + pub team_id: crate::consts::Team, /// Second Summoner Spell id. #[serde(rename = "spell2Id")] pub spell2_id: i32, @@ -463,12 +468,12 @@ pub mod match_v4 { /// Highest ranked tier achieved for the previous season in a specific subset of queueIds, if any, otherwise null. Used to display border in game loading screen. Please refer to the Ranked Info documentation. /// (Legal values: CHALLENGER, MASTER, DIAMOND, PLATINUM, GOLD, SILVER, BRONZE, UNRANKED) #[serde(rename = "highestAchievedSeasonTier")] - pub highest_achieved_season_tier: String, + pub highest_achieved_season_tier: crate::consts::Tier, /// First Summoner Spell id. #[serde(rename = "spell1Id")] pub spell1_id: i32, #[serde(rename = "championId")] - pub champion_id: i32, + pub champion_id: crate::consts::Champion, } /// ParticipantStats data object. This struct is automatically generated. #[derive(Debug)] @@ -793,13 +798,13 @@ pub mod match_v4 { #[serde(rename = "gameId")] pub game_id: i64, #[serde(rename = "champion")] - pub champion: i32, + pub champion: crate::consts::Champion, #[serde(rename = "platformId")] pub platform_id: String, #[serde(rename = "season")] pub season: i32, #[serde(rename = "queue")] - pub queue: i32, + pub queue: crate::consts::Queue, #[serde(rename = "role")] pub role: String, #[serde(rename = "timestamp")] @@ -868,7 +873,7 @@ pub mod match_v4 { #[serde(rename = "towerType")] pub tower_type: String, #[serde(rename = "teamId")] - pub team_id: i32, + pub team_id: crate::consts::Team, #[serde(rename = "ascendedType")] pub ascended_type: String, #[serde(rename = "killerId")] @@ -931,13 +936,13 @@ pub mod spectator_v4 { pub platform_id: String, /// The game mode #[serde(rename = "gameMode")] - pub game_mode: String, + pub game_mode: crate::consts::GameMode, /// The ID of the map #[serde(rename = "mapId")] - pub map_id: i64, + pub map_id: crate::consts::Map, /// The game type #[serde(rename = "gameType")] - pub game_type: String, + pub game_type: crate::consts::GameType, /// Banned champion information #[serde(rename = "bannedChampions")] pub banned_champions: std::vec::Vec, @@ -952,7 +957,7 @@ pub mod spectator_v4 { pub game_length: i64, /// The queue type (queue types are documented on the Game Constants page) #[serde(rename = "gameQueueConfigId")] - pub game_queue_config_id: i64, + pub game_queue_config_id: crate::consts::Queue, } /// BannedChampion data object. This struct is automatically generated. #[derive(Debug)] @@ -963,10 +968,10 @@ pub mod spectator_v4 { pub pick_turn: i32, /// The ID of the banned champion #[serde(rename = "championId")] - pub champion_id: i64, + pub champion_id: crate::consts::Champion, /// The ID of the team that banned the champion #[serde(rename = "teamId")] - pub team_id: i64, + pub team_id: crate::consts::Team, } /// Observer data object. This struct is automatically generated. #[derive(Debug)] @@ -985,7 +990,7 @@ pub mod spectator_v4 { pub profile_icon_id: i64, /// The ID of the champion played by this participant #[serde(rename = "championId")] - pub champion_id: i64, + pub champion_id: crate::consts::Champion, /// The summoner name of this participant #[serde(rename = "summonerName")] pub summoner_name: String, @@ -1003,7 +1008,7 @@ pub mod spectator_v4 { pub spell2_id: i64, /// The team ID of this participant, indicating the participant's team #[serde(rename = "teamId")] - pub team_id: i64, + pub team_id: crate::consts::Team, /// The ID of the first summoner spell used by this participant #[serde(rename = "spell1Id")] pub spell1_id: i64, @@ -1063,14 +1068,14 @@ pub mod spectator_v4 { /// The game mode /// (Legal values: CLASSIC, ODIN, ARAM, TUTORIAL, ONEFORALL, ASCENSION, FIRSTBLOOD, KINGPORO) #[serde(rename = "gameMode")] - pub game_mode: String, + pub game_mode: crate::consts::GameMode, /// The ID of the map #[serde(rename = "mapId")] - pub map_id: i64, + pub map_id: crate::consts::Map, /// The game type /// (Legal values: CUSTOM_GAME, MATCHED_GAME, TUTORIAL_GAME) #[serde(rename = "gameType")] - pub game_type: String, + pub game_type: crate::consts::GameType, /// Banned champion information #[serde(rename = "bannedChampions")] pub banned_champions: std::vec::Vec, @@ -1085,7 +1090,7 @@ pub mod spectator_v4 { pub game_length: i64, /// The queue type (queue types are documented on the Game Constants page) #[serde(rename = "gameQueueConfigId")] - pub game_queue_config_id: i64, + pub game_queue_config_id: crate::consts::Queue, } /// Participant data object. This struct is automatically generated. #[derive(Debug)] @@ -1096,7 +1101,7 @@ pub mod spectator_v4 { pub profile_icon_id: i64, /// The ID of the champion played by this participant #[serde(rename = "championId")] - pub champion_id: i64, + pub champion_id: crate::consts::Champion, /// The summoner name of this participant #[serde(rename = "summonerName")] pub summoner_name: String, @@ -1108,7 +1113,7 @@ pub mod spectator_v4 { pub spell2_id: i64, /// The team ID of this participant, indicating the participant's team #[serde(rename = "teamId")] - pub team_id: i64, + pub team_id: crate::consts::Team, /// The ID of the first summoner spell used by this participant #[serde(rename = "spell1Id")] pub spell1_id: i64, diff --git a/srcgen/consts/champion.rs.dt b/srcgen/consts/champion.rs.dt index f4383ba..dd69e5b 100644 --- a/srcgen/consts/champion.rs.dt +++ b/srcgen/consts/champion.rs.dt @@ -14,15 +14,21 @@ }}{{= require('./dotUtils.js').preamble() }} use std::fmt; -use num_derive; +use num_derive::{ FromPrimitive, ToPrimitive }; +use serde_repr::{ Serialize_repr, Deserialize_repr }; /// League of Legend's champions. /// /// The documentation of each variant specifies:
/// NAME (`IDENTIFIER`, ID). #[derive(Debug, Copy, Clone)] -#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive)] +#[derive(FromPrimitive, ToPrimitive)] +#[derive(Serialize_repr, Deserialize_repr)] +#[repr(i16)] pub enum Champion { + /// A champion that doesn't exist. Used in TeamBans when no ban occured. + None = -1, + {{ for (let { id, alias, name } of champions) { const comment = `${name.padEnd(14)} (\`${alias}\`, ${id}).`.padEnd(36); @@ -37,6 +43,7 @@ pub enum Champion { impl Champion { pub fn name(self) -> &'static str { match self { + Self::None => "None", {{ for (let { id, name } of champions) { }} @@ -49,6 +56,7 @@ impl Champion { pub fn identifier(self) -> &'static str { match self { + Self::None => "None", {{ for (let { name, alias } of champions) { }} diff --git a/srcgen/consts/game_mode.rs.dt b/srcgen/consts/game_mode.rs.dt index f809515..435de97 100644 --- a/srcgen/consts/game_mode.rs.dt +++ b/srcgen/consts/game_mode.rs.dt @@ -22,3 +22,5 @@ pub enum GameMode { } }} } + +serde_string!(GameMode); diff --git a/srcgen/consts/game_type.rs.dt b/srcgen/consts/game_type.rs.dt index c8ea9aa..f86274b 100644 --- a/srcgen/consts/game_type.rs.dt +++ b/srcgen/consts/game_type.rs.dt @@ -21,3 +21,5 @@ pub enum GameType { } }} } + +serde_string!(GameType); diff --git a/srcgen/consts/map.rs.dt b/srcgen/consts/map.rs.dt index a9a57e7..271087d 100644 --- a/srcgen/consts/map.rs.dt +++ b/srcgen/consts/map.rs.dt @@ -5,9 +5,12 @@ dotUtils.changeCase.pascalCase(mapName.replace(/[ ']+/, ''))); }}{{= dotUtils.preamble() }} +use serde_repr::{ Serialize_repr, Deserialize_repr }; + /// League of Legends maps. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] +#[derive(Serialize_repr, Deserialize_repr)] #[repr(u8)] pub enum Map { {{ diff --git a/srcgen/consts/season.rs.dt b/srcgen/consts/season.rs.dt index 08aedbb..42bd04e 100644 --- a/srcgen/consts/season.rs.dt +++ b/srcgen/consts/season.rs.dt @@ -3,9 +3,12 @@ const seasons = require('./.seasons.json'); }}{{= dotUtils.preamble() }} +use serde_repr::{ Serialize_repr, Deserialize_repr }; + /// League of Legends matchmaking seasons. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] +#[derive(Serialize_repr, Deserialize_repr)] #[repr(u8)] pub enum Season { {{ diff --git a/srcgen/dotUtils.js b/srcgen/dotUtils.js index 700f9cb..89ac6d8 100644 --- a/srcgen/dotUtils.js +++ b/srcgen/dotUtils.js @@ -14,8 +14,14 @@ Array.prototype.groupBy = function(lambda) { } function preamble() { - return `// This file is automatically generated. -// Do not directly edit.`; + return `\ +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // +///////////////////////////////////////////////`; } function capitalize(input) { @@ -41,12 +47,6 @@ function normalizePropName(propName, schemaName, value) { if ('type' === out) return 'r#' + out; return out; - // return propName; - // var tokens = propName.split('_'); - // var name = tokens.map(capitalize).join(''); - // if (name === schemaName) - // name += stringifyType(value); - // return name; } function stringifyType(prop, endpoint = null, optional = false, fullpath = true, owned = true) { @@ -54,6 +54,10 @@ function stringifyType(prop, endpoint = null, optional = false, fullpath = true, prop = prop.anyOf[0]; } + let enumType = prop['x-enum']; + if (enumType && 'locale' !== enumType) + return 'crate::consts::' + changeCase.pascalCase(enumType); + let refType = prop['$ref']; if (refType) { return (!endpoint ? '' : changeCase.snakeCase(endpoint) + '::') +