make a start toward better error handling
This commit is contained in:
180
src/game.rs
180
src/game.rs
@@ -1,4 +1,4 @@
|
||||
use crate::{Dialogue, Ending, Font, Image, Item, Palette, Room, Sprite, TextDirection, Tile, Variable, transform_line_endings, segments_from_string, new_unique_id, try_id, Instance};
|
||||
use crate::{Dialogue, Ending, Font, Image, Item, Palette, Room, Sprite, TextDirection, Tile, Variable, transform_line_endings, segments_from_string, new_unique_id, try_id, Instance, Error};
|
||||
|
||||
use loe::TransformMode;
|
||||
|
||||
@@ -6,7 +6,6 @@ use std::str::FromStr;
|
||||
use std::collections::HashMap;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::fmt;
|
||||
use std::fmt::Display;
|
||||
|
||||
/// in very early versions of Bitsy, room tiles were defined as single alphanumeric characters -
|
||||
/// so there was a maximum of 36 unique tiles. later versions are comma-separated.
|
||||
@@ -27,11 +26,11 @@ impl RoomFormat {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for RoomFormat {
|
||||
impl fmt::Display for RoomFormat {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", match &self {
|
||||
RoomFormat::Contiguous => "0",
|
||||
RoomFormat::CommaSeparated => "1",
|
||||
RoomFormat::Contiguous => 0,
|
||||
RoomFormat::CommaSeparated => 1,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -56,42 +55,37 @@ pub struct Version {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InvalidVersion;
|
||||
pub enum VersionError {
|
||||
MissingParts,
|
||||
ExtraneousParts,
|
||||
MalformedInteger,
|
||||
}
|
||||
|
||||
impl Version {
|
||||
fn from(str: &str) -> Result<Version, InvalidVersion> {
|
||||
let parts: Vec<&str> = str.split('.').collect();
|
||||
|
||||
if parts.len() == 2 {
|
||||
Ok(Version {
|
||||
major: parts[0].parse().unwrap(),
|
||||
minor: parts[1].parse().unwrap(),
|
||||
})
|
||||
} else {
|
||||
Err (InvalidVersion)
|
||||
}
|
||||
impl fmt::Display for VersionError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", match self {
|
||||
VersionError::MissingParts => "Not enough parts supplied for version",
|
||||
VersionError::ExtraneousParts => "Too many parts supplied for version",
|
||||
VersionError::MalformedInteger => "Version did not contain valid integers",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum NotFound {
|
||||
/// no game data whatsoever
|
||||
Anything,
|
||||
Avatar,
|
||||
Room,
|
||||
Sprite,
|
||||
Tile,
|
||||
}
|
||||
impl std::error::Error for VersionError {}
|
||||
|
||||
impl Display for NotFound {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f,"Not found: {} data", match self {
|
||||
NotFound::Anything => "game",
|
||||
NotFound::Avatar => "avatar",
|
||||
NotFound::Room => "room",
|
||||
NotFound::Sprite => "sprite",
|
||||
NotFound::Tile => "tile",
|
||||
})
|
||||
impl Version {
|
||||
fn from(str: &str) -> Result<Version, VersionError> {
|
||||
let parts: Vec<&str> = str.split('.').collect();
|
||||
|
||||
if parts.len() < 2 {
|
||||
Err(VersionError::MissingParts)
|
||||
} else if parts.len() > 2 {
|
||||
Err(VersionError::ExtraneousParts)
|
||||
} else if let (Ok(major), Ok(minor)) = (parts[0].parse(), parts[1].parse()) {
|
||||
Ok(Version { major, minor })
|
||||
} else {
|
||||
Err(VersionError::MalformedInteger)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,19 +110,14 @@ pub struct Game {
|
||||
pub(crate) line_endings_crlf: bool, // otherwise lf (unix/mac)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GameHasNoAvatar;
|
||||
// todo no tiles? no rooms? no palettes? turn this into an enum?
|
||||
|
||||
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, Vec<crate::Error>), crate::error::NotFound> {
|
||||
if string.trim() == "" {
|
||||
return Err(NotFound::Anything);
|
||||
return Err(crate::error::NotFound::Anything);
|
||||
}
|
||||
|
||||
let mut warnings = Vec::new();
|
||||
|
||||
let line_endings_crlf = string.contains("\r\n");
|
||||
let mut string = string;
|
||||
if line_endings_crlf {
|
||||
@@ -182,14 +171,17 @@ impl Game {
|
||||
let mut tiles: Vec<Tile> = Vec::new();
|
||||
let mut sprites: Vec<Sprite> = Vec::new();
|
||||
let mut items: Vec<Item> = Vec::new();
|
||||
let mut avatar_exists = false;
|
||||
// let mut avatar_exists = false;
|
||||
|
||||
for segment in segments {
|
||||
if segment.starts_with("# BITSY VERSION") {
|
||||
let segment = segment.replace("# BITSY VERSION ", "");
|
||||
let segment = Version::from(&segment);
|
||||
if let Ok(segment) = segment {
|
||||
version = Some(segment);
|
||||
let result = Version::from(&segment);
|
||||
|
||||
if let Ok(v) = result {
|
||||
version = Some(v);
|
||||
} else {
|
||||
warnings.push(Error::Version);
|
||||
}
|
||||
} else if segment.starts_with("! ROOM_FORMAT") {
|
||||
let segment = segment.replace("! ROOM_FORMAT ", "");
|
||||
@@ -207,7 +199,13 @@ impl Game {
|
||||
} else if segment.trim() == "TEXT_DIRECTION RTL" {
|
||||
text_direction = TextDirection::RightToLeft;
|
||||
} else if segment.starts_with("PAL ") {
|
||||
palettes.push(Palette::from(segment));
|
||||
let result = Palette::from_str(&segment);
|
||||
if let Ok((palette, mut errors)) = result {
|
||||
palettes.push(palette);
|
||||
warnings.append(&mut errors);
|
||||
} else {
|
||||
warnings.push(result.unwrap_err());
|
||||
}
|
||||
} else if segment.starts_with("ROOM ") || segment.starts_with("SET ") {
|
||||
if segment.starts_with("SET ") {
|
||||
room_type = RoomType::Set;
|
||||
@@ -219,7 +217,7 @@ impl Game {
|
||||
let sprite = Sprite::from(segment);
|
||||
|
||||
if let Ok(sprite) = sprite {
|
||||
avatar_exists |= sprite.id == "A";
|
||||
// avatar_exists |= sprite.id == "A";
|
||||
|
||||
sprites.push(sprite);
|
||||
}
|
||||
@@ -238,68 +236,71 @@ impl Game {
|
||||
}
|
||||
}
|
||||
|
||||
if ! avatar_exists {
|
||||
return Err(NotFound::Avatar);
|
||||
}
|
||||
// if ! avatar_exists {
|
||||
// return Err(crate::Error::NotFound::Avatar);
|
||||
// }
|
||||
|
||||
Ok(
|
||||
Game {
|
||||
name,
|
||||
version,
|
||||
room_format,
|
||||
room_type,
|
||||
font,
|
||||
custom_font,
|
||||
text_direction,
|
||||
palettes,
|
||||
rooms,
|
||||
tiles,
|
||||
sprites,
|
||||
items,
|
||||
dialogues,
|
||||
endings,
|
||||
variables,
|
||||
font_data,
|
||||
line_endings_crlf,
|
||||
}
|
||||
(
|
||||
Game {
|
||||
name,
|
||||
version,
|
||||
room_format,
|
||||
room_type,
|
||||
font,
|
||||
custom_font,
|
||||
text_direction,
|
||||
palettes,
|
||||
rooms,
|
||||
tiles,
|
||||
sprites,
|
||||
items,
|
||||
dialogues,
|
||||
endings,
|
||||
variables,
|
||||
font_data,
|
||||
line_endings_crlf,
|
||||
},
|
||||
warnings
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/// todo refactor this into "get T by ID", taking a Vec<T> and an ID name?
|
||||
pub fn get_sprite_by_id(&self, id: String) -> Result<&Sprite, NotFound> {
|
||||
pub fn get_sprite_by_id(&self, id: String) -> Result<&Sprite, crate::error::NotFound> {
|
||||
let index = self.sprites.iter().position(
|
||||
|sprite| sprite.id == id
|
||||
);
|
||||
|
||||
match index {
|
||||
Some(index) => Ok(&self.sprites[index]),
|
||||
None => Err(NotFound::Sprite),
|
||||
None => Err(crate::error::NotFound::Sprite),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_tile_by_id(&self, id: String) -> Result<&Tile, NotFound> {
|
||||
pub fn get_tile_by_id(&self, id: String) -> Result<&Tile, crate::error::NotFound> {
|
||||
let index = self.tiles.iter().position(
|
||||
|tile| tile.id == id
|
||||
);
|
||||
|
||||
match index {
|
||||
Some(index) => Ok(&self.tiles[index]),
|
||||
None => Err(NotFound::Tile),
|
||||
None => Err(crate::error::NotFound::Tile),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_room_by_id(&self, id: String) -> Result<&Room, NotFound> {
|
||||
pub fn get_room_by_id(&self, id: String) -> Result<&Room, crate::error::NotFound> {
|
||||
let index = self.rooms.iter().position(
|
||||
|room| room.id == id
|
||||
);
|
||||
|
||||
match index {
|
||||
Some(index) => Ok(&self.rooms[index]),
|
||||
None => Err(NotFound::Room),
|
||||
None => Err(crate::error::NotFound::Room),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_avatar(&self) -> Result<&Sprite, NotFound> {
|
||||
pub fn get_avatar(&self) -> Result<&Sprite, crate::error::NotFound> {
|
||||
self.get_sprite_by_id("A".to_string())
|
||||
}
|
||||
|
||||
@@ -316,12 +317,9 @@ impl Game {
|
||||
tiles
|
||||
}
|
||||
|
||||
pub fn get_tiles_for_room(&self, id: String) -> Result<Vec<&Tile>, NotFound> {
|
||||
let room = self.get_room_by_id(id);
|
||||
if room.is_err() {
|
||||
return Err(NotFound::Room);
|
||||
}
|
||||
let mut tile_ids = room.unwrap().tiles.clone();
|
||||
pub fn get_tiles_for_room(&self, id: String) -> Result<Vec<&Tile>, crate::error::NotFound> {
|
||||
let room = self.get_room_by_id(id)?;
|
||||
let mut tile_ids = room.tiles.clone();
|
||||
tile_ids.sort();
|
||||
tile_ids.dedup();
|
||||
// remove 0 as this isn't a real tile
|
||||
@@ -842,11 +840,11 @@ impl Game {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{TextDirection, Font, Version, Game, NotFound, Tile, Image};
|
||||
use crate::{TextDirection, Font, Version, Game, Tile, Image};
|
||||
|
||||
#[test]
|
||||
fn game_from_string() {
|
||||
let output = Game::from(include_str!["test-resources/default.bitsy"].to_string()).unwrap();
|
||||
let (output, _) = Game::from(include_str!["test-resources/default.bitsy"].to_string()).unwrap();
|
||||
let expected = crate::mock::game_default();
|
||||
|
||||
assert_eq!(output, expected);
|
||||
@@ -909,7 +907,7 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn arabic() {
|
||||
let game = Game::from(include_str!("test-resources/arabic.bitsy").to_string()).unwrap();
|
||||
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);
|
||||
@@ -1067,7 +1065,7 @@ mod test {
|
||||
|
||||
#[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);
|
||||
assert_eq!(Game::from("".to_string() ).unwrap_err(), crate::error::NotFound::Anything);
|
||||
assert_eq!(Game::from(" \n \r\n".to_string()).unwrap_err(), crate::error::NotFound::Anything);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user