Convert Champion enum into newtype

pull/27/head
Mingwei Samuel 2021-06-30 16:29:39 -07:00
parent 569a5bbc1b
commit 2989c4483e
3 changed files with 1123 additions and 532 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,83 +3,156 @@
const champions = require('./.champion.json') const champions = require('./.champion.json')
.filter(({ id }) => id > 0) .filter(({ id }) => id > 0)
.sortBy(({ name }) => name); .sortBy(({ name }) => name);
const constName = name => dotUtils.changeCase.constantCase(name).replace(/[^_A-Z0-9]+/g, '');
const constNamePad = 12;
const hashFactor = 256; const hashFactor = 256;
const enumName = name => name.replace(/[^a-z]+/i, ''); const strHash = (str) => {
const strHash = function(str) {
let h = 0; let h = 0;
for (let c of str) for (let c of str)
h = hashFactor * h + c.charCodeAt(0); h = hashFactor * h + c.charCodeAt(0);
return h; return h;
}; };
const padId = function(id) { return ('' + id).padEnd(3); };
}}{{= dotUtils.preamble() }} }}{{= dotUtils.preamble() }}
use num_enum::{ IntoPrimitive, TryFromPrimitive }; use serde::{ Serialize, Deserialize };
use serde_repr::{ Serialize_repr, Deserialize_repr };
use strum_macros::{ EnumString, EnumIter, Display, AsRefStr, IntoStaticStr };
/// League of Legend's champions. /// League of Legends champions.
/// ///
/// The documentation of each variant specifies:<br> /// The documentation of each const field specifies:<br>
/// NAME (`IDENTIFIER`, ID). /// NAME (`IDENTIFIER`, ID).
/// ///
/// Implements [IntoEnumIterator](super::IntoEnumIterator). /// Implements [IntoEnumIterator](super::IntoEnumIterator).
#[non_exhaustive] #[derive(Serialize, Deserialize)]
#[derive(Debug, Copy, Clone)] #[derive(Copy, Clone)]
#[derive(IntoPrimitive, TryFromPrimitive)] #[derive(PartialEq, Eq, PartialOrd, Ord)]
#[derive(Serialize_repr, Deserialize_repr)] #[serde(transparent)]
#[derive(EnumString, EnumIter, Display, AsRefStr, IntoStaticStr)] #[repr(transparent)]
#[repr(i16)] pub struct Champion(pub i16);
pub enum Champion {
/// A champion that doesn't exist. Used in TeamBans when no champion was banned.
///
/// None (`NONE`, -1).
None = -1,
impl Champion {
{{ {{
for (let { id, alias, name } of champions) { for (let { id, alias, name } of champions) {
}} }}
/// {{= name }} (`{{= alias }}`, {{= id }}). /// {{= name }} (`{{= alias }}`, {{= id }}).
#[strum(to_string="{{= name }}"{{? name !== alias }}, serialize="{{= alias }}"{{?}})] {{= enumName(name) }} = {{= id }}, pub const {{= constName(name) }}: Self = Self({{= id }});
{{ {{
} }
}} }}
}
impl Champion { pub const fn is_known(self) -> bool {
/// The champion's name (localized `en_US`), or `"NONE"` for the None variant. match self {
pub fn name(self) -> &'static str { {{
self.into() for (let { name, alias } of champions) {
}}
Self::{{= constName(name).padEnd(constNamePad) }} => true,
{{
}
}}
_ => false,
}
}
/// The champion's name (`en_US` localization).
pub const fn name(self) -> Option<&'static str> {
match self {
{{
for (let { name } of champions) {
}}
Self::{{= constName(name).padEnd(constNamePad) }} => Some("{{= name }}"),
{{
}
}}
_ => None,
}
} }
/// The champion's identifier key. Somtimes called "key", "identifier", or "alias". /// The champion's identifier key. Somtimes called "key", "identifier", or "alias".
/// This is mainly used in DDragon paths. /// This is mainly used in DDragon paths.
/// ///
/// This is generally the `en_US` name with spaces and punctuation removed, /// This is generally the `en_US` name with spaces and punctuation removed,
/// but there are the following exceptions: /// capitalization preserved, however the follow are exceptions:
/// ///
/// Variant | Name | Identifier /// Field | Name | Identifier
/// --------|------|----------- /// --------|------|-----------
/// `None` | "NONE" | "NONE"
{{ {{
for (let { name, alias } of champions) { for (let { name, alias } of champions) {
if (name.replace(/[^a-zA-Z0-9]+/, '') !== alias) { if (name.replace(/[^a-zA-Z0-9]+/, '') !== alias) {
}} }}
/// `{{= enumName(name) }}` | "{{= name }}" | "{{= alias }}" /// `{{= constName(name) }}` | "{{= name }}" | "{{= alias }}"
{{ {{
} }
} }
}} }}
pub fn identifier(self) -> &'static str { pub const fn identifier(self) -> Option<&'static str> {
match self { match self {
Self::None => "NONE",
{{ {{
for (let { name, alias } of champions) { for (let { name, alias } of champions) {
}} }}
Self::{{= enumName(name).padEnd(12) }} => "{{= alias }}", Self::{{= constName(name).padEnd(constNamePad) }} => Some("{{= alias }}"),
{{ {{
} }
}} }}
_ => None,
} }
} }
} }
impl std::str::FromStr for Champion {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.chars()
.take(4)
.filter(|c| c.is_ascii_alphanumeric())
.fold(0_u32, |hash, next| hash * {{= hashFactor }} + u32::from(next))
{
{{
const keyStrings = (name, alias) => new Set([].concat(...[ name, alias ].map(s => s.toUpperCase())
.map(s => [
s.replace(/[^A-Z0-9]+/, '').substring(0, 4),
s.split(/[^A-Z0-9]/, 1)[0].substring(0, 4),
s.split(/[^A-Z]/, 1)[0].substring(0, 4),
])));
for (const { id, alias, name } of champions) {
for (const prefix of keyStrings(name, alias)) {
}}
0x{{= strHash(prefix).toString(16).padEnd(8) }} /* {{= prefix.padEnd(4) }} */ => Ok(Champion::{{= constName(name) }}),
{{
}
}
}}
_ => Err(()),
}
}
}
impl std::convert::TryFrom<&str> for Champion {
type Error = <Self as std::str::FromStr>::Err;
fn try_from(value: &str) -> Result<Self, Self::Error> {
<Self as std::str::FromStr>::from_str(value)
}
}
impl std::convert::From<i16> for Champion {
fn from(value: i16) -> Self {
Self(value)
}
}
impl std::convert::From<Champion> for i16 {
fn from(value: Champion) -> Self {
value.0
}
}
impl std::fmt::Display for Champion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::fmt::Debug for Champion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Champion({} {})", self.0, self.identifier().unwrap_or("UNKNOWN"))
}
}

