redo version as a struct

This commit is contained in:
Max Bradbury 2020-04-18 11:03:24 +01:00
parent c8a4b4a8bc
commit 935075e7a1
3 changed files with 21 additions and 19 deletions

View File

@ -13,11 +13,24 @@ use crate::{
optional_data_line 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)] #[derive(Debug, PartialEq)]
pub struct Game { pub struct Game {
pub name: String, pub name: String,
pub major_version: u8, pub version: Version,
pub minor_version: u8,
pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently pub room_format: u8, // this is "0 = non-comma separated, 1 = comma separated" apparently
pub font: Font, pub font: Font,
pub custom_font: Option<String>, // used if font is Font::Custom pub custom_font: Option<String>, // used if font is Font::Custom
@ -86,8 +99,7 @@ impl From<String> for Game {
let segments: Vec<&str> = main.split("\n\n").collect(); let segments: Vec<&str> = main.split("\n\n").collect();
let name = segments[0].to_string(); let name = segments[0].to_string();
let mut major_version: u8 = 1; let mut version = Version { major: 1, minor: 0 };
let mut minor_version: u8 = 0;
let mut room_format: u8 = 1; let mut room_format: u8 = 1;
let mut font = Font::AsciiSmall; let mut font = Font::AsciiSmall;
let mut custom_font = None; let mut custom_font = None;
@ -104,9 +116,7 @@ impl From<String> for Game {
if segment.starts_with("# BITSY VERSION") { if segment.starts_with("# BITSY VERSION") {
let segment = segment.replace("# BITSY VERSION ", ""); let segment = segment.replace("# BITSY VERSION ", "");
let parts = Game::version_from_string(segment); version = Version::from(&segment);
major_version = parts.0;
minor_version = parts.1;
} else if segment.starts_with("! ROOM_FORMAT") { } else if segment.starts_with("! ROOM_FORMAT") {
room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap(); room_format = segment.replace("! ROOM_FORMAT ", "").parse().unwrap();
} else if segment.starts_with("DEFAULT_FONT") { } else if segment.starts_with("DEFAULT_FONT") {
@ -139,8 +149,7 @@ impl From<String> for Game {
Game { Game {
name, name,
major_version, version,
minor_version,
room_format, room_format,
font, font,
custom_font, custom_font,
@ -242,18 +251,12 @@ impl Game {
} }
fn version_line(&self) -> String { 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 { fn room_format_line(&self) -> String {
optional_data_line("! ROOM_FORMAT", Some(self.room_format)) 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] #[test]

View File

@ -23,7 +23,7 @@ use dialogue::Dialogue;
use ending::Ending; use ending::Ending;
use exit::{Transition, Exit}; use exit::{Transition, Exit};
use text::{Font, TextDirection}; use text::{Font, TextDirection};
use game::Game; use game::{Game, Version};
use palette::Palette; use palette::Palette;
use image::Image; use image::Image;
use item::Item; use item::Item;

View File

@ -188,8 +188,7 @@ pub fn room() -> Room {
pub fn game_default() -> Game { pub fn game_default() -> Game {
Game { Game {
name: "Write your game's title here".to_string(), name: "Write your game's title here".to_string(),
major_version: 6, version: Version { major: 6, minor: 5 },
minor_version: 5,
room_format: 1, room_format: 1,
font: Font::AsciiSmall, font: Font::AsciiSmall,
custom_font: None, custom_font: None,