diff --git a/src/ending.rs b/src/ending.rs index 1969fec..132b927 100644 --- a/src/ending.rs +++ b/src/ending.rs @@ -1,3 +1,7 @@ +use std::fmt; +use std::error::Error; +use std::str::FromStr; + // same as a dialogue basically #[derive(Debug, Eq, PartialEq)] pub struct Ending { @@ -5,32 +9,36 @@ pub struct Ending { dialogue: String, } -impl From for Ending { - #[inline] - fn from(string: String) -> Ending { - let lines: Vec<&str> = string.lines().collect(); +impl Error for Ending {} + +impl FromStr for Ending { + type Err = String; + + fn from_str(s: &str) -> Result { + let lines: Vec<&str> = s.lines().collect(); let id = lines[0].replace("END ", "").to_string(); let dialogue = lines[1..].join("\n"); - Ending { id, dialogue } + Ok(Ending { id, dialogue }) } } -impl ToString for Ending { +impl fmt::Display for Ending { #[inline] - fn to_string(&self) -> String { - format!("END {}\n{}", self.id, self.dialogue) + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f,"END {}\n{}", self.id, self.dialogue) } } #[cfg(test)] mod test { use crate::ending::Ending; + use std::str::FromStr; #[test] fn test_ending_from_string() { assert_eq!( - Ending::from(include_str!("test-resources/ending").to_string()), + Ending::from_str(include_str!("test-resources/ending")).unwrap(), Ending { id: "a".to_string(), dialogue: "This is a long line of dialogue. Blah blah blah".to_string() diff --git a/src/game.rs b/src/game.rs index 7e43c19..0a9dbb7 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,7 @@ use crate::{Dialogue, Ending, Font, Item, Palette, Room, Sprite, TextDirection, Tile, ToBase36, Variable, transform_line_endings, segments_from_string, is_string_numeric}; use std::error::Error; use loe::TransformMode; +use std::str::FromStr; /// in very early versions of Bitsy, room tiles were defined as single characters /// so, only 36 tiles total. later versions are comma-separated @@ -169,7 +170,10 @@ impl Game { } else if segment.starts_with("DLG ") { dialogues.push(Dialogue::from(segment)); } else if segment.starts_with("END ") { - endings.push(Ending::from(segment)); + let ending = Ending::from_str(&segment); + if ending.is_ok() { + endings.push(ending.unwrap()); + } } else if segment.starts_with("VAR ") { variables.push(Variable::from(segment)); } else if segment.starts_with("FONT ") {