mod root;
#[cfg(test)]
mod tests;
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
#[derive(Clone)]
pub struct HubConfig {
pub cache: PathBuf,
pub offline: bool,
pub endpoint: Option<String>,
pub token: Option<String>,
}
impl HubConfig {
pub fn from_env() -> Self {
Self {
cache: cache_dir(),
offline: offline(env::var("HF_HUB_OFFLINE").ok().as_deref()),
endpoint: None,
token: None,
}
}
}
pub fn offline(value: Option<&str>) -> bool {
value
.is_some_and(|value| ["1", "ON", "YES", "TRUE"].contains(&value.to_ascii_uppercase().as_str()))
}
pub fn cache_dir() -> PathBuf {
root::resolve(|name| env::var_os(name), dirs::home_dir().as_deref())
}
pub fn repo_dir_name(repo_id: &str) -> String {
format!("models--{}", repo_id.replace('/', "--"))
}
pub fn in_progress_marker(cache: &Path, repo_id: &str, commit: &str) -> PathBuf {
cache
.join(".candding")
.join("in-progress")
.join(repo_dir_name(repo_id))
.join(commit)
}
pub fn snapshot_dir(cache: &Path, repo_id: &str, revision: &str) -> Option<PathBuf> {
let repo = cache.join(repo_dir_name(repo_id));
let hash = match fs::read_to_string(repo.join("refs").join(revision)) {
Ok(hash) => hash.trim().to_string(),
Err(_) => revision.to_string(),
};
let snapshot = repo.join("snapshots").join(&hash);
let complete = !in_progress_marker(cache, repo_id, &hash).exists();
(complete && snapshot.is_dir()).then_some(snapshot)
}