use std::path::{Path, PathBuf};
use uv_cache_key::{CanonicalUrl, cache_digest};
use uv_distribution_types::IndexUrl;
use uv_redacted::DisplaySafeUrl;
#[derive(Debug, Clone)]
pub enum WheelCache<'a> {
Index(&'a IndexUrl),
Url(&'a DisplaySafeUrl),
Path(&'a DisplaySafeUrl),
Editable(&'a DisplaySafeUrl),
Git(&'a DisplaySafeUrl, &'a str),
}
impl WheelCache<'_> {
pub fn root(&self) -> PathBuf {
match self {
Self::Index(IndexUrl::Pypi(_)) => WheelCacheKind::Pypi.root(),
Self::Index(url) => WheelCacheKind::Index
.root()
.join(cache_digest(&CanonicalUrl::new(url.url()))),
Self::Url(url) => WheelCacheKind::Url
.root()
.join(cache_digest(&CanonicalUrl::new(url))),
Self::Path(url) => WheelCacheKind::Path
.root()
.join(cache_digest(&CanonicalUrl::new(url))),
Self::Editable(url) => WheelCacheKind::Editable
.root()
.join(cache_digest(&CanonicalUrl::new(url))),
Self::Git(url, sha) => WheelCacheKind::Git
.root()
.join(cache_digest(&CanonicalUrl::new(url)))
.join(sha),
}
}
pub fn wheel_dir(&self, package_name: impl AsRef<Path>) -> PathBuf {
self.root().join(package_name)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum WheelCacheKind {
Pypi,
Index,
Url,
Path,
Editable,
Git,
}
impl WheelCacheKind {
pub(crate) fn to_str(self) -> &'static str {
match self {
Self::Pypi => "pypi",
Self::Index => "index",
Self::Url => "url",
Self::Path => "path",
Self::Editable => "editable",
Self::Git => "git",
}
}
pub(crate) fn root(self) -> PathBuf {
Path::new(self.to_str()).to_path_buf()
}
}
impl AsRef<Path> for WheelCacheKind {
fn as_ref(&self) -> &Path {
self.to_str().as_ref()
}
}