dialogue module
This commit is contained in:
41
src/dialogue.rs
Normal file
41
src/dialogue.rs
Normal 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()
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user