From 434e98cc606a12edd8e28972238072b945c64314 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sat, 18 Apr 2020 17:48:29 +0100 Subject: [PATCH] implement error handling for game --- src/game.rs | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/game.rs b/src/game.rs index 50ac617..b0cf1a6 100644 --- a/src/game.rs +++ b/src/game.rs @@ -2,6 +2,7 @@ use crate::{ optional_data_line, Avatar, Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, }; +use std::error::Error; #[derive(Debug, Eq, PartialEq)] pub struct Version { @@ -39,8 +40,8 @@ pub struct Game { pub variables: Vec, } -impl From for Game { - fn from(string: String) -> Game { +impl Game { + fn from(string: String) -> Result { let mut string = format!("{}\n\n", string.trim_matches('\n')); if string.starts_with("# BITSY VERSION") { @@ -142,23 +143,25 @@ impl From for Game { assert!(avatar.is_some()); let avatar = avatar.unwrap(); - Game { - name, - version, - room_format, - font, - custom_font, - text_direction, - palettes, - rooms, - tiles, - avatar, - sprites, - items, - dialogues, - endings, - variables, - } + Ok ( + Game { + name, + version, + room_format, + font, + custom_font, + text_direction, + palettes, + rooms, + tiles, + avatar, + sprites, + items, + dialogues, + endings, + variables, + } + ) } } @@ -301,7 +304,9 @@ impl Game { #[test] fn test_game_from_string() { - let output = Game::from(include_str!["test-resources/default.bitsy"].to_string()); + let output = Game::from( + include_str!["test-resources/default.bitsy"].to_string() + ).unwrap(); let expected = crate::mock::game_default(); @@ -412,7 +417,7 @@ fn test_bitsy_omnibus() { println!("Testing: {}...", path.display()); let game_data = std::fs::read_to_string(path).unwrap(); - let game = Game::from(game_data.clone()); + let game = Game::from(game_data.clone()).unwrap(); assert_eq!( game.to_string().trim_matches('\n'), game_data.trim_matches('\n') @@ -424,7 +429,10 @@ fn test_bitsy_omnibus() { #[test] fn test_arabic() { - let game = Game::from(include_str!("test-resources/arabic.bitsy").to_string()); + let game = Game::from( + include_str!("test-resources/arabic.bitsy").to_string() + ).unwrap(); + assert_eq!(game.font, Font::Arabic); assert_eq!(game.text_direction, TextDirection::RightToLeft); }