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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::value::{AeonValue};
use crate::object::{AeonObject};
use std::collections::HashMap;
use crate::{AeonDeserialize,AeonSerialize};

pub trait AeonConvert {
    fn nil(self) -> bool;
    fn bool(self) -> bool;
    fn str(self) -> String;
    fn int(self) -> i64;
    fn double(self) -> f64;
    fn map(self) -> HashMap<String,AeonValue>;
    fn list(self) -> Vec<AeonValue>;
    fn get(&self, prop: &str) -> AeonValue;
    fn remove(&mut self, prop: &str) -> AeonValue;
}

macro_rules! panic_convert(
    ($self:ident, $to:path) => {
        match $self {
            $to(v) => v,
            _ => panic!("Invalid value conversion for {:?}", $self),
        }
    };
);

impl AeonConvert for AeonValue {
    fn nil(self) -> bool {
        match self {
            AeonValue::Nil => true,
            _ => false,
        }
    }

    fn bool(self) -> bool {
        panic_convert!(self, AeonValue::Bool)
    }

    fn str(self) -> String {
        panic_convert!(self, AeonValue::String)
    }

    fn int(self) -> i64 {
        panic_convert!(self, AeonValue::Integer)
    }

    fn double(self) -> f64 {
        panic_convert!(self, AeonValue::Double)
    }

    fn map(self) -> HashMap<String,AeonValue> {
        panic_convert!(self, AeonValue::Map)
    }

    fn list(self) -> Vec<AeonValue> {
        panic_convert!(self, AeonValue::List)
    }

    fn get(&self, prop: &str) -> AeonValue {
        match self {
            AeonValue::Map(v) => {
                if let Some(p) = v.get(prop) {
                    p.clone()
                } else {
                    panic!("Failed to get property {:?}", prop);
                }
            }
            _ => panic!("Failed to get property {:?}", prop),
        }
    }

    fn remove(&mut self, prop: &str) -> AeonValue {
        match self {
            AeonValue::Map(v) => {
                if let Some(p) = v.remove(prop) {
                    p
                } else {
                    panic!("Failed to get property {:?}", prop);
                }
            }
            _ => panic!("Failed to get property {:?}", prop),
        }
    }

}

pub trait AeonObjectConvert {
    fn get(&self, prop: &str) -> AeonValue;
    fn get_path(&self, path: &str) -> AeonValue;
    fn remove(&mut self, prop: &str) -> AeonValue;
    fn remove_path(&mut self, path: &str) -> AeonValue;
}

impl AeonObjectConvert for AeonObject {
    fn get(&self, prop: &str) -> AeonValue {
        return if let Some(p) = self.properties.get(prop) {
            p.value.clone()
        } else {
            panic!("Failed to get property '{}'", prop);
        }
    }

    fn get_path(&self, path: &str) -> AeonValue {
        let fragments = path.split('/');
        let mut iter = fragments.filter(|&f| f != "");
        let mut current : AeonValue;
        if let Some(frag) = iter.next() {
            current = self.get(frag);
        } else {
            panic!("Failed to get property '{}'", path);
        }

        while let Some(frag) = iter.next() {
            current = current.get(frag);
        }
        current
    }

    fn remove(&mut self, prop: &str) -> AeonValue {
        return if let Some(p) = self.properties.remove(prop) {
            p.value
        } else {
            panic!("Failed to get property '{}'", prop);
        }
    }

    fn remove_path(&mut self, path: &str) -> AeonValue {
        let fragments = path.split('/');
        let mut iter = fragments.filter(|&f| f != "");
        let mut current : AeonValue;
        if let Some(frag) = iter.next() {
            current = self.remove(frag);
        } else {
            panic!("Failed to get property '{}'", path);
        }

        while let Some(frag) = iter.next() {
            current = current.remove(frag);
        }
        current
    }
}

macro_rules! gen_deserialize {
    ($ty:ident, $conv:ident) => {
        impl AeonDeserialize for $ty {
            fn from_property(field: AeonValue) -> Self {
                field.$conv() as $ty
            }

            fn from_aeon(_s: String) -> Self {
                unimplemented!()
            }
        }
    };
}

gen_deserialize!(bool, bool);
gen_deserialize!(String, str);
gen_deserialize!(i64, int);
gen_deserialize!(i32, int);
gen_deserialize!(i16, int);
gen_deserialize!(i8, int);
gen_deserialize!(u64, int);
gen_deserialize!(u32, int);
gen_deserialize!(u16, int);
gen_deserialize!(u8, int);
gen_deserialize!(f64, double);
gen_deserialize!(f32, double);

macro_rules! gen_serialize {
    ($ty:ident, $val:ident, $conv:ident) => {
        impl AeonSerialize for $ty {
            fn to_aeon(&self) -> String {
                unimplemented!()
            }

            fn serialize_aeon_property(&self) -> crate::value::AeonValue {
                AeonValue::$val(self.clone() as $conv)
            }

            fn create_macros(_insert_self: bool) -> std::collections::HashMap::<String, crate::object::Macro> {
                std::collections::HashMap::new()
            }
        }
    };
}

gen_serialize!(bool, Bool, bool);
gen_serialize!(String, String, String);
gen_serialize!(i64, Integer, i64);
gen_serialize!(i32, Integer, i64);
gen_serialize!(i16, Integer, i64);
gen_serialize!(i8, Integer, i64);
gen_serialize!(u64, Integer, i64);
gen_serialize!(u32, Integer, i64);
gen_serialize!(u16, Integer, i64);
gen_serialize!(u8, Integer, i64);
gen_serialize!(f64, Double, f64);
gen_serialize!(f32, Double, f64);