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
use crate::{from_base36, optional_data_line, AnimationFrames, Image, ToBase36};
use crate::image::animation_frames_from_string;

#[derive(Debug, Eq, PartialEq)]
pub struct Item {
    pub id: u64,
    pub animation_frames: Vec<Image>,
    pub name: Option<String>,
    pub dialogue_id: Option<String>,
    pub colour_id: Option<u64>,
}

impl Item {
    #[inline]
    fn name_line(&self) -> String {
        optional_data_line("NAME", self.name.as_ref())
    }

    #[inline]
    fn dialogue_line(&self) -> String {
        optional_data_line("DLG", self.dialogue_id.as_ref())
    }

    #[inline]
    fn colour_line(&self) -> String {
        optional_data_line("COL", self.colour_id.as_ref())
    }
}

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

        let id = from_base36(&lines[0].replace("ITM ", ""));
        let mut name = None;
        let mut dialogue_id = None;
        let mut colour_id: Option<u64> = None;

        loop {
            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_id = Some(last_line.replace("DLG ", "").to_string());
            } else if last_line.starts_with("COL") {
                colour_id = Some(last_line.replace("COL ", "").parse().unwrap());
            } else {
                lines.push(last_line);
                break;
            }
        }

        let animation_frames = animation_frames_from_string(
            lines[1..].join("\n")
        );

        Item {
            id,
            name,
            animation_frames,
            dialogue_id,
            colour_id,
        }
    }
}

impl ToString for Item {
    #[inline]
    fn to_string(&self) -> String {
        format!(
            "ITM {}\n{}{}{}{}",
            self.id.to_base36(),
            self.animation_frames.to_string(),
            self.name_line(),
            self.dialogue_line(),
            self.colour_line(),
        )
    }
}

#[cfg(test)]
mod test {
    use crate::item::Item;
    use crate::mock;

    #[test]
    fn test_item_from_string() {
        let output = Item::from(include_str!("test-resources/item").to_string());
        let expected = mock::item();
        assert_eq!(output, expected);
    }

    #[test]
    fn test_item_to_string() {
        let output = mock::item().to_string();
        let expected = include_str!("test-resources/item").to_string();
        assert_eq!(output, expected);
    }
}