From a7057e8351a1a058533c41784be81c259aa2e238 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 17 Nov 2021 20:40:16 +0100 Subject: Implement FromStr for GameMode This may be useful for downstream applications and it fits into the pattern of implementing it for Boss/Encounter. --- src/gamedata.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/gamedata.rs') diff --git a/src/gamedata.rs b/src/gamedata.rs index a07ac05..bbe6e74 100644 --- a/src/gamedata.rs +++ b/src/gamedata.rs @@ -26,6 +26,28 @@ pub enum GameMode { WvW, } +/// Error for when converting a string to a game mode fails. +#[derive(Debug, Clone, Hash, PartialEq, Eq, Error)] +#[error("Invalid encounter identifier: {0}")] +pub struct ParseGameModeError(String); + +impl FromStr for GameMode { + type Err = ParseGameModeError; + + fn from_str(s: &str) -> Result { + let lower = s.to_lowercase(); + match &lower as &str { + "raid" => Ok(GameMode::Raid), + "fractal" => Ok(GameMode::Fractal), + "strike" => Ok(GameMode::Strike), + "golem" => Ok(GameMode::Golem), + "wvw" => Ok(GameMode::WvW), + + _ => Err(ParseGameModeError(s.to_owned())), + } + } +} + /// Enum containing all encounters with their IDs. /// /// An encounter is a fight or event for which a log can exist. An encounter consists of no, one or @@ -868,6 +890,32 @@ impl EliteSpec { mod tests { use super::*; + #[test] + fn test_gamemode_parsing_ok() { + use GameMode::*; + let tests: &[(&'static str, GameMode)] = &[ + ("raid", Raid), + ("Raid", Raid), + ("fractal", Fractal), + ("Fractal", Fractal), + ("strike", Strike), + ("Strike", Strike), + ("golem", Golem), + ("Golem", Golem), + ("wvw", WvW), + ("WvW", WvW), + ]; + + for (input, expected) in tests { + assert_eq!( + input.parse(), + Ok(*expected), + "parsing input {:?} failed", + input + ); + } + } + #[test] fn test_encounter_parsing_ok() { use Encounter::*; -- cgit v1.2.3