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
macro_rules! reference_wrappers {
    ($name:ident) => {
        #[derive(Clone)]
        pub struct $name(Reference);

        impl AsRef<Reference> for $name {
            fn as_ref(&self) -> &Reference {
                &self.0
            }
        }
        impl TryFrom<Value> for $name {
            type Error = <Value as TryInto<Reference>>::Error;

            fn try_from(v: Value) -> Result<$name, Self::Error> {
                Ok($name(v.try_into()?))
            }
        }
    };
    ($($name:ident),* $(,)*) => {
        $(
            reference_wrappers!($name);
        )*
    };
}

macro_rules! simple_accessors {
    ($struct_name:ident; $(($method:ident -> $prop:ident -> $ret:ty)),* $(,)*) => (
        impl $struct_name {
            $(
                pub fn $method(&self) -> $ret {
                    js_unwrap!(@{&self.0}.$prop)
                }
            )*
        }
    );
    (
        $trait_name:ident for $struct_name:ident;
        $(($method:ident -> $prop:ident -> $ret:ty)),* $(,)*
    ) => (
        simple_accessors! {
            $trait_name for $struct_name;
            $(($method -> $prop -> $ret),)*;
        }
    );
    (
        $trait_name:ident for $struct_name:ident;
        $(($method:ident -> $prop:ident -> $ret:ty)),* $(,)*;
        $(
            $extra_func:tt
        )*
    ) => (
        impl $trait_name for $struct_name {
            $(
                fn $method(&self) -> $ret {
                    js_unwrap!(@{&self.0}.$prop)
                }
            )*
            $(
                $extra_func
            )*
        }
    )
}

macro_rules! impl_room_object {
    ($name:ident) => (
        simple_accessors! {
            RoomObjectProperties for $name;
            (pos -> pos -> RoomPosition),
            (room -> room -> Room),
        }
    );

    ($($name:ident),* $(,)*) => (
        $(
            impl_room_object!($name);
        )*
    )
}

macro_rules! impl_structure {
    ($name:ident) => (
        simple_accessors! {
            StructureProperties for $name;
            (hits -> hits -> i32),
            (hits_max -> hitsMax -> i32),
            (id -> id -> String),
            (is_active -> isActive -> bool);

            fn destroy(&self) -> ReturnCode {
                js_unwrap!(@{&self.0}.destroy())
            }
            fn structure_type(&self) -> StructureType {
                js_unwrap!(@{&self.0}.structureType)
            }
        }
    );

    ($($name:ident),* $(,)*) => (
        $(
            impl_structure!($name);
        )*
    )
}

macro_rules! impl_owned_structure {
    ($name:ident) => (
        simple_accessors! {
            OwnedStructureProperties for $name;
            (my -> my -> bool);
            fn owner(&self) -> Option<String> {
                (js! {
                    var self = @{&self.0};
                    if (self.owner) {
                        return self.owner.username;
                    } else {
                        return null;
                    }
                }).try_into().expect("expected OwnerStructure.owner.username to be a string")
            }
        }
    );

    ($($name:ident),* $(,)*) => (
        $(
            impl_owned_structure!($name);
        )*
    )
}