From 3d7bf1d24e523ce837fa3719a091316bf657cab5 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 14:20:53 +0100 Subject: [PATCH] variable module --- src/lib.rs | 40 ++-------------------------------------- src/variable.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 src/variable.rs diff --git a/src/lib.rs b/src/lib.rs index ee32e99..68daec0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ pub mod mock; pub mod position; pub mod sprite; pub mod tile; +pub mod variable; use avatar::Avatar; use colour::Colour; @@ -22,6 +23,7 @@ use item::Item; use position::Position; use sprite::Sprite; use tile::Tile; +use variable::Variable; #[derive(Debug, Eq, PartialEq)] pub struct Instance { @@ -46,12 +48,6 @@ pub struct Room { endings: Vec, } -#[derive(Debug, Eq, PartialEq)] -pub struct Variable { - id: String, - initial_value: String, -} - #[derive(Debug, PartialEq)] pub struct Game { name: String, @@ -280,38 +276,6 @@ impl AnimationFrames for Vec { } } -impl From 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 for Room { fn from(string: String) -> Room { // todo handle room_format? diff --git a/src/variable.rs b/src/variable.rs new file mode 100644 index 0000000..9916f64 --- /dev/null +++ b/src/variable.rs @@ -0,0 +1,37 @@ +#[derive(Debug, Eq, PartialEq)] +pub struct Variable { + pub(crate) id: String, + pub(crate) initial_value: String, +} + +impl From 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); +}