1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::{from_base36, Position, ToBase36};
use std::str::FromStr;
use std::error::Error;
use std::fmt;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Transition {
    None,
    FadeToWhite,
    FadeToBlack,
    Wave,
    Tunnel,
    SlideUp,
    SlideDown,
    SlideLeft,
    SlideRight,
}

impl From<&str> for Transition {
    #[inline]
    fn from(str: &str) -> Transition {
        match str {
            "fade_w" => Transition::FadeToWhite,
            "fade_b" => Transition::FadeToBlack,
            "wave" => Transition::Wave,
            "tunnel" => Transition::Tunnel,
            "slide_u" => Transition::SlideUp,
            "slide_d" => Transition::SlideDown,
            "slide_l" => Transition::SlideLeft,
            "slide_r" => Transition::SlideRight,
            _ => Transition::None,
        }
    }
}

impl ToString for Transition {
    #[inline]
    fn to_string(&self) -> String {
        match &self {
            Transition::FadeToWhite => " FX fade_w",
            Transition::FadeToBlack => " FX fade_b",
            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()
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Exit {
    /// destination
    pub room_id: u64,
    /// id
    pub position: Position,
    pub effect: Transition,
}

impl Error for Exit {}

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
        };

        Ok(Exit { room_id, position, effect })
    }
}

impl fmt::Display for Exit {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {}{}",
            self.room_id.to_base36(),
            self.position.to_string(),
            self.effect.to_string()
        )
    }
}

#[cfg(test)]
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_str("a 12,13").unwrap(),
            Exit {
                room_id: 10,
                position: Position { x: 12, y: 13 },
                effect: Transition::None
            }
        );
    }

    #[test]
    fn test_exit_from_string_with_fx() {
        assert_eq!(
            Exit::from_str("a 12,13 FX slide_u").unwrap(),
            Exit {
                room_id: 10,
                position: Position { x: 12, y: 13 },
                effect: Transition::SlideUp
            }
        );
    }

    #[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()
        );
    }
}