reactive_graph_plugin_api/
embedded_asset_provider.rs

1// TODO: remove this macro once the migration has been done
2
3#[macro_export]
4macro_rules! embedded_asset_provider_impl {
5    ($asset: ident, $tys: ident) => {{
6        let mut entries = <$tys as reactive_graph_graph::NamespacedTypeContainer>::new();
7        for file in $asset::iter() {
8            let filename = file.as_ref();
9            if filename.starts_with(".") {
10                // do nothing
11                continue;
12            }
13            log::debug!("Loading resource {}", filename);
14            match $asset::get(filename) {
15                Some(asset) => match std::str::from_utf8(asset.data.as_ref()) {
16                    Ok(asset_str) => {
17                        if filename.ends_with(".json") {
18                            match serde_json::from_str(asset_str) {
19                                Ok(parsed_entry) => {
20                                    let entry: <$tys as reactive_graph_graph::NamespacedTypeContainer>::Type = parsed_entry;
21                                    reactive_graph_graph::NamespacedTypeContainer::push(&entries, entry);
22                                }
23                                Err(e) => log::error!("Error in parsing JSON file {}: {}", filename, e),
24                            }
25                        } else if filename.ends_with(".json5") {
26                            match json5::from_str(asset_str) {
27                                Ok(parsed_entry) => {
28                                    let entry: <$tys as reactive_graph_graph::NamespacedTypeContainer>::Type = parsed_entry;
29                                    reactive_graph_graph::NamespacedTypeContainer::push(&entries, entry);
30                                }
31                                Err(e) => log::error!("Error in parsing JSON5 file {}: {}", filename, e),
32                            }
33                        } else if filename.ends_with(".toml") {
34                            match toml::from_str(asset_str) {
35                                Ok(parsed_entry) => {
36                                    let entry: <$tys as reactive_graph_graph::NamespacedTypeContainer>::Type = parsed_entry;
37                                    reactive_graph_graph::NamespacedTypeContainer::push(&entries, entry);
38                                }
39                                Err(e) => log::error!("Error in parsing TOML file {}: {}", filename, e),
40                            }
41                        } else {
42                            log::error!("Can't read type definition {}: Only JSON, JSON5 and TOML are supported.", filename);
43                        }
44                    }
45                    Err(e) => log::error!("Error in decoding file to UTF-8 {}: {}", filename, e),
46                },
47                None => {}
48            }
49        }
50        entries
51    }};
52}