From 096eb39bf601d07dd6d6774cba9a2ca31254b1e7 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 23 Apr 2020 12:03:39 +0100 Subject: [PATCH] optional version handling --- src/game.rs | 27 ++++++++++++++++++--------- src/mock.rs | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/game.rs b/src/game.rs index 6b09048..14c6e0a 100644 --- a/src/game.rs +++ b/src/game.rs @@ -4,7 +4,7 @@ use crate::{ }; use std::error::Error; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Copy, Clone)] pub struct Version { pub major: u8, pub minor: u8, @@ -24,7 +24,7 @@ impl Version { #[derive(Debug, PartialEq)] pub struct Game { pub name: String, - pub version: Version, + pub version: Option, pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently pub font: Font, pub custom_font: Option, // used if font is Font::Custom @@ -103,7 +103,7 @@ impl Game { let segments: Vec<&str> = main.split("\n\n").collect(); let name = segments[0].to_string(); - let mut version = Version { major: 1, minor: 0 }; + let mut version = None; let mut room_format: u8 = 1; let mut font = Font::AsciiSmall; let mut custom_font = None; @@ -120,7 +120,7 @@ impl Game { if segment.starts_with("# BITSY VERSION") { let segment = segment.replace("# BITSY VERSION ", ""); - version = Version::from(&segment); + version = Some(Version::from(&segment)); } else if segment.starts_with("! ROOM_FORMAT") { room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap(); } else if segment.starts_with("DEFAULT_FONT") { @@ -282,10 +282,14 @@ impl Game { } fn version_line(&self) -> String { - format!( - "\n# BITSY VERSION {}.{}", - self.version.major, self.version.minor - ) + if self.version.is_some() { + format!( + "\n# BITSY VERSION {}.{}", + self.version.as_ref().unwrap().major, self.version.as_ref().unwrap().minor + ) + } else { + "".to_string() + } } fn room_format_line(&self) -> String { @@ -311,6 +315,11 @@ impl Game { "" } } + + /// older bitsy games do not specify a version, but we can infer 1.0 + pub fn version(&self) -> Version { + self.version.unwrap_or(Version { major: 1, minor: 0 }) + } } #[cfg(test)] @@ -452,7 +461,7 @@ mod test { #[test] fn test_version_formatting() { let mut game = crate::mock::game_default(); - game.version = Version { major: 5, minor: 0 }; + game.version = Some(Version { major: 5, minor: 0 }); assert!(game.to_string().contains("# BITSY VERSION 5.0")) } } diff --git a/src/mock.rs b/src/mock.rs index 7f97306..9c014c3 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -403,7 +403,7 @@ pub fn room() -> Room { pub fn game_default() -> Game { Game { name: "Write your game's title here".to_string(), - version: Version { major: 6, minor: 5 }, + version: Some(Version { major: 6, minor: 5 }), room_format: 1, font: Font::AsciiSmall, custom_font: None,