use std::path::{Path, PathBuf};
use std::sync::{OnceLock, RwLock};
fn roots() -> &'static RwLock<Vec<PathBuf>> {
static ROOTS: OnceLock<RwLock<Vec<PathBuf>>> = OnceLock::new();
ROOTS.get_or_init(|| RwLock::new(vec![PathBuf::from("resources")]))
}
pub fn prepend_resource_root<P: AsRef<Path>>(root: P) {
let p = root.as_ref().to_path_buf();
let mut guard = roots().write().expect("resource roots poisoned");
guard.retain(|existing| existing != &p);
guard.insert(0, p);
}
pub fn append_resource_root<P: AsRef<Path>>(root: P) {
let p = root.as_ref().to_path_buf();
let mut guard = roots().write().expect("resource roots poisoned");
if !guard.contains(&p) {
guard.push(p);
}
}
pub fn resource_roots() -> Vec<PathBuf> {
roots().read().expect("resource roots poisoned").clone()
}
pub fn resolve_classpath(resource: &str) -> Option<PathBuf> {
let rel = resource.trim_start_matches('/');
for root in resource_roots() {
let candidate = root.join(rel);
if candidate.is_file() {
return Some(candidate);
}
}
None
}