use std::path::PathBuf;
#[derive(rust_embed::RustEmbed)]
#[folder = "ui"]
struct Assets;
pub fn extract_to_cache() -> Option<PathBuf> {
let cache_root = dirs::cache_dir()?
.join("wyvern")
.join(env!("CARGO_PKG_VERSION"))
.join("ui");
if is_valid_ui_root(&cache_root) {
return Some(cache_root);
}
for path in Assets::iter() {
let dest = cache_root.join(path.as_ref());
if let Some(parent) = dest.parent() {
if std::fs::create_dir_all(parent).is_err() {
return None;
}
}
let embedded = Assets::get(path.as_ref())?;
if std::fs::write(&dest, embedded.data.as_ref()).is_err() {
return None;
}
}
if is_valid_ui_root(&cache_root) {
Some(cache_root)
} else {
None
}
}
fn is_valid_ui_root(root: &std::path::Path) -> bool {
["message", "input", "markdown", "question", "chrome"]
.iter()
.all(|t| root.join(t).join("index.html").is_file())
}