dialogue module

This commit is contained in:
Max Bradbury 2020-04-12 13:26:33 +01:00
parent ca1b86c2b3
commit 0faaf23a2d
2 changed files with 43 additions and 42 deletions

41
src/dialogue.rs Normal file
View File

@ -0,0 +1,41 @@
#[derive(Debug, Eq, PartialEq)]
pub struct Dialogue {
pub(crate) id: String,
pub(crate) contents: String,
}
impl From<String> 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()
);
}

View File

@ -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<String> 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<String> for Variable {
fn from(string: String) -> Variable {
let id_value: Vec<&str> = string.split('\n').collect();