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 ", "");