implement error handling for avatar

This commit is contained in:
Max Bradbury 2020-04-18 17:42:32 +01:00
parent 8d93843b55
commit c47c4205aa
2 changed files with 6 additions and 11 deletions

View File

@ -1,4 +1,5 @@
use crate::{from_base36, optional_data_line, AnimationFrames, Image, Position, ToBase36}; use crate::{from_base36, optional_data_line, AnimationFrames, Image, Position, ToBase36};
use std::error::Error;
/// avatar is a "sprite" in the game data but with a specific id /// avatar is a "sprite" in the game data but with a specific id
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
@ -24,8 +25,8 @@ impl Avatar {
} }
} }
impl From<String> for Avatar { impl Avatar {
fn from(string: String) -> Avatar { pub(crate) fn from(string: String) -> Result<Avatar, &'static dyn Error> {
let string = string.replace("SPR A\n", ""); let string = string.replace("SPR A\n", "");
let mut lines: Vec<&str> = string.lines().collect(); let mut lines: Vec<&str> = string.lines().collect();
@ -67,13 +68,7 @@ impl From<String> for Avatar {
.map(|&frame| Image::from(frame.to_string())) .map(|&frame| Image::from(frame.to_string()))
.collect(); .collect();
Avatar { Ok(Avatar { animation_frames, name, room_id, position, colour_id })
animation_frames,
name,
room_id,
position,
colour_id,
}
} }
} }
@ -92,7 +87,7 @@ impl ToString for Avatar {
#[test] #[test]
fn test_avatar_from_string() { fn test_avatar_from_string() {
let output = Avatar::from(include_str!("test-resources/avatar").to_string()); let output = Avatar::from(include_str!("test-resources/avatar").to_string()).unwrap();
assert_eq!(output, crate::mock::avatar()); assert_eq!(output, crate::mock::avatar());
} }

View File

@ -131,7 +131,7 @@ impl From<String> for Game {
} else if segment.starts_with("TIL") { } else if segment.starts_with("TIL") {
tiles.push(Tile::from(segment)); tiles.push(Tile::from(segment));
} else if segment.starts_with("SPR A") { } else if segment.starts_with("SPR A") {
avatar = Some(Avatar::from(segment)); avatar = Some(Avatar::from(segment).unwrap());
} else if segment.starts_with("SPR") { } else if segment.starts_with("SPR") {
sprites.push(Sprite::from(segment)); sprites.push(Sprite::from(segment));
} else if segment.starts_with("ITM") { } else if segment.starts_with("ITM") {