oxygengine_core/assets/protocols/
toml.rs1use crate::assets::protocol::{AssetLoadResult, AssetProtocol};
2use serde::de::DeserializeOwned;
3use std::str::from_utf8;
4use toml::Value;
5
6pub struct TomlAsset(Value);
7
8impl TomlAsset {
9 pub fn get(&self) -> &Value {
10 &self.0
11 }
12
13 pub fn deserialize<T>(&self) -> Result<T, String>
14 where
15 T: DeserializeOwned,
16 {
17 match self.0.clone().try_into() {
18 Ok(result) => Ok(result),
19 Err(error) => Err(error.to_string()),
20 }
21 }
22}
23
24pub struct TomlAssetProtocol;
25
26impl AssetProtocol for TomlAssetProtocol {
27 fn name(&self) -> &str {
28 "toml"
29 }
30
31 fn on_load(&mut self, data: Vec<u8>) -> AssetLoadResult {
32 let data = from_utf8(&data).unwrap();
33 match toml::from_str(data) {
34 Ok(value) => AssetLoadResult::Data(Box::new(TomlAsset(value))),
35 Err(error) => AssetLoadResult::Error(format!("Error loading TOML asset: {:?}", error)),
36 }
37 }
38}