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 fn new() -> Self {
22        Self(uv_fastid::insecure().to_string())
23    }
24}
25
26impl AsRef<Path> for ArchiveId {
27    fn as_ref(&self) -> &Path {
28        self.0.as_ref()
29    }
30}
31
32impl std::fmt::Display for ArchiveId {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        self.0.fmt(f)
35    }
36}
37
38impl FromStr for ArchiveId {
39    type Err = Infallible;
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        Ok(Self(s.to_string()))
43    }
44}