From 67d4e28773222750003cfbb4139146162150a4d6 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 18 Oct 2020 13:51:42 +0100 Subject: [PATCH] actually throw error on bad dialogue --- src/dialogue.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/dialogue.rs b/src/dialogue.rs index 09660f5..4cdcc82 100644 --- a/src/dialogue.rs +++ b/src/dialogue.rs @@ -12,11 +12,19 @@ pub struct Dialogue { impl Dialogue { pub fn from_str(str: &str) -> Result { 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 };