bitsy-parser/src/exit.rs

149 lines
3.6 KiB
Rust
Raw Normal View History

2020-04-18 15:58:30 +00:00
use crate::{from_base36, Position, ToBase36};
2020-04-12 14:40:40 +00:00
#[derive(Debug, Eq, PartialEq)]
pub enum Transition {
2020-04-12 14:40:40 +00:00
None,
FadeToWhite,
FadeToBlack,
Wave,
Tunnel,
SlideUp,
SlideDown,
SlideLeft,
SlideRight,
}
impl From<&str> for Transition {
#[inline]
2020-04-12 14:40:40 +00:00
fn from(str: &str) -> Transition {
match str {
2020-04-18 15:58:30 +00:00
"fade_w" => Transition::FadeToWhite,
"fade_b" => Transition::FadeToBlack,
"wave" => Transition::Wave,
"tunnel" => Transition::Tunnel,
2020-04-12 14:40:40 +00:00
"slide_u" => Transition::SlideUp,
"slide_d" => Transition::SlideDown,
"slide_l" => Transition::SlideLeft,
"slide_r" => Transition::SlideRight,
2020-04-18 15:58:30 +00:00
_ => Transition::None,
2020-04-12 14:40:40 +00:00
}
}
}
impl ToString for Transition {
#[inline]
2020-04-12 14:40:40 +00:00
fn to_string(&self) -> String {
match &self {
Transition::FadeToWhite => " FX fade_w",
Transition::FadeToBlack => " FX fade_b",
2020-04-18 15:58:30 +00:00
Transition::Wave => " FX wave",
Transition::Tunnel => " FX tunnel",
Transition::SlideUp => " FX slide_u",
Transition::SlideDown => " FX slide_d",
Transition::SlideLeft => " FX slide_l",
Transition::SlideRight => " FX slide_r",
Transition::None => "",
}
.to_string()
2020-04-12 14:40:40 +00:00
}
}
2020-04-12 12:49:19 +00:00
#[derive(Debug, Eq, PartialEq)]
pub struct Exit {
/// destination
2020-04-18 15:58:30 +00:00
pub room_id: u64,
/// id
pub position: Position,
pub effect: Transition,
2020-04-12 12:49:19 +00:00
}
impl From<String> for Exit {
#[inline]
2020-04-12 12:49:19 +00:00
fn from(string: String) -> Exit {
2020-04-12 14:40:40 +00:00
// e.g. "EXT 6,4 0 10,12 FX fade_w"
let room_position_effect: Vec<&str> = string.split_whitespace().collect();
2020-04-13 12:51:29 +00:00
let room_id = from_base36(room_position_effect[0]);
2020-04-18 15:12:06 +00:00
let position = Position::from(room_position_effect[1].to_string()).unwrap();
2020-04-12 14:40:40 +00:00
let effect = if room_position_effect.len() == 4 {
Transition::from(room_position_effect[3])
} else {
Transition::None
};
2020-04-12 12:49:19 +00:00
2020-04-18 15:58:30 +00:00
Exit {
room_id,
position,
effect,
}
2020-04-12 12:49:19 +00:00
}
}
impl ToString for Exit {
#[inline]
2020-04-12 12:49:19 +00:00
fn to_string(&self) -> String {
2020-04-13 12:51:29 +00:00
format!(
"{} {}{}",
self.room_id.to_base36(),
2020-04-13 12:51:29 +00:00
self.position.to_string(),
self.effect.to_string()
)
2020-04-12 12:49:19 +00:00
}
}
2020-04-19 07:13:55 +00:00
#[cfg(test)]
mod test {
use crate::exit::{Transition, Exit};
use crate::position::Position;
2020-04-12 14:40:40 +00:00
2020-04-19 07:13:55 +00:00
#[test]
fn test_exit_from_string() {
assert_eq!(
Exit::from("a 12,13".to_string()),
Exit {
room_id: 10,
position: Position { x: 12, y: 13 },
effect: Transition::None
}
);
}
2020-04-12 12:49:19 +00:00
2020-04-19 07:13:55 +00:00
#[test]
fn test_exit_from_string_with_fx() {
assert_eq!(
Exit::from("a 12,13 FX slide_u".to_string()),
Exit {
room_id: 10,
position: Position { x: 12, y: 13 },
effect: Transition::SlideUp
}
);
}
2020-04-12 14:40:40 +00:00
2020-04-19 07:13:55 +00:00
#[test]
fn test_exit_to_string() {
assert_eq!(
Exit {
room_id: 8,
position: Position { x: 5, y: 6 },
effect: Transition::None
}
.to_string(),
"8 5,6".to_string()
);
}
#[test]
fn test_exit_to_string_with_fx() {
assert_eq!(
Exit {
room_id: 8,
position: Position { x: 5, y: 6 },
effect: Transition::FadeToWhite
}
.to_string(),
"8 5,6 FX fade_w".to_string()
);
}
2020-04-12 14:40:40 +00:00
}