72 lines
1.8 KiB
Rust
72 lines
1.8 KiB
Rust
use crate::optional_data_line;
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct Dialogue {
|
|
pub id: String,
|
|
pub contents: String,
|
|
pub name: Option<String>,
|
|
}
|
|
|
|
impl From<String> for Dialogue {
|
|
#[inline]
|
|
fn from(string: String) -> Dialogue {
|
|
let mut lines: Vec<&str> = string.lines().collect();
|
|
let id = lines[0].replace("DLG ", "").to_string();
|
|
|
|
let name = if lines.last().unwrap().starts_with("NAME ") {
|
|
Some(lines.pop().unwrap().replace("NAME ", ""))
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let contents = lines[1..].join("\n");
|
|
|
|
Dialogue { id, contents, name }
|
|
}
|
|
}
|
|
|
|
impl ToString for Dialogue {
|
|
#[inline]
|
|
fn to_string(&self) -> String {
|
|
format!(
|
|
"DLG {}\n{}{}",
|
|
self.id,
|
|
self.contents,
|
|
optional_data_line("NAME", self.name.as_ref())
|
|
)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use crate::dialogue::Dialogue;
|
|
|
|
#[test]
|
|
fn test_dialogue_from_string() {
|
|
let output = Dialogue::from(
|
|
"DLG h\nhello\nNAME not a dialogue name\nNAME a dialogue name".to_string()
|
|
);
|
|
|
|
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);
|
|
}
|
|
|
|
#[test]
|
|
fn test_dialogue_to_string() {
|
|
let output = Dialogue {
|
|
id: "y".to_string(),
|
|
contents: "This is a bit of dialogue,\nblah blah\nblah blah".to_string(),
|
|
name: Some("a dialogue name".to_string())
|
|
}.to_string();
|
|
|
|
let expected = "DLG y\nThis is a bit of dialogue,\nblah blah\nblah blah\nNAME a dialogue name".to_string();
|
|
|
|
assert_eq!(output, expected);
|
|
}
|
|
}
|