From e9738b98b1688011c6280023b43894a170b90216 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 18 Oct 2020 17:00:35 +0100 Subject: [PATCH] sprite: from_str and error handling --- src/game.rs | 6 ++++-- src/sprite.rs | 9 ++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/game.rs b/src/game.rs index 6d6184e..0353f72 100644 --- a/src/game.rs +++ b/src/game.rs @@ -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); diff --git a/src/sprite.rs b/src/sprite.rs index af1b0e9..984f800 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -54,8 +54,8 @@ impl Sprite { } impl Sprite { - pub fn from(string: String) -> Result { - let mut lines: Vec<&str> = string.lines().collect(); + pub fn from_str(str: &str) -> Result { + 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")); } }