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
use crate::Format;
use amethyst_error::{format_err, Error, ResultExt};
use serde::{Deserialize, Serialize};

/// Format for loading from RON files. Mostly useful for prefabs.
/// This type cannot be used for tagged deserialization.
/// It is meant to be used at top-level loading, manually specified to the loader.
/// ```rust,ignore
/// loader.load("prefab.ron", RonFormat, ());
/// ```
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct RonFormat;

impl<D> Format<D> for RonFormat
where
    D: for<'a> Deserialize<'a> + Send + Sync + 'static,
{
    fn name(&self) -> &'static str {
        "Ron"
    }

    fn import_simple(&self, bytes: Vec<u8>) -> Result<D, Error> {
        use ron::de::Deserializer;
        let mut d = Deserializer::from_bytes(&bytes)
            .with_context(|_| format_err!("Failed deserializing Ron file"))?;
        let val =
            D::deserialize(&mut d).with_context(|_| format_err!("Failed parsing Ron file"))?;
        d.end()
            .with_context(|_| format_err!("Failed parsing Ron file"))?;

        Ok(val)
    }
}

/// Format for loading from JSON files. Mostly useful for prefabs.
/// This type can only be used as manually specified to the loader.
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct JsonFormat;

#[cfg(feature = "json")]
impl<D> Format<D> for JsonFormat
where
    D: for<'a> Deserialize<'a> + Send + Sync + 'static,
{
    fn name(&self) -> &'static str {
        "Json"
    }

    fn import_simple(&self, bytes: Vec<u8>) -> Result<D, Error> {
        use serde_json::de::Deserializer;
        let mut d = Deserializer::from_slice(&bytes);
        let val = D::deserialize(&mut d)
            .with_context(|_| format_err!("Failed deserializing Json file"))?;
        d.end()
            .with_context(|_| format_err!("Failed deserializing Json file"))?;

        Ok(val)
    }
}