From 02c3c3faae9e29681ccaa1c7e40d57f9dd55072e Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 13:52:36 +0100 Subject: [PATCH] ending module --- src/ending.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 51 +++------------------------------------------------ 2 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 src/ending.rs diff --git a/src/ending.rs b/src/ending.rs new file mode 100644 index 0000000..3696bcd --- /dev/null +++ b/src/ending.rs @@ -0,0 +1,46 @@ +// same as a dialogue basically +#[derive(Debug, Eq, PartialEq)] +pub struct Ending { + id: String, + dialogue: String, +} + +impl From for Ending { + fn from(string: String) -> Ending { + let string = string.replace("END ", ""); + let id_dialogue: Vec<&str> = string.lines().collect(); + let id = id_dialogue[0].to_string(); + let dialogue = id_dialogue[1].to_string(); + + Ending { id, dialogue } + } +} + +impl ToString for Ending { + #[inline] + fn to_string(&self) -> String { + format!("END {}\n{}", self.id, self.dialogue) + } +} + +#[test] +fn test_ending_from_string() { + assert_eq!( + Ending::from(include_str!("../test/resources/ending").to_string()), + Ending { + id: "a".to_string(), + dialogue: "This is a long line of dialogue. Blah blah blah".to_string() + } + ); +} + +#[test] +fn test_ending_to_string() { + assert_eq!( + Ending { + id: "7".to_string(), + dialogue: "This is another long ending. So long, farewell, etc.".to_string() + }.to_string(), + "END 7\nThis is another long ending. So long, farewell, etc.".to_string() + ); +} diff --git a/src/lib.rs b/src/lib.rs index 0fefea9..ee32e99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ pub mod avatar; pub mod colour; pub mod dialogue; +pub mod ending; pub mod exit; pub mod palette; pub mod image; @@ -13,6 +14,7 @@ pub mod tile; use avatar::Avatar; use colour::Colour; use dialogue::Dialogue; +use ending::Ending; use exit::Exit; use palette::Palette; use image::Image; @@ -24,7 +26,7 @@ use tile::Tile; #[derive(Debug, Eq, PartialEq)] pub struct Instance { position: Position, - id: String, // item / ending id + id: String, // item / ending.rs id } #[derive(Debug, Eq, PartialEq)] @@ -33,13 +35,6 @@ pub struct ExitInstance { exit: Exit, } -// same as a dialogue basically -#[derive(Debug, Eq, PartialEq)] -pub struct Ending { - id: String, - dialogue: String, -} - #[derive(Debug, Eq, PartialEq)] pub struct Room { id: String, @@ -285,46 +280,6 @@ impl AnimationFrames for Vec { } } -impl From for Ending { - fn from(string: String) -> Ending { - let string = string.replace("END ", ""); - let id_dialogue: Vec<&str> = string.lines().collect(); - let id = id_dialogue[0].to_string(); - let dialogue = id_dialogue[1].to_string(); - - Ending { id, dialogue } - } -} - -#[test] -fn test_ending_from_string() { - assert_eq!( - Ending::from(include_str!("../test/resources/ending").to_string()), - Ending { - id: "a".to_string(), - dialogue: "This is a long line of dialogue. Blah blah blah".to_string() - } - ); -} - -impl ToString for Ending { - #[inline] - fn to_string(&self) -> String { - format!("END {}\n{}", self.id, self.dialogue) - } -} - -#[test] -fn test_ending_to_string() { - assert_eq!( - Ending { - id: "7".to_string(), - dialogue: "This is another long ending. So long, farewell, etc.".to_string() - }.to_string(), - "END 7\nThis is another long ending. So long, farewell, etc.".to_string() - ); -} - impl From for Variable { fn from(string: String) -> Variable { let id_value: Vec<&str> = string.split('\n').collect();