bitsy-parser/src/exit.rs

146 lines
3.7 KiB
Rust
Raw Normal View History

use crate::Position;
2020-04-29 19:43:49 +00:00
use std::fmt;
2020-04-12 14:40:40 +00:00
2020-05-31 15:12:23 +00:00
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Transition {
2020-04-12 14:40:40 +00:00
None,
FadeToWhite,
FadeToBlack,
Wave,
Tunnel,
SlideUp,
SlideDown,
SlideLeft,
SlideRight,
}
impl Transition {
pub(crate) fn from_str(str: &str) -> Result<Transition, crate::Error> {
2020-04-12 14:40:40 +00:00
match str {
"fade_w" => Ok(Transition::FadeToWhite),
"fade_b" => Ok(Transition::FadeToBlack),
"wave" => Ok(Transition::Wave),
"tunnel" => Ok(Transition::Tunnel),
"slide_u" => Ok(Transition::SlideUp),
"slide_d" => Ok(Transition::SlideDown),
"slide_l" => Ok(Transition::SlideLeft),
"slide_r" => Ok(Transition::SlideRight),
_ => Err(crate::Error::Transition),
2020-04-12 14:40:40 +00:00
}
}
}
2020-10-17 12:55:33 +00:00
impl fmt::Display for Transition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match &self {
2020-04-12 14:40:40 +00:00
Transition::FadeToWhite => " FX fade_w",
Transition::FadeToBlack => " FX fade_b",
2020-10-17 10:38:47 +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 => "",
})
2020-04-12 14:40:40 +00:00
}
}
2020-04-12 12:49:19 +00:00
2020-05-31 15:12:23 +00:00
#[derive(Clone, Debug, Eq, PartialEq)]
2020-04-12 12:49:19 +00:00
pub struct Exit {
/// destination
2020-06-18 13:44:20 +00:00
pub room_id: String,
2020-04-18 15:58:30 +00:00
/// id
pub position: Position,
pub effect: Transition,
2020-04-12 12:49:19 +00:00
}
impl Exit {
pub(crate) fn from_str(s: &str) -> Result<Self, crate::Error> {
let parts: Vec<&str> = s.split_whitespace().collect();
2020-04-29 19:43:49 +00:00
if parts.len() < 2 {
return Err(crate::Error::Exit);
2020-04-29 19:43:49 +00:00
}
let mut parts = parts.iter();
let room_id = parts.next().unwrap().to_string();
let position = Position::from_str(parts.next().unwrap())?;
2020-04-29 19:43:49 +00:00
2020-10-16 15:31:35 +00:00
let effect = if parts.next().is_some() {
Transition::from_str(parts.next().unwrap())?
2020-04-12 14:40:40 +00:00
} else {
Transition::None
};
2020-04-12 12:49:19 +00:00
2020-04-29 19:43:49 +00:00
Ok(Exit { room_id, position, effect })
2020-04-12 12:49:19 +00:00
}
}
2020-04-29 19:43:49 +00:00
impl fmt::Display for Exit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
2020-04-13 12:51:29 +00:00
"{} {}{}",
2020-06-18 13:44:20 +00:00
self.room_id,
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::{Transition, Exit, Position};
2020-04-12 14:40:40 +00:00
2020-04-19 07:13:55 +00:00
#[test]
2020-07-26 11:37:41 +00:00
fn exit_from_string() {
2020-04-19 07:13:55 +00:00
assert_eq!(
2020-04-29 19:43:49 +00:00
Exit::from_str("a 12,13").unwrap(),
2020-04-19 07:13:55 +00:00
Exit {
2020-06-18 13:44:20 +00:00
room_id: "a".to_string(),
2020-04-19 07:13:55 +00:00
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]
2020-07-26 11:37:41 +00:00
fn exit_from_string_with_fx() {
2020-04-19 07:13:55 +00:00
assert_eq!(
2020-04-29 19:43:49 +00:00
Exit::from_str("a 12,13 FX slide_u").unwrap(),
2020-04-19 07:13:55 +00:00
Exit {
2020-06-18 13:44:20 +00:00
room_id: "a".to_string(),
2020-04-19 07:13:55 +00:00
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]
2020-07-26 11:37:41 +00:00
fn exit_to_string() {
2020-04-19 07:13:55 +00:00
assert_eq!(
Exit {
2020-06-18 13:44:20 +00:00
room_id: "8".to_string(),
2020-04-19 07:13:55 +00:00
position: Position { x: 5, y: 6 },
effect: Transition::None
2020-04-29 19:43:49 +00:00
}.to_string(),
2020-04-19 07:13:55 +00:00
"8 5,6".to_string()
);
}
#[test]
2020-07-26 11:37:41 +00:00
fn exit_to_string_with_fx() {
2020-04-19 07:13:55 +00:00
assert_eq!(
Exit {
2020-06-18 13:44:20 +00:00
room_id: "8".to_string(),
2020-04-19 07:13:55 +00:00
position: Position { x: 5, y: 6 },
effect: Transition::FadeToWhite
2020-04-29 19:43:49 +00:00
}.to_string(),
2020-04-19 07:13:55 +00:00
"8 5,6 FX fade_w".to_string()
);
}
2020-04-12 14:40:40 +00:00
}