diff options
| author | troido <troido@protonmail.com> | 2020-09-24 13:10:54 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-09-24 13:10:54 +0200 |
| commit | e6476e7afff0234d67ebce7c74e4c91c31d87755 (patch) | |
| tree | 638133c0f7c9253e863e01806150f8bec860d12e /src/roomtemplate.rs | |
| parent | 523a635dc88bc7890a0e1d3c062a8165259761d6 (diff) | |
actually use the serde serialisation more
Diffstat (limited to 'src/roomtemplate.rs')
| -rw-r--r-- | src/roomtemplate.rs | 58 |
1 files changed, 0 insertions, 58 deletions
diff --git a/src/roomtemplate.rs b/src/roomtemplate.rs index 7110e64..7f25291 100644 --- a/src/roomtemplate.rs +++ b/src/roomtemplate.rs @@ -1,12 +1,9 @@ use std::collections::HashMap; -use serde_json::{json, Value, value}; use serde::{Deserialize, Deserializer, de, Serialize}; use crate::{ Pos, Template, - PResult, - perr, resources::RoomPermissions }; @@ -65,61 +62,6 @@ impl<'de> Deserialize<'de> for RoomTemplate { } } -impl RoomTemplate { - - pub fn from_json(jsonroom: &Value) -> PResult<RoomTemplate>{ - let size = ( - jsonroom.get("width").ok_or(perr!("no width"))?.as_i64().ok_or(perr!("width not a number"))?, - jsonroom.get("height").ok_or(perr!("no height"))?.as_i64().ok_or(perr!("height not a number"))? - ); - let spawn = Pos::from_json(jsonroom.get("spawn").ok_or(perr!("no spawn"))?).ok_or(perr!("spawn not a pos"))?; - - let mut mapping = HashMap::new(); - for (key, value) in jsonroom.get("mapping").ok_or(perr!("no mapping"))?.as_object().ok_or(perr!("mapping not a json object"))?.iter() { - let mut templates: Vec<Template> = Vec::new(); - if value.is_array() { - for template in value.as_array().unwrap() { - templates.push(Template::from_json(template)?); - } - } else { - templates.push(Template::from_json(value)?); - } - mapping.insert(key.chars().next().ok_or(perr!("mapping key is empty string"))?, templates); - } - - let width = size.0 as usize; - let height = size.1 as usize; - let mut field = Vec::new(); - field.resize_with(width * height, Vec::new); - let jsonfield: &Vec<Value> = jsonroom.get("field").ok_or(perr!("no field"))?.as_array().ok_or(perr!("field not an array"))?; - for (y, row) in jsonfield.iter().take(height).enumerate() { - for (x, ch) in row.as_str().ok_or(perr!("field row not a string"))?.chars().take(width).enumerate() { - field[x + y * width] = mapping.get(&ch).ok_or(perr!("char not found in mapping"))?.clone(); - } - } - - let mut places = HashMap::new(); - for (name, jsonpos) in jsonroom.get("places").unwrap_or(&json!({})).as_object().ok_or(perr!("places not an object"))? { - places.insert(name.to_string(), Pos::from_json(jsonpos).ok_or(perr!("pos of places invalid"))?); - } - - let permissions: RoomPermissions = value::from_value::<RoomPermissions>( - jsonroom - .get("permissions") - .unwrap_or(&json!({})) - .clone() - ).map_err(|e| perr!("can't deserialise permissions: {:?}", e))?; - - Ok(RoomTemplate { - size, - spawn, - field, - places, - permissions - }) - } -} - #[cfg(test)] mod tests { use super::*; |
