From 522aad7889cd62e96af7c420789507ccbf5b7aaa Mon Sep 17 00:00:00 2001 From: troido Date: Sat, 22 Feb 2020 23:41:51 +0100 Subject: it is now possible to use items --- src/components/item.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/components/item.rs') 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 { + 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} + }) + } +} -- cgit