summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-09-25 08:54:20 +0200
committertroido <troido@protonmail.com>2020-09-25 08:54:20 +0200
commit09306cb76c6e1eabb4082a985a0a0fa335bda5c1 (patch)
treee43abd096374a8e79186b519d80372112ab0ca74 /src/components
parent9eb3a9da97e53cee14e585e027badb3783b8e25e (diff)
proper serialisation for playerstate; strum for old-style enums
Diffstat (limited to 'src/components')
-rw-r--r--src/components/equipment.rs52
-rw-r--r--src/components/interactable.rs5
-rw-r--r--src/components/messages.rs18
3 files changed, 23 insertions, 52 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]
diff --git a/src/components/interactable.rs b/src/components/interactable.rs
index 651437c..8141416 100644
--- a/src/components/interactable.rs
+++ b/src/components/interactable.rs
@@ -1,5 +1,6 @@
use std::collections::HashMap;
+use std::str::FromStr;
use specs::{
Component,
HashMapStorage,
@@ -30,9 +31,9 @@ impl Interactable {
pub fn parse_from_parameter(typ: &str, arg: &Parameter) -> Option<Self> {
Some(match (typ, arg) {
- ("trigger", Parameter::String(s)) => Trigger(Trigger::from_str(s)?),
+ ("trigger", Parameter::String(s)) => Trigger(Trigger::from_str(s).ok()?),
("visit", Parameter::String(s)) => Visit(RoomId(s.clone())),
- ("mine", Parameter::String(s)) => Mine(Stat::from_str(s)?),
+ ("mine", Parameter::String(s)) => Mine(Stat::from_str(s).ok()?),
("say", Parameter::String(s)) => Say(s.clone()),
("reply", Parameter::String(s)) => Reply(s.clone()),
("exchange", p) => {
diff --git a/src/components/messages.rs b/src/components/messages.rs
index 5e27cc6..f815ab5 100644
--- a/src/components/messages.rs
+++ b/src/components/messages.rs
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::any::Any;
+use strum_macros::{EnumString, Display};
use specs::{
Component,
DenseVecStorage,
@@ -84,7 +85,8 @@ pub type AttackInbox = Inbox<AttackMessage>;
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumString, Display)]
+#[strum(serialize_all="snake_case")]
pub enum Trigger {
// basic triggers
Loot,
@@ -96,20 +98,6 @@ pub enum Trigger {
Change // Remove + Build
}
-impl Trigger {
- pub fn from_str(txt: &str) -> Option<Self> {
- Some(match txt {
- "loot" => Self::Loot,
- "remove" => Self::Remove,
- "build" => Self::Build,
- "spawn" => Self::Spawn,
- "die" => Self::Die,
- "change" => Self::Change,
- _ => {return None}
- })
- }
-}
-
impl Message for Trigger {}
pub type TriggerBox = Inbox<Trigger>;