From 9f4a4eaa06f3d0136de9088c0e60a0c077248c91 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 20 Dec 2019 18:04:24 +0100 Subject: remove hard coded palette IDs Now that the API actually returns the proper palette IDs, we can use those values instead of relying on the hard coded values. This also gets rid of the make_table script that was mostly hackish anyway, and the lazy static HashMap. --- src/api/professions.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'src/api/professions.rs') diff --git a/src/api/professions.rs b/src/api/professions.rs index 2716a1a..f3d1f94 100644 --- a/src/api/professions.rs +++ b/src/api/professions.rs @@ -4,7 +4,7 @@ //! * [Wiki](https://wiki.guildwars2.com/wiki/API:2/professions) use super::HasId; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[derive(Deserialize, Serialize, Debug, Clone)] pub struct Profession { @@ -12,10 +12,32 @@ pub struct Profession { pub id: String, /// The name of the profession. pub name: String, + /// The numeric code of the profession, e.g. for chat links. + pub code: u32, /// List of specialization ids. pub specializations: Vec, /// List of skills. pub skills: Vec, + /// Conversion of palette ID to skill ID. + pub skills_by_palette: Vec, +} + +impl Profession { + /// Resolves a given palette ID to the corresponding skill ID. + pub fn palette_id_to_skill_id(&self, palette_id: u32) -> Option { + self.skills_by_palette + .iter() + .find(|entry| entry.palette_id == palette_id) + .map(|entry| entry.skill_id) + } + + /// Resolves a given skill ID to the corresponding palette ID. + pub fn skill_id_to_palette_id(&self, skill_id: u32) -> Option { + self.skills_by_palette + .iter() + .find(|entry| entry.skill_id == skill_id) + .map(|entry| entry.palette_id) + } } impl HasId for Profession { @@ -34,3 +56,30 @@ pub struct Skill { #[serde(rename = "type")] pub typ: String, } + +#[derive(Debug, Clone)] +pub struct PaletteEntry { + /// The palette ID, as used in the chat link. + /// + /// Note that the actual palette only allows 2 bytes for this number, i.e. an `u16`. To stay + /// consistent with other integers that are handled here however, this struct uses a `u32`. + pub palette_id: u32, + /// The skill ID, as used in the API. + pub skill_id: u32, +} + +impl Serialize for PaletteEntry { + fn serialize(&self, serializer: S) -> Result { + (self.palette_id, self.skill_id).serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for PaletteEntry { + fn deserialize>(deserializer: D) -> Result { + let (palette_id, skill_id) = Deserialize::deserialize(deserializer)?; + Ok(PaletteEntry { + palette_id, + skill_id, + }) + } +} -- cgit v1.2.3