Skip to main content

hello_redb/
hello_redb.rs

1use keket::{
2    database::AssetDatabase,
3    fetch::container::ContainerAssetFetch,
4    protocol::{bundle::BundleAssetProtocol, bytes::BytesAssetProtocol, text::TextAssetProtocol},
5};
6use keket_redb::{RedbContainerPartialFetch, third_party::redb::Database};
7use serde_json::Value;
8use std::error::Error;
9
10fn main() -> Result<(), Box<dyn Error>> {
11    /* ANCHOR: main */
12    let mut database = AssetDatabase::default()
13        .with_protocol(TextAssetProtocol)
14        .with_protocol(BytesAssetProtocol)
15        .with_protocol(BundleAssetProtocol::new("json", |bytes: Vec<u8>| {
16            Ok((serde_json::from_slice::<Value>(&bytes)?,).into())
17        }))
18        .with_fetch(ContainerAssetFetch::new(RedbContainerPartialFetch::new(
19            Database::create("./resources/database.redb")?,
20            "assets",
21        )));
22
23    let lorem = database.ensure("text://lorem.txt")?;
24    println!("Lorem Ipsum: {}", lorem.access::<&String>(&database));
25
26    let json = database.ensure("json://person.json")?;
27    println!("JSON: {:#}", json.access::<&Value>(&database));
28
29    let trash = database.ensure("bytes://trash.bin")?;
30    println!("Bytes: {:?}", trash.access::<&Vec<u8>>(&database));
31    /* ANCHOR_END: main */
32
33    Ok(())
34}