diff options
Diffstat (limited to 'src/roomtemplate.rs')
| -rw-r--r-- | src/roomtemplate.rs | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/src/roomtemplate.rs b/src/roomtemplate.rs index b5834e9..da4d3a5 100644 --- a/src/roomtemplate.rs +++ b/src/roomtemplate.rs @@ -4,8 +4,8 @@ use serde_json::{json, Value}; use crate::{ Pos, Template, - Result, - aerr + PResult, + perr }; #[derive(Debug, Clone)] @@ -18,39 +18,39 @@ pub struct RoomTemplate { impl RoomTemplate { - pub fn from_json(jsonroom: &Value) -> Result<RoomTemplate>{ + pub fn from_json(jsonroom: &Value) -> PResult<RoomTemplate>{ let size = ( - jsonroom.get("width").ok_or("no with")?.as_i64().ok_or("with not a number")?, - jsonroom.get("height").ok_or("no height")?.as_i64().ok_or("height not a number")? + jsonroom.get("width").ok_or(perr!("no with"))?.as_i64().ok_or(perr!("with 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("no spawn")?).ok_or("spawn not a pos")?; + 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("no mapping")?.as_object().ok_or("mapping not a json object")?.iter() { + 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().ok_or("weird")? { + 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("mapping key is empty string")?, templates); + mapping.insert(key.chars().next().ok_or(perr!("mapping key is empty string"))?, templates); } let mut field = Vec::new(); field.resize((size.0 * size.1) as usize, Vec::new()); - let jsonfield: &Vec<Value> = jsonroom.get("field").ok_or("no field")?.as_array().ok_or("field not an array")?; + let jsonfield: &Vec<Value> = jsonroom.get("field").ok_or(perr!("no field"))?.as_array().ok_or(perr!("field not an array"))?; // todo: what if size doesn't match actual dimensions for (y, row) in jsonfield.iter().enumerate() { - for (x, ch) in row.as_str().ok_or("field row not a string")?.chars().enumerate() { - field[x + y * (size.0 as usize)] = mapping.get(&ch).ok_or(aerr!("char not found in mapping"))?.clone(); + for (x, ch) in row.as_str().ok_or(perr!("field row not a string"))?.chars().enumerate() { + field[x + y * (size.0 as usize)] = 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("places not an object")? { - places.insert(name.to_string(), Pos::from_json(jsonpos).ok_or("pos of places invalid")?); + 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"))?); } Ok(RoomTemplate { |
