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
use crate::Position;

#[derive(Debug, Eq, PartialEq)]
pub struct Exit {
    /// destination
    pub(crate) room: String,  /// id
    pub(crate) position: Position,
}

impl From<String> for Exit {
    fn from(string: String) -> Exit {
        // e.g. "4 3,3"
        let room_position: Vec<&str> = string.split(' ').collect();
        let room = room_position[0].to_string();
        let position = Position::from(room_position[1].to_string());

        Exit { room, position }
    }
}

impl ToString for Exit {
    fn to_string(&self) -> String {
        format!("{} {}", self.room, self.position.to_string())
    }
}

#[test]
fn test_exit_from_string() {
    assert_eq!(
        Exit::from("a 12,13".to_string()),
        Exit { room: "a".to_string(), position: Position { x: 12, y: 13 } }
    );
}

#[test]
fn test_exit_to_string() {
    assert_eq!(
        Exit { room: "8".to_string(), position: Position { x: 5, y: 6 } }.to_string(),
        "8 5,6".to_string()
    );
}