Skip to main content

uv_cache/
archive.rs

1use std::convert::Infallible;
2use std::path::Path;
3use std::str::FromStr;
4
5/// A unique identifier for an archive (unzipped wheel) in the cache.
6///
7/// Note: for compatibility with the existing `archive-v0` bucket, this is a newtype
8/// around a `String` instead of a newtype around `uv_fastid::Id`. In the future,
9/// we may want to bump to `archive-v1` and switch to using `uv_fastid::Id` directly.
10#[derive(Debug, Clone, Eq, PartialEq, Hash, serde::Serialize, serde::Deserialize)]
11pub struct ArchiveId(String);
12
13impl Default for ArchiveId {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl ArchiveId {
20    /// Generate a new unique identifier for an archive.
21    pub(crate) fn new() -> Self {
22        Self(uv_fastid::Id::secure().to_string())
23    }
24
25    /// Use a path-safe digest as the complete archive identifier.
26    ///
27    /// This does not generate or hash an identifier. Callers must ensure that the digest uniquely
28    /// identifies the persisted directory contents.
29    pub fn from_digest(digest: String) -> Self {
30        Self(digest)
31    }
32}
33
34impl AsRef<Path> for ArchiveId {
35    fn as_ref(&self) -> &Path {
36        self.0.as_ref()
37    }
38}
39
40impl std::fmt::Display for ArchiveId {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        self.0.fmt(f)
43    }
44}
45
46impl FromStr for ArchiveId {
47    type Err = Infallible;
48
49    fn from_str(s: &str) -> Result<Self, Self::Err> {
50        Ok(Self(s.to_string()))
51    }
52}