handle errors for exit
This commit is contained in:
parent
5bfcc9ab4c
commit
e760027ae4
52
src/exit.rs
52
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<String> 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<Self, Self::Err> {
|
||||
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()
|
||||
);
|
||||
}
|
||||
|
|
|
@ -84,7 +84,12 @@ impl From<String> for Room {
|
|||
|
||||
if position.is_ok() {
|
||||
let position = position.unwrap();
|
||||
let exit = Exit::from(format!("{} {}", parts[1], parts[2]));
|
||||
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);
|
||||
|
@ -97,6 +102,7 @@ impl From<String> for Room {
|
|||
}
|
||||
exits.push(ExitInstance { position, exit, transition, dialogue_id });
|
||||
}
|
||||
}
|
||||
} else if last_line.starts_with("END") {
|
||||
let last_line = last_line.replace("END ", "");
|
||||
let ending_position: Vec<&str> = last_line.split(' ').collect();
|
||||
|
|
Loading…
Reference in New Issue