use keket::{
database::{AssetDatabase, path::AssetPathStatic},
fetch::file::FileAssetFetch,
protocol::text::TextAssetProtocol,
store::future::FutureAssetStore,
};
use std::{error::Error, path::PathBuf};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut database = AssetDatabase::default()
.with_protocol(TextAssetProtocol)
.with_fetch(FileAssetFetch::default().with_root("resources"))
.with_store(FutureAssetStore::new(tokio_save_file));
let _ = tokio::fs::remove_file("./resources/saved2.txt").await;
let before = database.spawn("text://saved2.txt", ("Abra cadabra!".to_owned(),))?;
println!("Before: {}", before.access::<&String>(&database));
before.store(&mut database)?;
while database.is_busy() {
database.maintain()?;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
}
before.delete(&mut database);
assert!(!before.does_exists(&database));
let after = database.ensure("text://saved2.txt")?;
println!("After: {}", after.access::<&String>(&database));
Ok(())
}
async fn tokio_save_file(path: AssetPathStatic, bytes: Vec<u8>) -> Result<(), Box<dyn Error>> {
let file_path = PathBuf::from("resources").join(path.path());
tokio::fs::create_dir_all(file_path.parent().unwrap()).await?;
tokio::fs::write(&file_path, bytes).await?;
Ok(())
}