diff options
| author | troido <troido@protonmail.com> | 2020-09-25 08:54:20 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-09-25 08:54:20 +0200 |
| commit | 09306cb76c6e1eabb4082a985a0a0fa335bda5c1 (patch) | |
| tree | e43abd096374a8e79186b519d80372112ab0ca74 /src/components/equipment.rs | |
| parent | 9eb3a9da97e53cee14e585e027badb3783b8e25e (diff) | |
proper serialisation for playerstate; strum for old-style enums
Diffstat (limited to 'src/components/equipment.rs')
| -rw-r--r-- | src/components/equipment.rs | 52 |
1 files changed, 17 insertions, 35 deletions
diff --git a/src/components/equipment.rs b/src/components/equipment.rs index cbd482c..929a035 100644 --- a/src/components/equipment.rs +++ b/src/components/equipment.rs @@ -5,50 +5,31 @@ use specs::{ Component, HashMapStorage }; +use strum_macros::{EnumString, Display}; use crate::{ Sprite }; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, EnumString, Display)] +#[serde(rename_all = "lowercase")] +#[strum(serialize_all = "snake_case")] pub enum Slot { Hand, Body, Back } -impl Slot { - pub fn from_str(txt: &str) -> Option<Self> { - match txt { - "hand" => Some(Self::Hand), - "body" => Some(Self::Body), - "back" => Some(Self::Back), - _ => None - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] -#[serde(rename_all = "lowercase")] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, EnumString, Display)] +#[serde(rename_all = "lowercase")] +#[strum(serialize_all = "snake_case")] pub enum Stat { Strength, Defence, Mining } -impl Stat { - pub fn from_str(txt: &str) -> Option<Self> { - match txt { - "strength" => Some(Self::Strength), - "defence" => Some(Self::Defence), - "mining" => Some(Self::Mining), - _ => None - } - } -} - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Equippable { @@ -70,23 +51,24 @@ mod tests { use super::*; use crate::hashmap; use serde_json::json; + use std::str::FromStr; #[test] fn slots() { - assert_eq!(Slot::from_str("hand"), Some(Slot::Hand)); - assert_eq!(Slot::from_str("body"), Some(Slot::Body)); - assert_eq!(Slot::from_str("hands"), None); - assert_eq!(Slot::from_str("head"), None); + assert_eq!(Slot::from_str("hand"), Ok(Slot::Hand)); + assert_eq!(Slot::from_str("body"), Ok(Slot::Body)); + assert!(Slot::from_str("hands").is_err()); + assert!(Slot::from_str("head").is_err()); } #[test] fn stats() { - assert_eq!(Stat::from_str("strength"), Some(Stat::Strength)); - assert_eq!(Stat::from_str("defence"), Some(Stat::Defence)); - assert_eq!(Stat::from_str("hand"), None); - assert_eq!(Stat::from_str("body"), None); - assert_eq!(Stat::from_str("attack"), None); + assert_eq!(Stat::from_str("strength"), Ok(Stat::Strength)); + assert_eq!(Stat::from_str("defence"), Ok(Stat::Defence)); + assert!(Stat::from_str("hand").is_err()); + assert!(Stat::from_str("body").is_err()); + assert!(Stat::from_str("attack").is_err()); } #[test] |
