Compare commits

..

2 Commits

Author SHA1 Message Date
Max Bradbury fe690a6d9b check if game data is present 2020-09-25 16:45:06 +01:00
Max Bradbury 2d3bcc4f50 todo 2020-09-25 16:38:15 +01:00
1 changed files with 17 additions and 2 deletions

View File

@ -67,8 +67,10 @@ impl Version {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum NotFound { pub enum NotFound {
/// no game data whatsoever
Anything,
Avatar, Avatar,
Room, Room,
Sprite, Sprite,
@ -101,7 +103,14 @@ pub struct GameHasNoAvatar;
// todo no tiles? no rooms? no palettes? turn this into an enum? // todo no tiles? no rooms? no palettes? turn this into an enum?
impl Game { impl Game {
// todo return (Result<Game, ?>, Vec<Box<dyn Error>>)?
// 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<Game, NotFound> { pub fn from(string: String) -> Result<Game, NotFound> {
if string.clone().trim() == "".to_string() {
return Err(NotFound::Anything);
}
let line_endings_crlf = string.contains("\r\n"); let line_endings_crlf = string.contains("\r\n");
let mut string = string; let mut string = string;
if line_endings_crlf { if line_endings_crlf {
@ -834,7 +843,7 @@ impl Game {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::game::{Version, Game}; use crate::game::{Version, Game, NotFound};
use crate::text::{TextDirection, Font}; use crate::text::{TextDirection, Font};
use crate::tile::Tile; use crate::tile::Tile;
use crate::image::Image; use crate::image::Image;
@ -1059,4 +1068,10 @@ mod test {
let expected = Some(&game.tiles[0]); let expected = Some(&game.tiles[0]);
assert_eq!(output, expected); 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);
}
} }