reorganizing modules, sorting enums

pull/5/head
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 serde_repr::{ Serialize_repr, Deserialize_repr };
/// League of Legend's champions.
/// League of Legends champions.
///
/// The documentation of each variant specifies:<br>
/// NAME (`IDENTIFIER`, ID).

View File

@ -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<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 };
/// LoL or TFT ranked queue types.
#[derive(Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash)]
#[derive(EnumString, Display, AsRefStr)]

View File

@ -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());
}
}

View File

@ -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::*;

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 BURST_FACTOR_DENOM: f32 = 256.0;
}
use riot_api_config::*;
/// Configuration for instantiating RiotApi.
#[derive(Debug, PartialEq, Eq)]

View File

@ -1,8 +1,12 @@
use std::error::Error;
use std::error::Error as StdError;
use std::fmt;
pub use reqwest::Error as ReqwestError;
/// 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<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.
///
/// 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<Response>,
}
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 {
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)
}
}