hf_fetch_model/
cache_layout.rs1use std::path::{Path, PathBuf};
13
14use hf_hub::{Repo, RepoType};
15
16#[must_use]
20pub fn repo_folder_name(repo_id: &str) -> String {
21 Repo::new(repo_id.to_owned(), RepoType::Model).folder_name()
23}
24
25#[must_use]
27pub fn repo_dir(cache_root: &Path, repo_id: &str) -> PathBuf {
28 cache_root.join(repo_folder_name(repo_id))
29}
30
31#[must_use]
33pub fn snapshots_dir(repo_dir: &Path) -> PathBuf {
34 repo_dir.join("snapshots")
35}
36
37#[must_use]
39pub fn snapshot_dir(repo_dir: &Path, commit_hash: &str) -> PathBuf {
40 snapshots_dir(repo_dir).join(commit_hash)
41}
42
43#[must_use]
45pub fn pointer_path(repo_dir: &Path, commit_hash: &str, filename: &str) -> PathBuf {
46 snapshot_dir(repo_dir, commit_hash).join(filename)
47}
48
49#[must_use]
51pub fn blobs_dir(repo_dir: &Path) -> PathBuf {
52 repo_dir.join("blobs")
53}
54
55#[must_use]
57pub fn blob_path(repo_dir: &Path, etag: &str) -> PathBuf {
58 blobs_dir(repo_dir).join(etag)
59}
60
61#[must_use]
67pub fn temp_blob_path(repo_dir: &Path, etag: &str) -> PathBuf {
68 let mut name = etag.to_owned();
70 name.push_str(".chunked.part");
71 blobs_dir(repo_dir).join(name)
72}
73
74#[must_use]
76pub fn refs_dir(repo_dir: &Path) -> PathBuf {
77 repo_dir.join("refs")
78}
79
80#[must_use]
82pub fn ref_path(repo_dir: &Path, revision: &str) -> PathBuf {
83 refs_dir(repo_dir).join(revision)
84}
85
86#[cfg(test)]
87mod tests {
88 #![allow(clippy::panic, clippy::unwrap_used, clippy::expect_used)]
89
90 use super::*;
91
92 #[test]
93 fn blob_path_joins_repo_dir_and_etag() {
94 let rd = Path::new("/tmp/models--x--y");
95 assert_eq!(blob_path(rd, "abc123"), rd.join("blobs").join("abc123"));
96 }
97
98 #[test]
99 fn temp_blob_path_preserves_periods_in_etag() {
100 let rd = Path::new("/tmp/models--x--y");
105 assert_eq!(
106 temp_blob_path(rd, "abc.def"),
107 rd.join("blobs").join("abc.def.chunked.part")
108 );
109 }
110}