From 4d33a0a2843d39c237f1c75363baf772aff68d06 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 23 Jun 2020 13:01:04 +0100 Subject: [PATCH] handle errors on sprite parsing --- src/game.rs | 5 ++++- src/sprite.rs | 15 +++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/game.rs b/src/game.rs index 8126cf1..d29284b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -186,7 +186,10 @@ impl Game { } else if segment.starts_with("TIL ") { tiles.push(Tile::from(segment)); } else if segment.starts_with("SPR ") { - sprites.push(Sprite::from(segment)); + let sprite = Sprite::from(segment); + if sprite.is_ok() { + sprites.push(sprite.unwrap()); + } } else if segment.starts_with("ITM ") { items.push(Item::from(segment)); } else if segment.starts_with("DLG ") { diff --git a/src/sprite.rs b/src/sprite.rs index f42fd2e..c8bae13 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -54,9 +54,12 @@ impl Sprite { } } -impl From for Sprite { +#[derive(Debug)] +pub struct SpriteMissingRoomPosition; + +impl Sprite { #[inline] - fn from(string: String) -> Sprite { + pub(crate) fn from(string: String) -> Result { let mut lines: Vec<&str> = string.lines().collect(); let id = lines[0].replace("SPR ", ""); @@ -80,7 +83,7 @@ impl From for Sprite { room_id = Some(room_position[0].to_string()); if room_position.len() < 2 { - panic!("Bad room/position for sprite: {}", string); + return Err(SpriteMissingRoomPosition); } position = Some(Position::from_str(room_position[1]).unwrap()); @@ -100,7 +103,7 @@ impl From for Sprite { lines[1..].join("\n") ); - Sprite { + Ok(Sprite { id, name, animation_frames, @@ -109,7 +112,7 @@ impl From for Sprite { position, colour_id, items - } + }) } } @@ -136,7 +139,7 @@ mod test { #[test] fn test_sprite_from_string() { - let output = Sprite::from(include_str!("test-resources/sprite").to_string()); + let output = Sprite::from(include_str!("test-resources/sprite").to_string()).unwrap(); let expected = mock::sprite(); assert_eq!(output, expected);