diff options
| author | troido <troido@protonmail.com> | 2020-02-22 23:41:51 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-02-22 23:41:51 +0100 |
| commit | 522aad7889cd62e96af7c420789507ccbf5b7aaa (patch) | |
| tree | bfb3c21d627552bcda6f2743854cee920722fd3b /src/components/item.rs | |
| parent | f4331041e5d906f95063f317852f32f19e6cdf9c (diff) | |
it is now possible to use items
Diffstat (limited to 'src/components/item.rs')
| -rw-r--r-- | src/components/item.rs | 30 |
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} + }) + } +} |
