diff options
| author | troido <troido@protonmail.com> | 2020-05-19 15:06:45 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-05-19 15:06:45 +0200 |
| commit | dac89209fdde17e2e4fdf89768e814945a8cea62 (patch) | |
| tree | 8713ca53fd85b1c88a9263197fa7306766747e19 /src/components | |
| parent | 0d382ea19f8f964c35761f6a3ff80bc9bfc25375 (diff) | |
better json parsing using serde_json::value::from_value
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/interactable.rs | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/components/interactable.rs b/src/components/interactable.rs index 757e27e..d97b742 100644 --- a/src/components/interactable.rs +++ b/src/components/interactable.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use serde_json; use serde_json::{Value}; use specs::{ Component, @@ -33,20 +34,17 @@ impl Interactable { "trigger" => Trigger(Trigger::from_str(arg.as_str()?)?), "say" => Say(arg.as_str()?.to_string()), "reply" => Reply(arg.as_str()?.to_string()), - "exchange" => Exchange( - arg.get(0)?.as_str()?.to_string(), - arg.get(1)? - .as_object()? - .iter() - .map(|(id, ex)| { - let exchange = Exchange { - cost: ex.get(0)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::<Option<Vec<ItemId>>>()?, - offer: ex.get(1)?.as_array()?.iter().map(|i| Some(ItemId(i.as_str()?.to_string()))).collect::<Option<Vec<ItemId>>>()? - }; - Some((id.clone(), exchange)) - }) - .collect::<Option<HashMap<String, Exchange>>>()? - ), + "exchange" => { + let (prefix, change) = serde_json::value::from_value::< + (String, HashMap<String, (Vec<ItemId>, Vec<ItemId>)>) + >(arg.clone()).ok()?; + Exchange( + prefix, + change.into_iter().map( + |(id, (cost, offer))| (id, Exchange{cost, offer}) + ).collect::<HashMap<String, Exchange>>() + ) + }, "visit" => Visit(RoomId::from_str(arg.as_str()?)), "mine" => Mine(Stat::from_str(arg.as_str()?)?), _ => None? |
