sprite: from_str and error handling

This commit is contained in:
Max Bradbury 2020-10-18 17:00:35 +01:00
parent 8f558a908f
commit e9738b98b1
2 changed files with 8 additions and 7 deletions

View File

@ -213,12 +213,14 @@ impl Game {
} else if segment.starts_with("TIL ") {
tiles.push(Tile::from(segment));
} else if segment.starts_with("SPR ") {
let sprite = Sprite::from(segment);
let result = Sprite::from_str(&segment);
if let Ok(sprite) = sprite {
if let Ok(sprite) = result {
// avatar_exists |= sprite.id == "A";
sprites.push(sprite);
} else {
warnings.push(result.unwrap_err());
}
} else if segment.starts_with("ITM ") {
let result = Item::from_str(&segment);

View File

@ -54,8 +54,8 @@ impl Sprite {
}
impl Sprite {
pub fn from(string: String) -> Result<Sprite, crate::Error> {
let mut lines: Vec<&str> = string.lines().collect();
pub fn from_str(str: &str) -> Result<Sprite, crate::Error> {
let mut lines: Vec<&str> = str.lines().collect();
if lines.is_empty() || !lines[0].starts_with("SPR ") {
return Err(crate::Error::Sprite);
@ -141,8 +141,7 @@ mod test {
#[test]
fn sprite_from_string() {
let string = include_str!("test-resources/sprite").to_string();
let output = Sprite::from(string).unwrap();
let output = Sprite::from_str(include_str!("test-resources/sprite")).unwrap();
let expected = mock::sprite();
assert_eq!(output, expected);
@ -150,6 +149,6 @@ mod test {
#[test]
fn sprite_to_string() {
assert_eq!(mock::sprite().to_string(), include_str!("test-resources/sprite").to_string());
assert_eq!(mock::sprite().to_string(), include_str!("test-resources/sprite"));
}
}