07_fallback/
07_fallback.rs1use keket::{
2 database::AssetDatabase,
3 fetch::{fallback::FallbackAssetFetch, file::FileAssetFetch},
4 protocol::text::TextAssetProtocol,
5 third_party::anput::bundle::DynamicBundle,
6};
7use std::error::Error;
8
9fn main() -> Result<(), Box<dyn Error>> {
10 let mut database = AssetDatabase::default()
12 .with_protocol(TextAssetProtocol)
13 .with_fetch(
14 FallbackAssetFetch::new(FileAssetFetch::default().with_root("resources"))
17 .path("text://this-fails-to-load.txt")
19 .path("text://lorem.txt")
21 .factory(|path| {
22 if path.path() == "default.txt" {
23 Some(DynamicBundle::new("default content".to_owned()).unwrap())
24 } else {
25 None
26 }
27 }),
28 );
29
30 let lorem = database.ensure("text://lorem.txt")?;
32
33 let non_existent = database.ensure("text://non-existent.txt")?;
35
36 if lorem.access::<&String>(&database) == non_existent.access::<&String>(&database) {
37 println!("Non existent asset loaded from fallback!");
38 }
39
40 let def = database.ensure("text://default.txt")?;
41 if def.access::<&String>(&database) == "default content" {
42 println!("Default asset loaded from factory!");
43 }
44 Ok(())
47}