From 0faaf23a2d2dea0d9423980a19ea7da2d30492d4 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 13:26:33 +0100 Subject: [PATCH] dialogue module --- src/dialogue.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 44 ++------------------------------------------ 2 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 src/dialogue.rs diff --git a/src/dialogue.rs b/src/dialogue.rs new file mode 100644 index 0000000..12a0096 --- /dev/null +++ b/src/dialogue.rs @@ -0,0 +1,41 @@ +#[derive(Debug, Eq, PartialEq)] +pub struct Dialogue { + pub(crate) id: String, + pub(crate) contents: String, +} + +impl From for Dialogue { + fn from(string: String) -> Dialogue { + let lines: Vec<&str> = string.lines().collect(); + let id = lines[0].replace("DLG ", "").to_string(); + let contents = lines[1..].join("\n"); + + Dialogue { id, contents } + } +} + +impl ToString for Dialogue { + #[inline] + fn to_string(&self) -> String { + format!("DLG {}\n{}", self.id, self.contents) + } +} + +#[test] +fn test_dialogue_from_string() { + assert_eq!( + Dialogue::from("DLG h\nhello\ngoodbye".to_string()), + Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()} + ) +} + +#[test] +fn test_dialogue_to_string() { + assert_eq!( + Dialogue { + id: "y".to_string(), + contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string() + }.to_string(), + "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah".to_string() + ); +} diff --git a/src/lib.rs b/src/lib.rs index f317528..fec9a14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod colour; +pub mod dialogue; pub mod palette; pub mod image; pub mod mocks; @@ -6,6 +7,7 @@ pub mod position; pub mod tile; use colour::Colour; +use dialogue::Dialogue; use palette::Palette; use image::Image; use position::Position; @@ -23,12 +25,6 @@ pub struct ExitInstance { exit: Exit, } -#[derive(Debug, Eq, PartialEq)] -pub struct Dialogue { - id: String, - contents: String, -} - #[derive(Debug, Eq, PartialEq)] pub struct Sprite { id: String, // lowercase base36 @@ -612,42 +608,6 @@ fn test_ending_to_string() { ); } -impl From for Dialogue { - fn from(string: String) -> Dialogue { - let lines: Vec<&str> = string.lines().collect(); - let id = lines[0].replace("DLG ", "").to_string(); - let contents = lines[1..].join("\n"); - - Dialogue { id, contents } - } -} - -#[test] -fn test_dialogue_from_string() { - assert_eq!( - Dialogue::from("DLG h\nhello\ngoodbye".to_string()), - Dialogue { id: "h".to_string(), contents: "hello\ngoodbye".to_string()} - ) -} - -impl ToString for Dialogue { - #[inline] - fn to_string(&self) -> String { - format!("DLG {}\n{}", self.id, self.contents) - } -} - -#[test] -fn test_dialogue_to_string() { - assert_eq!( - Dialogue { - id: "y".to_string(), - contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string() - }.to_string(), - "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah".to_string() - ); -} - impl From for Variable { fn from(string: String) -> Variable { let id_value: Vec<&str> = string.split('\n').collect();