View File

@ -59,7 +59,7 @@ async_tests!{
matchlist_get2: async { matchlist_get2: async {
let sp = RIOT_API.summoner_v4().get_by_summoner_name(ROUTE, "haha yes"); let sp = RIOT_API.summoner_v4().get_by_summoner_name(ROUTE, "haha yes");
let s = sp.await.map_err(|e| e.to_string())?.ok_or("Failed to get \"haha yes\"".to_owned())?; let s = sp.await.map_err(|e| e.to_string())?.ok_or("Failed to get \"haha yes\"".to_owned())?;
let mp = RIOT_API.match_v4().get_matchlist(ROUTE, &s.account_id, None, None, Some(&[ Champion::Sion, Champion::Sivir, Champion::Cassiopeia ]), None, None, None, None); let mp = RIOT_API.match_v4().get_matchlist(ROUTE, &s.account_id, None, None, Some(&[ Champion::SION, Champion::SIVIR, Champion::CASSIOPEIA ]), None, None, None, None);
let m = mp.await.map_err(|e| e.to_string())?.ok_or("Failed to get matchlist".to_owned())?; let m = mp.await.map_err(|e| e.to_string())?.ok_or("Failed to get matchlist".to_owned())?;
rassert!(m.matches.len() > 0, "Matchlist should not be empty"); rassert!(m.matches.len() > 0, "Matchlist should not be empty");
Ok(()) Ok(())