use std::path::{Path, PathBuf};
use hf_hub::{Repo, RepoType};
#[must_use]
pub fn repo_folder_name(repo_id: &str) -> String {
Repo::new(repo_id.to_owned(), RepoType::Model).folder_name()
}
#[must_use]
pub fn repo_dir(cache_root: &Path, repo_id: &str) -> PathBuf {
cache_root.join(repo_folder_name(repo_id))
}
#[must_use]
pub fn snapshots_dir(repo_dir: &Path) -> PathBuf {
repo_dir.join("snapshots")
}
#[must_use]
pub fn snapshot_dir(repo_dir: &Path, commit_hash: &str) -> PathBuf {
snapshots_dir(repo_dir).join(commit_hash)
}
#[must_use]
pub fn pointer_path(repo_dir: &Path, commit_hash: &str, filename: &str) -> PathBuf {
snapshot_dir(repo_dir, commit_hash).join(filename)
}
#[must_use]
pub fn blobs_dir(repo_dir: &Path) -> PathBuf {
repo_dir.join("blobs")
}
#[must_use]
pub fn blob_path(repo_dir: &Path, etag: &str) -> PathBuf {
blobs_dir(repo_dir).join(etag)
}
#[must_use]
pub fn temp_blob_path(repo_dir: &Path, etag: &str) -> PathBuf {
let mut name = etag.to_owned();
name.push_str(".chunked.part");
blobs_dir(repo_dir).join(name)
}
#[must_use]
pub fn refs_dir(repo_dir: &Path) -> PathBuf {
repo_dir.join("refs")
}
#[must_use]
pub fn ref_path(repo_dir: &Path, revision: &str) -> PathBuf {
refs_dir(repo_dir).join(revision)
}
#[cfg(test)]
mod tests {
#![allow(clippy::panic, clippy::unwrap_used, clippy::expect_used)]
use super::*;
#[test]
fn blob_path_joins_repo_dir_and_etag() {
let rd = Path::new("/tmp/models--x--y");
assert_eq!(blob_path(rd, "abc123"), rd.join("blobs").join("abc123"));
}
#[test]
fn temp_blob_path_preserves_periods_in_etag() {
let rd = Path::new("/tmp/models--x--y");
assert_eq!(
temp_blob_path(rd, "abc.def"),
rd.join("blobs").join("abc.def.chunked.part")
);
}
}