optional version handling

This commit is contained in:
Max Bradbury 2020-04-23 12:03:39 +01:00
parent 6e7240e2d7
commit 096eb39bf6
2 changed files with 19 additions and 10 deletions

View File

@ -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<Version>,
pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently
pub font: Font,
pub custom_font: Option<String>, // 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 {
if self.version.is_some() {
format!(
"\n# BITSY VERSION {}.{}",
self.version.major, self.version.minor
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"))
}
}

View File

@ -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,