2020-04-30 18:44:20 +00:00
|
|
|
use crate::optional_data_line;
|
|
|
|
|
2020-10-18 12:46:22 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2020-05-31 15:12:23 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
2020-04-12 12:26:33 +00:00
|
|
|
pub struct Dialogue {
|
2020-04-12 16:13:08 +00:00
|
|
|
pub id: String,
|
|
|
|
pub contents: String,
|
2020-04-30 18:44:20 +00:00
|
|
|
pub name: Option<String>,
|
2020-04-12 12:26:33 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 12:46:22 +00:00
|
|
|
impl Dialogue {
|
|
|
|
pub fn from_str(str: &str) -> Result<Dialogue, crate::Error> {
|
|
|
|
let mut lines: Vec<&str> = str.lines().collect();
|
2020-10-18 12:51:42 +00:00
|
|
|
|
|
|
|
if lines.is_empty() || !lines[0].starts_with("DLG ") {
|
|
|
|
return Err(crate::Error::Dialogue);
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:54:23 +00:00
|
|
|
let id = lines[0].replace("DLG ", "");
|
2020-04-30 18:44:20 +00:00
|
|
|
|
2020-10-18 12:51:42 +00:00
|
|
|
let last_line = lines.pop().unwrap();
|
|
|
|
|
|
|
|
let name = if last_line.starts_with("NAME ") {
|
|
|
|
Some(last_line.replace("NAME ", ""))
|
2020-04-30 18:44:20 +00:00
|
|
|
} else {
|
2020-10-18 12:51:42 +00:00
|
|
|
lines.push(last_line);
|
2020-04-30 18:44:20 +00:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-04-12 12:26:33 +00:00
|
|
|
let contents = lines[1..].join("\n");
|
|
|
|
|
2020-10-18 12:46:22 +00:00
|
|
|
Ok(Dialogue { id, contents, name })
|
2020-04-12 12:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:46:22 +00:00
|
|
|
impl fmt::Display for Dialogue {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
2020-04-30 18:44:20 +00:00
|
|
|
"DLG {}\n{}{}",
|
|
|
|
self.id,
|
|
|
|
self.contents,
|
|
|
|
optional_data_line("NAME", self.name.as_ref())
|
|
|
|
)
|
2020-04-12 12:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-10-09 10:57:02 +00:00
|
|
|
use crate::Dialogue;
|
2020-04-19 07:13:55 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-10-18 12:46:22 +00:00
|
|
|
fn dialogue_from_str() {
|
|
|
|
let output = Dialogue::from_str(
|
|
|
|
"DLG h\nhello\nNAME not a dialogue name\nNAME a dialogue name"
|
|
|
|
).unwrap();
|
2020-04-30 18:44:20 +00:00
|
|
|
|
|
|
|
let expected = Dialogue {
|
|
|
|
id: "h".to_string(),
|
|
|
|
contents: "hello\nNAME not a dialogue name".to_string(),
|
|
|
|
name: Some("a dialogue name".to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
2020-04-19 07:13:55 +00:00
|
|
|
}
|
2020-04-12 12:26:33 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[test]
|
2020-07-26 11:37:41 +00:00
|
|
|
fn dialogue_to_string() {
|
2020-04-19 07:13:55 +00:00
|
|
|
let output = Dialogue {
|
2020-04-12 12:26:33 +00:00
|
|
|
id: "y".to_string(),
|
2020-04-30 18:44:20 +00:00
|
|
|
contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string(),
|
|
|
|
name: Some("a dialogue name".to_string())
|
2020-04-19 07:13:55 +00:00
|
|
|
}.to_string();
|
2020-04-30 18:44:20 +00:00
|
|
|
|
2020-10-18 12:46:22 +00:00
|
|
|
let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah\nNAME a dialogue name";
|
2020-04-30 18:44:20 +00:00
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
assert_eq!(output, expected);
|
|
|
|
}
|
2020-04-12 12:26:33 +00:00
|
|
|
}
|