use crate::error::CallmError;
use crate::loaders::{LoaderGguf, LoaderImpl, LoaderSafetensors};
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
pub fn autodetect_loader(path: &str) -> Result<Arc<Mutex<dyn LoaderImpl>>, CallmError> {
let metadata = fs::metadata(path)?;
if metadata.is_file() {
let pthbuf = PathBuf::from(path);
match pthbuf
.as_path()
.extension()
.expect("Unable to extract file extension name")
.to_str()
.expect("Unable to convert file extension name")
{
"gguf" => {
return Ok(Arc::new(Mutex::new(LoaderGguf::new(path))));
}
"ggml" => todo!("GGML format is not supported, yet."),
"safetensors" => return Ok(Arc::new(Mutex::new(LoaderSafetensors::new(path)))),
_ => {
if let Some(parent) = pthbuf.parent() {
return autodetect_loader(parent.to_str().unwrap());
}
}
}
} else {
return Ok(Arc::new(Mutex::new(LoaderSafetensors::new(path))));
}
Err(CallmError::LoaderFail(
"No suitable loader found".to_string(),
))
}