diff options
| author | troido <troido@protonmail.com> | 2020-04-06 19:28:45 +0200 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-04-06 19:28:45 +0200 |
| commit | 7c351a0c7a497d30f4826a19e6c6e92d3e7b5065 (patch) | |
| tree | 26add4ae11f03b2435c149ef5fe95d66afd60eda | |
| parent | 66a3d3131f32e7bae2f0f7c4fd0b0c876eb3e8a0 (diff) | |
improved error handling
| -rw-r--r-- | src/assemblage.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 17 | ||||
| -rw-r--r-- | src/purgatory.rs | 2 | ||||
| -rw-r--r-- | src/room.rs | 11 | ||||
| -rw-r--r-- | src/systems/spawn.rs | 12 | ||||
| -rw-r--r-- | src/template.rs | 4 | ||||
| -rw-r--r-- | src/world.rs | 2 |
7 files changed, 27 insertions, 23 deletions
diff --git a/src/assemblage.rs b/src/assemblage.rs index e453471..82cb119 100644 --- a/src/assemblage.rs +++ b/src/assemblage.rs @@ -37,7 +37,7 @@ impl Assemblage { ( key.clone(), typ, - Some(Parameter::from_typed_json(typ, def).ok_or(perr!("invalid argument default"))?) + Some(Parameter::from_typed_json(typ, def).ok_or(perr!("invalid argument default {:?} {:?}", typ, def))?) ) ); } else { diff --git a/src/main.rs b/src/main.rs index 8c2c716..b4e8d82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,12 +62,12 @@ use self::{ -fn main() -> Result<()>{ +fn main(){ let config = config::Config::from_args(); let adresses = config.address - .unwrap_or(vec!["abstract:rustifarm".parse()?, "inet:127.0.0.1:1234".parse()?]); + .unwrap_or(vec!["abstract:rustifarm".parse().unwrap(), "inet:127.0.0.1:1234".parse().unwrap()]); println!("adresses: {:?}", adresses); let servers: Vec<Box<dyn Server>> = adresses @@ -90,10 +90,10 @@ fn main() -> Result<()>{ content_dir .join("encyclopediae") .join("default_encyclopedia.json") - )? - )? - )?; - encyclopedia.validate()?; + ).expect("can not load default_encyclopedia.json") + ).expect("default_encyclopedia is invalid json") + ).expect("can not load encyclopedia from json"); + encyclopedia.validate().expect("invalid encyclopedia"); let save_dir = config.save_dir.unwrap_or( FileStorage::default_save_dir().expect("couldn't find any save directory") @@ -127,10 +127,10 @@ fn main() -> Result<()>{ let _ = world.control_player(player, control); } Action::Join(player) => { - world.add_player(&player)?; + world.add_player(&player).expect("can not add player"); } Action::Leave(player) => { - world.remove_player(&player)?; + world.remove_player(&player).expect("can not remove player"); message_cache.remove(&player); } } @@ -156,7 +156,6 @@ fn main() -> Result<()>{ println!("saving world"); world.save(); println!("world saved"); - Ok(()) } diff --git a/src/purgatory.rs b/src/purgatory.rs index e5803d0..2b5a731 100644 --- a/src/purgatory.rs +++ b/src/purgatory.rs @@ -89,6 +89,6 @@ pub fn create_purgatory<'a, 'b>(encyclopedia: &Encyclopedia) -> Room<'a, 'b> { "Y": [{"type": "letter", "args": ["Y"]}], "Z": [{"type": "letter", "args": ["Z"]}] } - })).unwrap()); + })).unwrap()).unwrap(); room } diff --git a/src/room.rs b/src/room.rs index bc5b651..c2e635a 100644 --- a/src/room.rs +++ b/src/room.rs @@ -137,7 +137,7 @@ impl <'a, 'b>Room<'a, 'b> { } } - pub fn load_from_template(&mut self, template: &RoomTemplate) { + pub fn load_from_template(&mut self, template: &RoomTemplate) -> Result<()> { let (width, height) = template.size; self.world.fetch_mut::<Size>().width = width; @@ -150,19 +150,20 @@ impl <'a, 'b>Room<'a, 'b> { let y = (idx as i64) / width; for template in templates { - self.create_entity(template.clone().unsaved(), Pos{x, y}).unwrap(); + self.create_entity(template.clone().unsaved(), Pos{x, y})?; } } for (name, place) in &template.places { self.places.insert(name.clone(), *place); } + Ok(()) } - pub fn create(id: RoomId, encyclopedia: &Encyclopedia, template: &RoomTemplate) -> Room<'a, 'b> { + pub fn create(id: RoomId, encyclopedia: &Encyclopedia, template: &RoomTemplate) -> Result<Room<'a, 'b>> { let mut room = Self::new(id, encyclopedia.clone(), default_dispatcher()); - room.load_from_template(template); - room + room.load_from_template(template)?; + Ok(room) } pub fn view(&self) -> HashMap<PlayerId, WorldMessage> { diff --git a/src/systems/spawn.rs b/src/systems/spawn.rs index 33dbc95..903b9b2 100644 --- a/src/systems/spawn.rs +++ b/src/systems/spawn.rs @@ -44,10 +44,14 @@ impl <'a> System<'a> for Spawn { if let Some(last_spawn) = spawner.last_spawn { if time.time > last_spawn + spawner.delay { spawner.last_spawn = None; - let mut preent = new.encyclopedia.construct(&spawner.template).expect("unable to spawn entity from spawner"); - preent.push(ComponentWrapper::Clan(spawner.clan.clone())); - preent.push(ComponentWrapper::Home(Home{home: position.pos})); - new.to_build.push((position.pos, preent)); + match new.encyclopedia.construct(&spawner.template) { + Ok(mut preent) => { + preent.push(ComponentWrapper::Clan(spawner.clan.clone())); + preent.push(ComponentWrapper::Home(Home{home: position.pos})); + new.to_build.push((position.pos, preent)); + } + Err(err) => {println!("Error: can not spawn entity from spawner: {}", err);} + } } } else { spawner.last_spawn = Some(time.time) diff --git a/src/template.rs b/src/template.rs index 850a323..dbaec93 100644 --- a/src/template.rs +++ b/src/template.rs @@ -63,11 +63,11 @@ impl Template { let name = EntityType(val.get("type").ok_or(perr!("template doesn't have 'type'"))?.as_str().ok_or(perr!("template type not a string"))?.to_string()); let mut args = Vec::new(); for arg in val.get("args").unwrap_or(&json!([])).as_array().ok_or(perr!("template args not an array"))? { - args.push(Parameter::guess_from_json(arg).ok_or(perr!("template arg not a parameter"))?); + args.push(Parameter::guess_from_json(arg).ok_or(perr!("template arg {:?} not a parameter", arg))?); } let mut kwargs = HashMap::new(); for (key, arg) in val.get("kwargs").unwrap_or(&json!({})).as_object().ok_or(perr!("template kwargs not a json object"))? { - kwargs.insert(key.to_string(), Parameter::guess_from_json(arg).ok_or(perr!("template arg not a parameter"))?); + kwargs.insert(key.to_string(), Parameter::guess_from_json(arg).ok_or(perr!("template kwarg {}: {:?} not a parameter", key, arg))?); } let save = val.get("save").unwrap_or(&json!(true)).as_bool().ok_or(perr!("save not a bool"))?; Ok(Template {name, args, kwargs, save}) diff --git a/src/world.rs b/src/world.rs index 6e59a5b..0012312 100644 --- a/src/world.rs +++ b/src/world.rs @@ -59,7 +59,7 @@ impl <'a, 'b>World<'a, 'b> { purgatory::create_purgatory(&self.encyclopedia) } else { let template = self.template_loader.load_room(id.clone())?; - Room::create(id.clone(), &self.encyclopedia, &template) + Room::create(id.clone(), &self.encyclopedia, &template)? }; if let Ok(state) = self.persistence.load_room(id.clone()){ room.load_saved(&state); |
