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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
use crate::{Prefab, PrefabError, PrefabValue};
use serde::{Deserialize, Serialize};
use std::{
    any::{type_name, Any, TypeId},
    collections::HashMap,
};

type PropsSerializeFactory =
    Box<dyn Fn(&dyn PropsData) -> Result<PrefabValue, PrefabError> + Send + Sync>;
type PropsDeserializeFactory =
    Box<dyn Fn(PrefabValue, &mut Props) -> Result<(), PrefabError> + Send + Sync>;

#[derive(Default)]
pub struct PropsRegistry {
    type_mapping: HashMap<TypeId, String>,
    factories: HashMap<String, (PropsSerializeFactory, PropsDeserializeFactory)>,
}

impl PropsRegistry {
    pub fn register_factory<T>(&mut self, name: &str)
    where
        T: 'static + Prefab + PropsData,
    {
        let s: PropsSerializeFactory = Box::new(move |data| {
            if let Some(data) = data.as_any().downcast_ref::<T>() {
                data.to_prefab()
            } else {
                Err(PrefabError::CouldNotSerialize(
                    "Could not downcast to concrete type!".to_owned(),
                ))
            }
        });
        let d: PropsDeserializeFactory = Box::new(move |data, props| {
            props.write(T::from_prefab(data)?);
            Ok(())
        });
        self.factories.insert(name.to_owned(), (s, d));
        self.type_mapping.insert(TypeId::of::<T>(), name.to_owned());
    }

    pub fn unregister_factory(&mut self, name: &str) {
        self.factories.remove(name);
    }

    pub fn serialize(&self, props: &Props) -> Result<PrefabValue, PrefabError> {
        let mut group = PropsGroupPrefab::default();
        for (t, p) in &props.0 {
            if let Some(name) = self.type_mapping.get(t) {
                if let Some(factory) = self.factories.get(name) {
                    group.data.insert(name.to_owned(), (factory.0)(p.as_ref())?);
                }
            } else {
                return Err(PrefabError::CouldNotSerialize(
                    "No type mapping found!".to_owned(),
                ));
            }
        }
        group.to_prefab()
    }

    pub fn deserialize(&self, data: PrefabValue) -> Result<Props, PrefabError> {
        let data = if data.is_null() {
            PropsGroupPrefab::default()
        } else {
            PropsGroupPrefab::from_prefab(data)?
        };
        let mut props = Props::default();
        for (key, value) in data.data {
            if let Some(factory) = self.factories.get(&key) {
                (factory.1)(value, &mut props)?;
            } else {
                return Err(PrefabError::CouldNotDeserialize(format!(
                    "Could not find properties factory: {:?}",
                    key
                )));
            }
        }
        Ok(props)
    }
}

#[derive(Debug, Clone)]
pub enum PropsError {
    CouldNotReadData,
    HasNoDataOfType(String),
}

impl Prefab for PrefabValue {}

