summaryrefslogtreecommitdiff
path: root/src/components/item.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/item.rs')
-rw-r--r--src/components/item.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/components/item.rs b/src/components/item.rs
index 76c5c1c..bcc672a 100644
--- a/src/components/item.rs
+++ b/src/components/item.rs
@@ -9,10 +9,36 @@ pub struct Item {
pub action: ItemAction
}
-#[derive(Debug, Clone)]
+
+
+use serde_json::{json, Value};
+
+#[derive(Debug, Clone, PartialEq)]
pub enum ItemAction {
- Eat{health: i64},
+ Eat(i64),
Build(Template),
None
}
+use ItemAction::{Eat, Build, None};
+
+impl ItemAction {
+ pub fn to_json(&self) -> Value {
+ match self {
+ Eat(health) => json!(["eat", health]),
+ Build(template) => json!(["build", template.to_json()]),
+ None => json!(["none", null])
+ }
+ }
+
+ pub fn from_json(val: &Value) -> Option<Self> {
+ let typ = val.get(0)?;
+ let arg = val.get(1)?;
+ Some(match typ.as_str()? {
+ "eat" => Eat(arg.as_i64()?),
+ "build" => Build(Template::from_json(arg).ok()?),
+ "none" => None,
+ _ => {return Option::None}
+ })
+ }
+}