From 73117465dd82b30de3bb91b9e9885bf46f7f8de2 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 31 May 2019 02:45:55 +0200 Subject: implement FromStr for Boss --- src/statistics/gamedata.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'src/statistics') diff --git a/src/statistics/gamedata.rs b/src/statistics/gamedata.rs index 63d77eb..400f070 100644 --- a/src/statistics/gamedata.rs +++ b/src/statistics/gamedata.rs @@ -1,9 +1,10 @@ //! This module contains some game data that is necessary to correctly calculate //! some statistics. +use std::{fmt, str::FromStr}; use super::boon::{BoonQueue, BoonType}; /// Enum containing all bosses with their IDs. -#[derive(Debug, Clone, PartialEq, Eq, Hash, FromPrimitive)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, FromPrimitive)] pub enum Boss { // Wing 1 ValeGuardian = 0x3C4E, @@ -49,6 +50,61 @@ pub enum Boss { Ensolyss = 0x4234, } + +/// Error for when converting a string to the boss fails. +#[derive(Debug, Clone, Hash, PartialEq, Eq)] +pub struct ParseBossError(String); + + +impl fmt::Display for ParseBossError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Invalid boss identifier: {}", self.0) + } +} + + +impl FromStr for Boss { + type Err = ParseBossError; + + fn from_str(s: &str) -> Result { + let lower = s.to_lowercase(); + match &lower as &str { + "vg" | "vale guardian" => Ok(Boss::ValeGuardian), + "gorse" | "gorseval" => Ok(Boss::Gorseval), + "sab" | "sabetha" => Ok(Boss::Sabetha), + + "sloth" | "slothasor" => Ok(Boss::Slothasor), + "matthias" => Ok(Boss::Matthias), + + "kc" | "keep construct" => Ok(Boss::KeepConstruct), + "xera" => Ok(Boss::Xera), + + "cairn" => Ok(Boss::Cairn), + "mo" | "mursaat overseer" => Ok(Boss::MursaatOverseer), + "sam" | "sama" | "samarog" => Ok(Boss::Samarog), + "deimos" => Ok(Boss::Deimos), + + "desmina" | "sh" => Ok(Boss::SoullessHorror), + "dhuum" => Ok(Boss::Dhuum), + + "ca" | "conjured almagamate" => Ok(Boss::ConjuredAmalgamate), + "largos" | "twins" => Ok(Boss::LargosTwins), + "qadim" => Ok(Boss::Qadim), + + "skorvald" => Ok(Boss::Skorvald), + "artsariiv" => Ok(Boss::Artsariiv), + "arkk" => Ok(Boss::Arkk), + + "mama" => Ok(Boss::MAMA), + "siax" => Ok(Boss::Siax), + "ensolyss" => Ok(Boss::Ensolyss), + + _ => Err(ParseBossError(s.to_owned())) + } + } +} + + /// ID for Xera in the second phase. /// /// The original Xera will despawn, and after the tower phase, a separate spawn @@ -199,3 +255,17 @@ pub static MECHANICS: &[Mechanic] = mechanics! { pub fn get_mechanics(boss_id: u16) -> Vec<&'static Mechanic> { MECHANICS.iter().filter(|m| m.0 == boss_id).collect() } + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + pub fn test_parsing() { + assert_eq!("vg".parse(), Ok(Boss::ValeGuardian)); + assert_eq!("VG".parse(), Ok(Boss::ValeGuardian)); + + assert!("vga".parse::().is_err()); + } +} -- cgit v1.2.3