impl PropsData for PrefabValue
where
    Self: Clone,
{
    fn clone_props(&self) -> Box<dyn PropsData> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct PropsGroupPrefab {
    #[serde(default)]
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub data: HashMap<String, PrefabValue>,
}

impl Prefab for PropsGroupPrefab {}

impl PropsData for PropsGroupPrefab
where
    Self: Clone,
{
    fn clone_props(&self) -> Box<dyn PropsData> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

pub trait PropsData: std::fmt::Debug + Send + Sync {
    fn clone_props(&self) -> Box<dyn PropsData>;
    fn as_any(&self) -> &dyn Any;
}

impl Clone for Box<dyn PropsData> {
    fn clone(&self) -> Self {
        self.clone_props()
    }
}

#[derive(Default, Clone)]
pub struct Props(HashMap<TypeId, Box<dyn PropsData>>);

impl Props {
    pub fn new<T>(data: T) -> Self
    where
        T: 'static + PropsData,
    {
        let mut result = HashMap::with_capacity(1);
        result.insert(TypeId::of::<T>(), Box::new(data) as Box<dyn PropsData>);
        Self(result)
    }

    pub fn has<T>(&self) -> bool
    where
        T: 'static + PropsData,
    {
        let e = TypeId::of::<T>();
        self.0.iter().any(|(t, _)| *t == e)
    }

    pub fn consume<T>(&mut self) -> Result<Box<dyn PropsData>, PropsError>
    where
        T: 'static + PropsData,
    {
        if let Some(v) = self.0.remove(&TypeId::of::<T>()) {
            Ok(v)
        } else {
            Err(PropsError::HasNoDataOfType(type_name::<T>().to_owned()))
        }
    }

    pub fn read<T>(&self) -> Result<&T, PropsError>
    where
        T: 'static + PropsData,
    {
        let e = TypeId::of::<T>();
        if let Some((_, v)) = self.0.iter().find(|(t, _)| **t == e) {
            if let Some(data) = v.as_any().downcast_ref::<T>() {
                Ok(data)
            } else {
                Err(PropsError::CouldNotReadData)
            }
        } else {
            Err(PropsError::HasNoDataOfType(type_name::<T>().to_owned()))
        }
    }

    pub fn map_or_default<T, R, F>(&self, mut f: F) -> R
    where
        T: 'static + PropsData,
        R: Default,
        F: FnMut(&T) -> R,
    {
        match self.read() {
            Ok(data) => f(data),
            Err(_) => R::default(),
        }
    }

    pub fn map_or_else<T, R, F, E>(&self, mut f: F, mut e: E) -> R
    where
        T: 'static + PropsData,
        F: FnMut(&T) -> R,
        E: FnMut() -> R,
    {
        match self.read() {
            Ok(data) => f(data),
            Err(_) => e(),
        }
    }

    pub fn read_cloned<T>(&self) -> Result<T, PropsError>
    where
        T: 'static + PropsData + Clone,
    {
        self.read::<T>().map(|v| v.clone())
    }

    pub fn read_cloned_or_default<T>(&self) -> T
    where
        T: 'static + PropsData + Clone + Default,
    {
        self.read_cloned().unwrap_or_default()
    }

    pub fn read_cloned_or_else<T, F>(&self, mut f: F) -> T
    where
        T: 'static + PropsData + Clone + Default,
        F: FnMut() -> T,
    {
        self.read_cloned().unwrap_or_else(|_| f())
    }

    pub fn write<T>(&mut self, data: T)
    where
        T: 'static + PropsData,
    {
        self.0
            .insert(TypeId::of::<T>(), Box::new(data) as Box<dyn PropsData>);
    }

    pub fn mutate<T, F>(&mut self, mut f: F)
    where
        T: 'static + PropsData,
        F: FnMut(&T) -> T,
    {
        if let Ok(data) = self.read() {
            let data = f(data);
            self.write(data);
        }
    }

    pub fn mutate_cloned<T, F>(&mut self, mut f: F)
    where
        T: 'static + PropsData + Clone,
        F: FnMut(&mut T),
    {
        if let Ok(data) = self.read::<T>() {
            let mut data = data.clone();
            f(&mut data);
            self.write(data);
        }
    }

    pub fn with<T>(mut self, data: T) -> Self
    where
        T: 'static + PropsData,
    {
        self.write(data);
        self
    }

    pub fn without<T>(mut self) -> Self
    where
        T: 'static + PropsData,
    {
        self.0.remove(&TypeId::of::<T>());
        self
    }

    pub fn merge(self, other: Self) -> Self {
        let mut result = self.into_inner();
        result.extend(other.into_inner());
        Self(result)
    }

    pub fn merge_from(&mut self, other: Self) {
        self.0.extend(other.into_inner());
    }

    pub(crate) fn into_inner(self) -> HashMap<TypeId, Box<dyn PropsData>> {
        self.0
    }
}

impl std::fmt::Debug for Props {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Props ")?;
        f.debug_set().entries(self.0.values()).finish()
    }
}

impl<T> From<T> for Props
where
    T: 'static + PropsData,
{
    fn from(data: T) -> Self {
        Self::new(data)
    }
}

impl From<&Self> for Props {
    fn from(data: &Self) -> Self {
        data.clone()
    }
}

#[macro_export]
macro_rules! implement_props_data {
    ($type_name:ty) => {
        impl $crate::props::PropsData for $type_name
        where
            Self: Clone,
        {
            fn clone_props(&self) -> Box<dyn $crate::props::PropsData> {
                Box::new(self.clone())
            }

            fn as_any(&self) -> &dyn std::any::Any {
                self
            }
        }

        impl $crate::Prefab for $type_name {}
    };
}

implement_props_data!(());
implement_props_data!(i8);
implement_props_data!(i16);
implement_props_data!(i32);
implement_props_data!(i64);
implement_props_data!(i128);
implement_props_data!(u8);
implement_props_data!(u16);
implement_props_data!(u32);
implement_props_data!(u64);
implement_props_data!(u128);
implement_props_data!(f32);
implement_props_data!(f64);
implement_props_data!(bool);
implement_props_data!(String);