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
#![feature(custom_derive, plugin)]
#![plugin(serde_macros)]

extern crate serde;
extern crate serde_json;

use serde::{Deserialize, Deserializer};

pub mod layer;
pub mod level;
pub mod tileset;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct GlobalTile(u32);

impl GlobalTile {
    /// From this GlobalTile, given the set of tilesets associated with the
    /// map, find the Tileset and LocalTile this ID belongs to, or None
    /// if it does not belong to any.
    pub fn find_local(self, sets: &[tileset::Tileset]) -> Option<(&tileset::Tileset, LocalTile)> {
        for set in sets {
            if set.contains_tile(self) {
                let id = LocalTile(self.0 - set.firstgid.0);
                return Some((set, id))
            }
        }
        None
    }
}

impl Deserialize for GlobalTile {
    fn deserialize<D: Deserializer>(d: &mut D) -> Result<Self, D::Error> {
        // These are just wrapper structs, the values
        // should be decoded as a plain u32
        Ok(GlobalTile(try!(u32::deserialize(d))))
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct LocalTile(u32);

impl Deserialize for LocalTile {
    fn deserialize<D: Deserializer>(d: &mut D) -> Result<Self, D::Error> {
        // These are just wrapper structs, the values
        // should be decoded as a plain u32
        Ok(LocalTile(try!(u32::deserialize(d))))
    }
}