From 2511734bd5844c58bad33e8c6734286242af3284 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Sun, 27 Oct 2019 01:34:10 -0700 Subject: [PATCH] reorganizing modules, sorting enums --- src/consts/champion.rs | 14 ++++++------ src/consts/division.rs | 31 +++++++++++++++++++++++++- src/consts/queue_type.rs | 1 + src/consts/tier.rs | 48 +++++++++++++++++++++++++++++++--------- src/lib.rs | 4 ++-- src/riot_api_config.rs | 11 +++++---- src/riot_api_error.rs | 24 +++++++++++--------- 7 files changed, 96 insertions(+), 37 deletions(-) diff --git a/src/consts/champion.rs b/src/consts/champion.rs index 81bc755..017543a 100644 --- a/src/consts/champion.rs +++ b/src/consts/champion.rs @@ -1,16 +1,16 @@ -/////////////////////////////////////////////// -// // -// ! // -// This file is automatically generated! // -// Do not directly edit! // -// // +/////////////////////////////////////////////// +// // +// ! // +// This file is automatically generated! // +// Do not directly edit! // +// // /////////////////////////////////////////////// use std::fmt; use num_enum::{ IntoPrimitive, TryFromPrimitive }; use serde_repr::{ Serialize_repr, Deserialize_repr }; -/// League of Legend's champions. +/// League of Legends champions. /// /// The documentation of each variant specifies:
/// NAME (`IDENTIFIER`, ID). diff --git a/src/consts/division.rs b/src/consts/division.rs index 941b03d..ec65b3b 100644 --- a/src/consts/division.rs +++ b/src/consts/division.rs @@ -1,11 +1,18 @@ #![allow(deprecated)] +use std::cmp::Ordering; + use strum_macros::{ EnumString, Display, AsRefStr }; use serde_repr::{ Serialize_repr, Deserialize_repr }; use num_enum::{ IntoPrimitive, TryFromPrimitive }; +/// LoL and TFT rank divisions, I, II, III, IV, and (deprecated) V. +/// +/// Sorts in reverse numeric order, from low to high rank. +/// +/// Repr'd as equivalent numeric values, (1, 2, 3, 4, 5). #[derive(Debug, Copy, Clone)] -#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] +#[derive(Eq, PartialEq, Hash)] #[derive(EnumString, Display, AsRefStr)] #[derive(Serialize_repr, Deserialize_repr)] #[derive(IntoPrimitive, TryFromPrimitive)] @@ -18,3 +25,25 @@ pub enum Division { #[deprecated(note="Removed for 2019.")] V = 5, } + +impl Ord for Division { + fn cmp(&self, other: &Self) -> Ordering { + u8::from(*self).cmp(&u8::from(*other)).reverse() + } +} + +impl PartialOrd for Division { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sort() { + assert!(Division::IV < Division::I); + } +} \ No newline at end of file diff --git a/src/consts/queue_type.rs b/src/consts/queue_type.rs index 2d45172..0f8b39e 100644 --- a/src/consts/queue_type.rs +++ b/src/consts/queue_type.rs @@ -1,5 +1,6 @@ use strum_macros::{ EnumString, Display, AsRefStr }; +/// LoL or TFT ranked queue types. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash)] #[derive(EnumString, Display, AsRefStr)] diff --git a/src/consts/tier.rs b/src/consts/tier.rs index 446c24f..b783e3d 100644 --- a/src/consts/tier.rs +++ b/src/consts/tier.rs @@ -3,21 +3,26 @@ use strum_macros::{ EnumString, Display, AsRefStr }; use num_enum::{ IntoPrimitive, TryFromPrimitive }; +/// LoL and TFT ranked tiers, such as gold, diamond, challenger, etc. +/// +/// Sorts from lowest rank to highest rank. +/// +/// Repr'd as arbitrary u8 values. #[derive(Debug, Copy, Clone)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(EnumString, Display, AsRefStr)] #[derive(IntoPrimitive, TryFromPrimitive)] #[repr(u8)] pub enum Tier { - #[strum(to_string="IRON")] Iron = 40, - #[strum(to_string="BRONZE")] Bronze = 60, - #[strum(to_string="SILVER")] Silver = 80, - #[strum(to_string="GOLD")] Gold = 100, - #[strum(to_string="PLATINUM")] Platinum = 120, - #[strum(to_string="DIAMOND")] Diamond = 140, - #[strum(to_string="MASTER")] Master = 180, - #[strum(to_string="GRANDMASTER")] Grandmaster = 200, - #[strum(to_string="CHALLENGER")] Challenger = 220, + IRON = 40, + BRONZE = 60, + SILVER = 80, + GOLD = 100, + PLATINUM = 120, + DIAMOND = 140, + MASTER = 180, + GRANDMASTER = 200, + CHALLENGER = 220, } serde_string!(Tier); @@ -28,12 +33,33 @@ impl Tier { /// /// Only these tiers are queryable by LeagueV4Endpoints::get_league_entries(...). pub fn is_standard_tier(self) -> bool { - self < Self::Master + self < Self::MASTER } /// If this tier is an apex tier. /// Master and above. pub fn is_apex_tier(self) -> bool { - Self::Master <= self + Self::MASTER <= self + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn sort() { + assert!(Tier::GOLD < Tier::DIAMOND); + } + + #[test] + fn to_string() { + assert_eq!("GRANDMASTER", Tier::GRANDMASTER.as_ref()); + assert_eq!("GRANDMASTER", Tier::GRANDMASTER.to_string()); + } + + #[test] + fn from_string() { + assert_eq!(Ok(Tier::GRANDMASTER), "GRANDMASTER".parse()); } } diff --git a/src/lib.rs b/src/lib.rs index 9837e36..4d04b10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,8 +7,8 @@ pub mod consts; pub mod endpoints; -mod riot_api_config; -pub use riot_api_config::*; +pub mod riot_api_config; +pub use riot_api_config::RiotApiConfig; mod riot_api; pub use riot_api::*; diff --git a/src/riot_api_config.rs b/src/riot_api_config.rs index a678578..5593eb1 100644 --- a/src/riot_api_config.rs +++ b/src/riot_api_config.rs @@ -1,9 +1,8 @@ -pub mod riot_api_config { - pub const DEFAULT_BURST_PCT: f32 = 0.93; - pub const DEFAULT_BURST_FACTOR: u8 = ((BURST_FACTOR_DENOM * DEFAULT_BURST_PCT) as u16 - 1) as u8; - pub const BURST_FACTOR_DENOM: f32 = 256.0; -} -use riot_api_config::*; +//! Configuration of RiotApi. + +pub const DEFAULT_BURST_PCT: f32 = 0.93; +pub const DEFAULT_BURST_FACTOR: u8 = ((BURST_FACTOR_DENOM * DEFAULT_BURST_PCT) as u16 - 1) as u8; +pub const BURST_FACTOR_DENOM: f32 = 256.0; /// Configuration for instantiating RiotApi. #[derive(Debug, PartialEq, Eq)] diff --git a/src/riot_api_error.rs b/src/riot_api_error.rs index aaa886b..34ee029 100644 --- a/src/riot_api_error.rs +++ b/src/riot_api_error.rs @@ -1,8 +1,12 @@ -use std::error::Error; +use std::error::Error as StdError; use std::fmt; -pub use reqwest::Error as ReqwestError; -pub use reqwest::Response; +/// Re-exported `reqwest` types. +pub mod reqwest { + pub use reqwest::Error; + pub use reqwest::Response; +} +use ::reqwest::*; /// Result containing RiotApiError on failure. pub type Result = std::result::Result; @@ -10,23 +14,23 @@ pub type Result = std::result::Result; /// An error that occurred while processing a Riot API request. /// /// Although Riven may make multiple requests due to retries, this will always -/// contain exactly one ReqwestError for the final request which failed. +/// contain exactly one reqwest::Error for the final request which failed. #[derive(Debug)] pub struct RiotApiError { - reqwest_error: ReqwestError, + reqwest_error: Error, retries: u8, response: Option, } impl RiotApiError { - pub fn new(reqwest_error: ReqwestError, retries: u8, response: Option) -> Self { + pub fn new(reqwest_error: Error, retries: u8, response: Option) -> Self { Self { reqwest_error: reqwest_error, retries: retries, response: response, } } - /// The ReqwestError for the final failed request. - pub fn source_reqwest_error(&self) -> &ReqwestError { + /// The reqwest::Error for the final failed request. + pub fn source_reqwest_error(&self) -> &Error { &self.reqwest_error } /// The number of retires attempted. Zero means exactly one request, zero retries. @@ -43,8 +47,8 @@ impl fmt::Display for RiotApiError { write!(f, "{:#?}", self) } } -impl Error for RiotApiError { - fn source(&self) -> Option<&(dyn Error + 'static)> { +impl StdError for RiotApiError { + fn source(&self) -> Option<&(dyn StdError + 'static)> { Some(&self.reqwest_error) } }