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
use crate::{AnimationFrames, Image, Position, mock};

#[derive(Debug, Eq, PartialEq)]
pub struct Sprite {
    pub(crate) id: String, // lowercase base36
    pub(crate) name: Option<String>,
    pub(crate) animation_frames: Vec<Image>,
    pub(crate) dialogue: Option<String>, /// dialogue id
    pub(crate) room: String, /// room id
    pub(crate) position: Position,
}

impl From<String> for Sprite {
    fn from(string: String) -> Sprite {
        let mut lines: Vec<&str> = string.lines().collect();

        let id = lines[0].replace("SPR ", "");
        let mut name = None;
        let mut dialogue = None;
        let mut room: Option<String> = None;
        let mut position: Option<Position> = None;

        for _ in 0..3 {
            let last_line = lines.pop().unwrap();

            if last_line.starts_with("NAME") {
                name = Some(last_line.replace("NAME ", "").to_string());
            } else if last_line.starts_with("DLG") {
                dialogue = Some(last_line.replace("DLG ", "").to_string());
            } else if last_line.starts_with("POS") {
                let last_line = last_line.replace("POS ", "");
                let room_position: Vec<&str> = last_line.split(' ').collect();
                room = Some(room_position[0].to_string());
                position = Some(Position::from(room_position[1].to_string()));
            } else {
                lines.push(last_line);
                break;
            }
        }

        let room = room.unwrap();
        let position = position.unwrap();

        // todo dedupe
        let animation_frames = lines[1..].join("");
        let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
        let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
            Image::from(frame.to_string())
        }).collect();

        Sprite { id, name, animation_frames, dialogue, room, position }
    }
}

impl ToString for Sprite {
    #[inline]
    fn to_string(&self) -> String {
        format!(
            "SPR {}\n{}{}{}\nPOS {} {}",
            self.id,
            self.animation_frames.to_string(),
            if self.name.as_ref().is_some() { format!("\nNAME {}", self.name.as_ref().unwrap()) } else { "".to_string() },
            if self.dialogue.as_ref().is_some() { format!("\nDLG {}", self.dialogue.as_ref().unwrap()) } else { "".to_string() },
            self.room,
            self.position.to_string(),
        )
    }
}

#[test]
fn test_sprite_from_string() {
    let output = Sprite::from(
        include_str!("../test/resources/sprite").to_string()
    );

    let expected = mock::sprite();

    assert_eq!(output, expected);
}

#[test]
fn test_sprite_to_string() {
    assert_eq!(mock::sprite().to_string(), include_str!("../test/resources/sprite").to_string());
}