forked from mirror/Riven
1
0
Fork 0

reorganizing modules, sorting enums

users/mingwei/surf
Mingwei Samuel 2019-10-27 01:34:10 -07:00
parent e6d8bc5add
commit 2511734bd5
7 changed files with 96 additions and 37 deletions

View File

@ -10,7 +10,7 @@ use std::fmt;
use num_enum::{ IntoPrimitive, TryFromPrimitive }; use num_enum::{ IntoPrimitive, TryFromPrimitive };
use serde_repr::{ Serialize_repr, Deserialize_repr }; use serde_repr::{ Serialize_repr, Deserialize_repr };
/// League of Legend's champions. /// League of Legends champions.
/// ///
/// The documentation of each variant specifies:<br> /// The documentation of each variant specifies:<br>
/// NAME (`IDENTIFIER`, ID). /// NAME (`IDENTIFIER`, ID).

View File

@ -1,11 +1,18 @@
#![allow(deprecated)] #![allow(deprecated)]
use std::cmp::Ordering;
use strum_macros::{ EnumString, Display, AsRefStr }; use strum_macros::{ EnumString, Display, AsRefStr };
use serde_repr::{ Serialize_repr, Deserialize_repr }; use serde_repr::{ Serialize_repr, Deserialize_repr };
use num_enum::{ IntoPrimitive, TryFromPrimitive }; 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(Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr)] #[derive(EnumString, Display, AsRefStr)]
#[derive(Serialize_repr, Deserialize_repr)] #[derive(Serialize_repr, Deserialize_repr)]
#[derive(IntoPrimitive, TryFromPrimitive)] #[derive(IntoPrimitive, TryFromPrimitive)]
@ -18,3 +25,25 @@ pub enum Division {
#[deprecated(note="Removed for 2019.")] #[deprecated(note="Removed for 2019.")]
V = 5, 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<Ordering> {
Some(self.cmp(other))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sort() {
assert!(Division::IV < Division::I);
}
}

View File

@ -1,5 +1,6 @@
use strum_macros::{ EnumString, Display, AsRefStr }; use strum_macros::{ EnumString, Display, AsRefStr };
/// LoL or TFT ranked queue types.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash)] #[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr)] #[derive(EnumString, Display, AsRefStr)]

View File

@ -3,21 +3,26 @@
use strum_macros::{ EnumString, Display, AsRefStr }; use strum_macros::{ EnumString, Display, AsRefStr };
use num_enum::{ IntoPrimitive, TryFromPrimitive }; 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(Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Eq, PartialEq, Hash, PartialOrd, Ord)]
#[derive(EnumString, Display, AsRefStr)] #[derive(EnumString, Display, AsRefStr)]
#[derive(IntoPrimitive, TryFromPrimitive)] #[derive(IntoPrimitive, TryFromPrimitive)]
#[repr(u8)] #[repr(u8)]
pub enum Tier { pub enum Tier {
#[strum(to_string="IRON")] Iron = 40, IRON = 40,
#[strum(to_string="BRONZE")] Bronze = 60, BRONZE = 60,
#[strum(to_string="SILVER")] Silver = 80, SILVER = 80,
#[strum(to_string="GOLD")] Gold = 100, GOLD = 100,
#[strum(to_string="PLATINUM")] Platinum = 120, PLATINUM = 120,
#[strum(to_string="DIAMOND")] Diamond = 140, DIAMOND = 140,
#[strum(to_string="MASTER")] Master = 180, MASTER = 180,
#[strum(to_string="GRANDMASTER")] Grandmaster = 200, GRANDMASTER = 200,
#[strum(to_string="CHALLENGER")] Challenger = 220, CHALLENGER = 220,
} }
serde_string!(Tier); serde_string!(Tier);
@ -28,12 +33,33 @@ impl Tier {
/// ///
/// Only these tiers are queryable by LeagueV4Endpoints::get_league_entries(...). /// Only these tiers are queryable by LeagueV4Endpoints::get_league_entries(...).
pub fn is_standard_tier(self) -> bool { pub fn is_standard_tier(self) -> bool {
self < Self::Master self < Self::MASTER
} }
/// If this tier is an apex tier. /// If this tier is an apex tier.
/// Master and above. /// Master and above.
pub fn is_apex_tier(self) -> bool { 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());
} }
} }

View File

@ -7,8 +7,8 @@ pub mod consts;
pub mod endpoints; pub mod endpoints;
mod riot_api_config; pub mod riot_api_config;
pub use riot_api_config::*; pub use riot_api_config::RiotApiConfig;
mod riot_api; mod riot_api;
pub use riot_api::*; pub use riot_api::*;

View File

@ -1,9 +1,8 @@
pub mod 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 DEFAULT_BURST_PCT: f32 = 0.93;
pub const BURST_FACTOR_DENOM: f32 = 256.0; 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 for instantiating RiotApi. /// Configuration for instantiating RiotApi.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]

View File

@ -1,8 +1,12 @@
use std::error::Error; use std::error::Error as StdError;
use std::fmt; use std::fmt;
pub use reqwest::Error as ReqwestError; /// Re-exported `reqwest` types.
pub use reqwest::Response; pub mod reqwest {
pub use reqwest::Error;
pub use reqwest::Response;
}
use ::reqwest::*;
/// Result containing RiotApiError on failure. /// Result containing RiotApiError on failure.
pub type Result<T> = std::result::Result<T, RiotApiError>; pub type Result<T> = std::result::Result<T, RiotApiError>;
@ -10,23 +14,23 @@ pub type Result<T> = std::result::Result<T, RiotApiError>;
/// An error that occurred while processing a Riot API request. /// An error that occurred while processing a Riot API request.
/// ///
/// Although Riven may make multiple requests due to retries, this will always /// 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)] #[derive(Debug)]
pub struct RiotApiError { pub struct RiotApiError {
reqwest_error: ReqwestError, reqwest_error: Error,
retries: u8, retries: u8,
response: Option<Response>, response: Option<Response>,
} }
impl RiotApiError { impl RiotApiError {
pub fn new(reqwest_error: ReqwestError, retries: u8, response: Option<Response>) -> Self { pub fn new(reqwest_error: Error, retries: u8, response: Option<Response>) -> Self {
Self { Self {
reqwest_error: reqwest_error, reqwest_error: reqwest_error,
retries: retries, retries: retries,
response: response, response: response,
} }
} }
/// The ReqwestError for the final failed request. /// The reqwest::Error for the final failed request.
pub fn source_reqwest_error(&self) -> &ReqwestError { pub fn source_reqwest_error(&self) -> &Error {
&self.reqwest_error &self.reqwest_error
} }
/// The number of retires attempted. Zero means exactly one request, zero retries. /// The number of retires attempted. Zero means exactly one request, zero retries.
@ -43,8 +47,8 @@ impl fmt::Display for RiotApiError {
write!(f, "{:#?}", self) write!(f, "{:#?}", self)
} }
} }
impl Error for RiotApiError { impl StdError for RiotApiError {
fn source(&self) -> Option<&(dyn Error + 'static)> { fn source(&self) -> Option<&(dyn StdError + 'static)> {
Some(&self.reqwest_error) Some(&self.reqwest_error)
} }
} }