From 6090d365598e880053300ee59c649d7f7ae6845b Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 13:49:19 +0100 Subject: [PATCH] exit module --- src/exit.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 42 ++---------------------------------------- 2 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 src/exit.rs diff --git a/src/exit.rs b/src/exit.rs new file mode 100644 index 0000000..ce477d6 --- /dev/null +++ b/src/exit.rs @@ -0,0 +1,41 @@ +use crate::Position; + +#[derive(Debug, Eq, PartialEq)] +pub struct Exit { + /// destination + pub(crate) room: String, /// id + pub(crate) position: Position, +} + +impl From for Exit { + fn from(string: String) -> Exit { + // e.g. "4 3,3" + let room_position: Vec<&str> = string.split(' ').collect(); + let room = room_position[0].to_string(); + let position = Position::from(room_position[1].to_string()); + + Exit { room, position } + } +} + +impl ToString for Exit { + fn to_string(&self) -> String { + format!("{} {}", self.room, self.position.to_string()) + } +} + +#[test] +fn test_exit_from_string() { + assert_eq!( + Exit::from("a 12,13".to_string()), + Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } } + ); +} + +#[test] +fn test_exit_to_string() { + assert_eq!( + Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(), + "8 5,6".to_string() + ); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f303e28..5b3ab3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ pub mod avatar; pub mod colour; pub mod dialogue; +pub mod exit; pub mod palette; pub mod image; pub mod item; @@ -12,6 +13,7 @@ pub mod tile; use avatar::Avatar; use colour::Colour; use dialogue::Dialogue; +use exit::Exit; use palette::Palette; use image::Image; use item::Item; @@ -31,13 +33,6 @@ pub struct ExitInstance { exit: Exit, } -#[derive(Debug, Eq, PartialEq)] -pub struct Exit { - /// destination - room: String, /// id - position: Position, -} - // same as a dialogue basically #[derive(Debug, Eq, PartialEq)] pub struct Ending { @@ -290,39 +285,6 @@ impl AnimationFrames for Vec { } } -impl From for Exit { - fn from(string: String) -> Exit { - // e.g. "4 3,3" - let room_position: Vec<&str> = string.split(' ').collect(); - let room = room_position[0].to_string(); - let position = Position::from(room_position[1].to_string()); - - Exit { room, position } - } -} - -#[test] -fn test_exit_from_string() { - assert_eq!( - Exit::from("a 12,13".to_string()), - Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } } - ); -} - -impl ToString for Exit { - fn to_string(&self) -> String { - format!("{} {}", self.room, self.position.to_string()) - } -} - -#[test] -fn test_exit_to_string() { - assert_eq!( - Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(), - "8 5,6".to_string() - ); -} - impl From for Ending { fn from(string: String) -> Ending { let string = string.replace("END ", "");