diff options
author | Daniel Schadt <kingdread@gmx.de> | 2021-11-17 20:40:16 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2021-11-17 20:40:16 +0100 |
commit | a7057e8351a1a058533c41784be81c259aa2e238 (patch) | |
tree | 8ce197720c9fd975288a59a42627443ddf97dd0c /src | |
parent | 70750b3c10aa05e8fe922b5d70e9cfe61253c72a (diff) | |
download | evtclib-a7057e8351a1a058533c41784be81c259aa2e238.tar.gz evtclib-a7057e8351a1a058533c41784be81c259aa2e238.tar.bz2 evtclib-a7057e8351a1a058533c41784be81c259aa2e238.zip |
Implement FromStr for GameMode
This may be useful for downstream applications and it fits into the
pattern of implementing it for Boss/Encounter.
Diffstat (limited to 'src')
-rw-r--r-- | src/gamedata.rs | 48 |
1 files changed, 48 insertions, 0 deletions
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<Self, Self::Err> { + 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 @@ -869,6 +891,32 @@ 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::*; let tests: &[(&'static str, Encounter)] = &[ |