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
use serde::Deserialize;
use crate::{
error::{Error, ResultExt},
Asset, SimpleFormat,
};
#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct RonFormat;
impl<T> SimpleFormat<T> for RonFormat
where
T: Asset,
T::Data: for<'a> Deserialize<'a> + Send + Sync + 'static,
{
const NAME: &'static str = "Ron";
type Options = ();
fn import(&self, bytes: Vec<u8>, _: ()) -> Result<T::Data, Error> {
use ron::de::Deserializer;
let mut d =
Deserializer::from_bytes(&bytes).chain_err(|| "Failed deserializing Ron file")?;
let val = T::Data::deserialize(&mut d).chain_err(|| "Failed parsing Ron file")?;
d.end().chain_err(|| "Failed parsing Ron file")?;
Ok(val)
}
}
#[cfg(feature = "json")]
#[derive(Default, Clone, Debug)]
pub struct JsonFormat;
#[cfg(feature = "json")]
impl<T> SimpleFormat<T> for JsonFormat
where
T: Asset,
T::Data: for<'a> Deserialize<'a> + Send + Sync + 'static,
{
const NAME: &'static str = "Json";
type Options = ();
fn import(&self, bytes: Vec<u8>, _: ()) -> Result<T::Data, Error> {
use serde_json::de::Deserializer;
let mut d = Deserializer::from_slice(&bytes);
let val = T::Data::deserialize(&mut d).chain_err(|| "Failed deserializing Json file")?;
d.end().chain_err(|| "Failed parsing Json file")?;
Ok(val)
}
}