handle errors on sprite parsing
This commit is contained in:
parent
7773949a8e
commit
4d33a0a284
|
@ -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 ") {
|
||||
|
|
|
@ -54,9 +54,12 @@ impl Sprite {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<String> for Sprite {
|
||||
#[derive(Debug)]
|
||||
pub struct SpriteMissingRoomPosition;
|
||||
|
||||
impl Sprite {
|
||||
#[inline]
|
||||
fn from(string: String) -> Sprite {
|
||||
pub(crate) fn from(string: String) -> Result<Sprite, SpriteMissingRoomPosition> {
|
||||
let mut lines: Vec<&str> = string.lines().collect();
|
||||
|
||||
let id = lines[0].replace("SPR ", "");
|
||||
|
@ -80,7 +83,7 @@ impl From<String> 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<String> for Sprite {
|
|||
lines[1..].join("\n")
|
||||
);
|
||||
|
||||
Sprite {
|
||||
Ok(Sprite {
|
||||
id,
|
||||
name,
|
||||
animation_frames,
|
||||
|
@ -109,7 +112,7 @@ impl From<String> 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);
|
||||
|
|
Loading…
Reference in New Issue