1use std::path::{Path, PathBuf};
2
3use uv_cache_key::{CanonicalUrl, cache_digest};
4use uv_distribution_types::IndexUrl;
5use uv_redacted::DisplaySafeUrl;
6
7#[derive(Debug, Clone)]
9pub enum WheelCache<'a> {
10 Index(&'a IndexUrl),
12 Url(&'a DisplaySafeUrl),
14 Path(&'a DisplaySafeUrl),
16 Editable(&'a DisplaySafeUrl),
18 Git(&'a DisplaySafeUrl, &'a str),
23}
24
25impl WheelCache<'_> {
26 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 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 Pypi,
59 Index,
61 Url,
63 Path,
65 Editable,
67 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}