hello_graph/
hello_graph.rs1use keket::{
2 database::{AssetDatabase, path::AssetPathStatic},
3 fetch::file::FileAssetFetch,
4 protocol::bundle::BundleAssetProtocol,
5};
6use keket_graph::{
7 node::AssetNode,
8 protocol::{AssetTree, AssetTreeProcessor},
9};
10use keket_graph_derive::AssetTree;
11use serde::{Deserialize, Serialize};
12use std::error::Error;
13
14fn main() -> Result<(), Box<dyn Error>> {
15 let mut database = AssetDatabase::default()
17 .with_protocol(BundleAssetProtocol::new(
18 "custom",
19 AssetTreeProcessor::<CustomAsset>::new(|bytes| {
23 Ok(serde_json::from_slice::<CustomAsset>(&bytes)?)
24 }),
25 ))
26 .with_fetch(FileAssetFetch::default().with_root("resources"));
27
28 let asset = AssetNode::<CustomAsset>::new("custom://part1.json");
30 asset.ensure(&mut database)?;
31
32 while database.is_busy() {
34 database.maintain()?;
35 }
36
37 let contents = asset
40 .resolve(&database)?
41 .read_unchecked()
42 .contents(&database);
43 println!("Custom chain contents: {contents:?}");
44 Ok(())
46}
47
48#[derive(Debug, Default, Serialize, Deserialize, AssetTree)]
54struct CustomAsset {
55 content: String,
56 #[serde(default)]
59 #[asset_deps]
60 next: Option<AssetNode<CustomAsset>>,
61}
62
63impl CustomAsset {
64 fn contents(&self, database: &AssetDatabase) -> String {
65 let mut result = self.content.as_str().to_owned();
66 if let Some(next) = self.next.as_ref() {
67 result.push(' ');
68 if let Ok(resolved) = next.resolve(database) {
69 result.push_str(&resolved.read_unchecked().contents(database));
70 }
71 }
72 result
73 }
74}
75