2020-04-12 12:26:33 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
|
|
pub struct Dialogue {
|
2020-04-12 16:13:08 +00:00
|
|
|
pub id: String,
|
|
|
|
pub contents: String,
|
2020-04-12 12:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Dialogue {
|
2020-04-28 17:00:31 +00:00
|
|
|
#[inline]
|
2020-04-12 12:26:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::dialogue::Dialogue;
|
|
|
|
|
|
|
|
#[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()}
|
|
|
|
)
|
|
|
|
}
|
2020-04-12 12:26:33 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
|
|
|
fn test_dialogue_to_string() {
|
|
|
|
let output = Dialogue {
|
2020-04-12 12:26:33 +00:00
|
|
|
id: "y".to_string(),
|
|
|
|
contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string()
|
2020-04-19 07:13:55 +00:00
|
|
|
}.to_string();
|
|
|
|
let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah".to_string();
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-12 12:26:33 +00:00
|
|
|
}
|