From 935075e7a146543c960cbd3766f796220f6f681e Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sat, 18 Apr 2020 11:03:24 +0100 Subject: [PATCH] redo version as a struct --- src/game.rs | 35 +++++++++++++++++++---------------- src/lib.rs | 2 +- src/mock.rs | 3 +-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/game.rs b/src/game.rs index 8d6cab7..6e36adc 100644 --- a/src/game.rs +++ b/src/game.rs @@ -13,11 +13,24 @@ use crate::{ optional_data_line }; +#[derive(Debug, Eq, PartialEq)] +pub struct Version { + pub major: u8, + pub minor: u8, +} + +impl Version { + fn from(str: &str) -> Version { + let parts: Vec<&str> = str.split(".").collect(); + assert_eq!(parts.len(), 2); + Version { major: parts[0].parse().unwrap(), minor: parts[1].parse().unwrap() } + } +} + #[derive(Debug, PartialEq)] pub struct Game { pub name: String, - pub major_version: u8, - pub minor_version: u8, + pub version: Version, 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 @@ -86,8 +99,7 @@ impl From for Game { let segments: Vec<&str> = main.split("\n\n").collect(); let name = segments[0].to_string(); - let mut major_version: u8 = 1; - let mut minor_version: u8 = 0; + let mut version = Version { major: 1, minor: 0 }; let mut room_format: u8 = 1; let mut font = Font::AsciiSmall; let mut custom_font = None; @@ -104,9 +116,7 @@ impl From for Game { if segment.starts_with("# BITSY VERSION") { let segment = segment.replace("# BITSY VERSION ", ""); - let parts = Game::version_from_string(segment); - major_version = parts.0; - minor_version = parts.1; + version = 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") { @@ -139,8 +149,7 @@ impl From for Game { Game { name, - major_version, - minor_version, + version, room_format, font, custom_font, @@ -242,18 +251,12 @@ impl Game { } fn version_line(&self) -> String { - format!("\n# BITSY VERSION {}.{}", self.major_version, self.minor_version) + format!("\n# BITSY VERSION {}.{}", self.version.major, self.version.minor) } fn room_format_line(&self) -> String { optional_data_line("! ROOM_FORMAT", Some(self.room_format)) } - - fn version_from_string(string: String) -> (u8, u8) { - let parts: Vec<&str> = string.split(".").collect(); - assert_eq!(parts.len(), 2); - (parts[0].parse().unwrap(),parts[1].parse().unwrap()) - } } #[test] diff --git a/src/lib.rs b/src/lib.rs index 30a3443..0dbc3ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ use dialogue::Dialogue; use ending::Ending; use exit::{Transition, Exit}; use text::{Font, TextDirection}; -use game::Game; +use game::{Game, Version}; use palette::Palette; use image::Image; use item::Item; diff --git a/src/mock.rs b/src/mock.rs index 3205ad6..f2af151 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -188,8 +188,7 @@ pub fn room() -> Room { pub fn game_default() -> Game { Game { name: "Write your game's title here".to_string(), - major_version: 6, - minor_version: 5, + version: Version { major: 6, minor: 5 }, room_format: 1, font: Font::AsciiSmall, custom_font: None,