From fe690a6d9b5336ed2b97669009e052d1277e6b5a Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Fri, 25 Sep 2020 16:45:06 +0100 Subject: [PATCH] check if game data is present --- src/game.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/game.rs b/src/game.rs index 5a39b5f..e8887ba 100644 --- a/src/game.rs +++ b/src/game.rs @@ -67,8 +67,10 @@ impl Version { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum NotFound { + /// no game data whatsoever + Anything, Avatar, Room, Sprite, @@ -105,6 +107,10 @@ impl Game { // would be nice to *try* to parse a game, and catalogue any and all errors without crashing, // for display purposes etc. pub fn from(string: String) -> Result { + if string.clone().trim() == "".to_string() { + return Err(NotFound::Anything); + } + let line_endings_crlf = string.contains("\r\n"); let mut string = string; if line_endings_crlf { @@ -837,7 +843,7 @@ impl Game { #[cfg(test)] mod test { - use crate::game::{Version, Game}; + use crate::game::{Version, Game, NotFound}; use crate::text::{TextDirection, Font}; use crate::tile::Tile; use crate::image::Image; @@ -1062,4 +1068,10 @@ mod test { let expected = Some(&game.tiles[0]); assert_eq!(output, expected); } + + #[test] + fn empty_game_data_throws_error() { + assert_eq!(Game::from("".to_string() ).err().unwrap(), NotFound::Anything); + assert_eq!(Game::from(" \n \r\n".to_string()).err().unwrap(), NotFound::Anything); + } }