diff --git a/src/ending.rs b/src/ending.rs index 3f3228c..970c9ab 100644 --- a/src/ending.rs +++ b/src/ending.rs @@ -1,6 +1,4 @@ use std::fmt; -use std::error::Error; -use std::str::FromStr; // same as a dialogue basically #[derive(Clone, Debug, Eq, PartialEq)] @@ -9,13 +7,14 @@ pub struct Ending { pub dialogue: String, } -impl Error for Ending {} - -impl FromStr for Ending { - type Err = String; - - fn from_str(s: &str) -> Result { +impl Ending { + pub fn from_str(s: &str) -> Result { let lines: Vec<&str> = s.lines().collect(); + + if lines.is_empty() || !lines[0].starts_with("END ") { + return Err(crate::Error::Ending); + } + let id = lines[0].replace("END ", ""); let dialogue = lines[1..].join("\n"); @@ -32,7 +31,6 @@ impl fmt::Display for Ending { #[cfg(test)] mod test { use crate::Ending; - use std::str::FromStr; #[test] fn ending_from_string() { diff --git a/src/game.rs b/src/game.rs index 6fe54bc..169e23b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -2,7 +2,6 @@ use crate::{Dialogue, Ending, Font, Image, Item, Palette, Room, Sprite, TextDire use loe::TransformMode; -use std::str::FromStr; use std::collections::HashMap; use std::borrow::BorrowMut; use std::fmt; @@ -226,8 +225,11 @@ impl Game { } else if segment.starts_with("DLG ") { dialogues.push(Dialogue::from(segment)); } else if segment.starts_with("END ") { - if let Ok(ending) = Ending::from_str(&segment) { + let result = Ending::from_str(&segment); + if let Ok(ending) = result { endings.push(ending); + } else { + warnings.push(result.unwrap_err()); } } else if segment.starts_with("VAR ") { variables.push(Variable::from(segment));