uv_cache/
wheel.rs

1use std::path::{Path, PathBuf};
2
3use uv_cache_key::{CanonicalUrl, cache_digest};
4use uv_distribution_types::IndexUrl;
5use uv_redacted::DisplaySafeUrl;
6
7/// Cache wheels and their metadata, both from remote wheels and built from source distributions.
8#[derive(Debug, Clone)]
9pub enum WheelCache<'a> {
10    /// Either PyPI or an alternative index, which we key by index URL.
11    Index(&'a IndexUrl),
12    /// A direct URL dependency, which we key by URL.
13    Url(&'a DisplaySafeUrl),
14    /// A path dependency, which we key by URL.
15    Path(&'a DisplaySafeUrl),
16    /// An editable dependency, which we key by URL.
17    Editable(&'a DisplaySafeUrl),
18    /// A Git dependency, which we key by URL (including LFS state), SHA.
19    ///
20    /// Note that this variant only exists for source distributions; wheels can't be delivered
21    /// through Git.
22    Git(&'a DisplaySafeUrl, &'a str),
23}
24
25impl WheelCache<'_> {
26    /// The root directory for a cache bucket.
27    pub fn root(&self) -> PathBuf {
28        match self {
29            Self::Index(IndexUrl::Pypi(_)) => WheelCacheKind::Pypi.root(),
30            Self::Index(url) => WheelCacheKind::Index
31                .root()
32                .join(cache_digest(&CanonicalUrl::new(url.url()))),
33            Self::Url(url) => WheelCacheKind::Url
34                .root()
35                .join(cache_digest(&CanonicalUrl::new(url))),
36            Self::Path(url) => WheelCacheKind::Path
37                .root()
38                .join(cache_digest(&CanonicalUrl::new(url))),
39            Self::Editable(url) => WheelCacheKind::Editable
40                .root()
41                .join(cache_digest(&CanonicalUrl::new(url))),
42            Self::Git(url, sha) => WheelCacheKind::Git
43                .root()
44                .join(cache_digest(&CanonicalUrl::new(url)))
45                .join(sha),
46        }
47    }
48
49    /// A subdirectory in a bucket for wheels for a specific package.
50    pub fn wheel_dir(&self, package_name: impl AsRef<Path>) -> PathBuf {
51        self.root().join(package_name)
52    }
53}
54
55#[derive(Debug, Clone, Copy)]
56pub(crate) enum WheelCacheKind {
57    /// A cache of data from PyPI.
58    Pypi,
59    /// A cache of data from an alternative index.
60    Index,
61    /// A cache of data from an arbitrary URL.
62    Url,
63    /// A cache of data from a local path.
64    Path,
65    /// A cache of data from an editable URL.
66    Editable,
67    /// A cache of data from a Git repository.
68    Git,
69}
70
71impl WheelCacheKind {
72    pub(crate) fn to_str(self) -> &'static str {
73        match self {
74            Self::Pypi => "pypi",
75            Self::Index => "index",
76            Self::Url => "url",
77            Self::Path => "path",
78            Self::Editable => "editable",
79            Self::Git => "git",
80        }
81    }
82
83    pub(crate) fn root(self) -> PathBuf {
84        Path::new(self.to_str()).to_path_buf()
85    }
86}
87
88impl AsRef<Path> for WheelCacheKind {
89    fn as_ref(&self) -> &Path {
90        self.to_str().as_ref()
91    }
92}