actually throw error on bad dialogue

This commit is contained in:
Max Bradbury 2020-10-18 13:51:42 +01:00
parent d8183e29fc
commit 67d4e28773
1 changed files with 10 additions and 2 deletions

View File

@ -12,11 +12,19 @@ pub struct Dialogue {
impl Dialogue {
pub fn from_str(str: &str) -> Result<Dialogue, crate::Error> {
let mut lines: Vec<&str> = str.lines().collect();
if lines.is_empty() || !lines[0].starts_with("DLG ") {
return Err(crate::Error::Dialogue);
}
let id = lines[0].replace("DLG ", "");
let name = if lines.last().unwrap().starts_with("NAME ") {
Some(lines.pop().unwrap().replace("NAME ", ""))
let last_line = lines.pop().unwrap();
let name = if last_line.starts_with("NAME ") {
Some(last_line.replace("NAME ", ""))
} else {
lines.push(last_line);
None
};