variable module

This commit is contained in:
Max Bradbury 2020-04-12 14:20:53 +01:00
parent 02c3c3faae
commit 3d7bf1d24e
2 changed files with 39 additions and 38 deletions

View File

@ -10,6 +10,7 @@ pub mod mock;
pub mod position; pub mod position;
pub mod sprite; pub mod sprite;
pub mod tile; pub mod tile;
pub mod variable;
use avatar::Avatar; use avatar::Avatar;
use colour::Colour; use colour::Colour;
@ -22,6 +23,7 @@ use item::Item;
use position::Position; use position::Position;
use sprite::Sprite; use sprite::Sprite;
use tile::Tile; use tile::Tile;
use variable::Variable;
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub struct Instance { pub struct Instance {
@ -46,12 +48,6 @@ pub struct Room {
endings: Vec<Instance>, endings: Vec<Instance>,
} }
#[derive(Debug, Eq, PartialEq)]
pub struct Variable {
id: String,
initial_value: String,
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Game { pub struct Game {
name: String, name: String,
@ -280,38 +276,6 @@ impl AnimationFrames for Vec<Image> {
} }
} }
impl From<String> for Variable {
fn from(string: String) -> Variable {
let id_value: Vec<&str> = string.split('\n').collect();
let id = id_value[0].replace("VAR ", "").to_string();
let initial_value = id_value[1].to_string();
Variable { id, initial_value }
}
}
#[test]
fn test_variable_from_string() {
assert_eq!(
Variable::from("VAR a\n42".to_string()),
Variable { id: "a".to_string(), initial_value: "42".to_string()}
);
}
impl ToString for Variable {
#[inline]
fn to_string(&self) -> String {
format!("VAR {}\n{}", self.id, self.initial_value)
}
}
#[test]
fn test_variable_to_string() {
let output = Variable { id: "c".to_string(), initial_value: "57".to_string() }.to_string();
let expected = "VAR c\n57".to_string();
assert_eq!(output, expected);
}
impl From<String> for Room { impl From<String> for Room {
fn from(string: String) -> Room { fn from(string: String) -> Room {
// todo handle room_format? // todo handle room_format?

37
src/variable.rs Normal file
View File

@ -0,0 +1,37 @@
#[derive(Debug, Eq, PartialEq)]
pub struct Variable {
pub(crate) id: String,
pub(crate) initial_value: String,
}
impl From<String> for Variable {
fn from(string: String) -> Variable {
let id_value: Vec<&str> = string.split('\n').collect();
let id = id_value[0].replace("VAR ", "").to_string();
let initial_value = id_value[1].to_string();
Variable { id, initial_value }
}
}
impl ToString for Variable {
#[inline]
fn to_string(&self) -> String {
format!("VAR {}\n{}", self.id, self.initial_value)
}
}
#[test]
fn test_variable_from_string() {
assert_eq!(
Variable::from("VAR a\n42".to_string()),
Variable { id: "a".to_string(), initial_value: "42".to_string()}
);
}
#[test]
fn test_variable_to_string() {
let output = Variable { id: "c".to_string(), initial_value: "57".to_string() }.to_string();
let expected = "VAR c\n57".to_string();
assert_eq!(output, expected);
}