diff --git a/src/exit.rs b/src/exit.rs index fecddff..a1cffec 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -1,5 +1,7 @@ use crate::{from_base36, Position, ToBase36}; use std::str::FromStr; +use std::error::Error; +use std::fmt; #[derive(Debug, Eq, PartialEq)] pub enum Transition { @@ -58,32 +60,37 @@ pub struct Exit { pub effect: Transition, } -impl From for Exit { - #[inline] - fn from(string: String) -> Exit { - // e.g. "EXT 6,4 0 10,12 FX fade_w" - let room_position_effect: Vec<&str> = string.split_whitespace().collect(); - let room_id = from_base36(room_position_effect[0]); - let position = Position::from_str(room_position_effect[1]).unwrap(); +impl Error for Exit {} - let effect = if room_position_effect.len() == 4 { - Transition::from(room_position_effect[3]) +impl FromStr for Exit { + type Err = String; + + fn from_str(s: &str) -> Result { + let mut parts = s.split_whitespace(); + let room_id = from_base36(parts.next().unwrap()); + let position = Position::from_str(parts.next().unwrap()); + + if position.is_err() { + return Err("Invalid position for exit".to_string()); + } + + let position = position.unwrap(); + + let effect = if parts.next().is_some() { + Transition::from(parts.next().unwrap()) } else { Transition::None }; - Exit { - room_id, - position, - effect, - } + Ok(Exit { room_id, position, effect }) } } -impl ToString for Exit { +impl fmt::Display for Exit { #[inline] - fn to_string(&self) -> String { - format!( + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, "{} {}{}", self.room_id.to_base36(), self.position.to_string(), @@ -96,11 +103,12 @@ impl ToString for Exit { mod test { use crate::exit::{Transition, Exit}; use crate::position::Position; + use std::str::FromStr; #[test] fn test_exit_from_string() { assert_eq!( - Exit::from("a 12,13".to_string()), + Exit::from_str("a 12,13").unwrap(), Exit { room_id: 10, position: Position { x: 12, y: 13 }, @@ -112,7 +120,7 @@ mod test { #[test] fn test_exit_from_string_with_fx() { assert_eq!( - Exit::from("a 12,13 FX slide_u".to_string()), + Exit::from_str("a 12,13 FX slide_u").unwrap(), Exit { room_id: 10, position: Position { x: 12, y: 13 }, @@ -128,8 +136,7 @@ mod test { room_id: 8, position: Position { x: 5, y: 6 }, effect: Transition::None - } - .to_string(), + }.to_string(), "8 5,6".to_string() ); } @@ -141,8 +148,7 @@ mod test { room_id: 8, position: Position { x: 5, y: 6 }, effect: Transition::FadeToWhite - } - .to_string(), + }.to_string(), "8 5,6 FX fade_w".to_string() ); } diff --git a/src/room.rs b/src/room.rs index 29d4f5c..132bdb4 100644 --- a/src/room.rs +++ b/src/room.rs @@ -84,18 +84,24 @@ impl From for Room { if position.is_ok() { let position = position.unwrap(); - let exit = Exit::from(format!("{} {}", parts[1], parts[2])); - let mut transition = None; - let mut dialogue_id = None; - let chunks = parts[3..].chunks(2); - for chunk in chunks { - if chunk[0] == "FX" { - transition = Some(Transition::from(chunk[1])); - } else if chunk[0] == "DLG" { - dialogue_id = Some(chunk[1].to_string()); + let exit = Exit::from_str( + &format!("{} {}", parts[1], parts[2]) + ); + + if exit.is_ok() { + let exit = exit.unwrap(); + let mut transition = None; + let mut dialogue_id = None; + let chunks = parts[3..].chunks(2); + for chunk in chunks { + if chunk[0] == "FX" { + transition = Some(Transition::from(chunk[1])); + } else if chunk[0] == "DLG" { + dialogue_id = Some(chunk[1].to_string()); + } } + exits.push(ExitInstance { position, exit, transition, dialogue_id }); } - exits.push(ExitInstance { position, exit, transition, dialogue_id }); } } else if last_line.starts_with("END") { let last_line = last_line.replace("END ", "");