uv_distribution/
download.rs

1use std::path::Path;
2
3use uv_cache_info::CacheInfo;
4use uv_distribution_filename::WheelFilename;
5use uv_distribution_types::{BuildInfo, CachedDist, Dist, Hashed};
6use uv_metadata::read_flat_wheel_metadata;
7use uv_pypi_types::{HashDigest, HashDigests, ResolutionMetadata};
8
9use crate::Error;
10
11/// A locally available wheel.
12#[derive(Debug, Clone)]
13pub struct LocalWheel {
14    /// The remote distribution from which this wheel was downloaded.
15    pub(crate) dist: Dist,
16    /// The parsed filename.
17    pub(crate) filename: WheelFilename,
18    /// The canonicalized path in the cache directory to which the wheel was downloaded.
19    /// Typically, a directory within the archive bucket.
20    pub(crate) archive: Box<Path>,
21    /// The cache info of the wheel.
22    pub(crate) cache: CacheInfo,
23    /// The build info, if available.
24    pub(crate) build: Option<BuildInfo>,
25    /// The computed hashes of the wheel.
26    pub(crate) hashes: HashDigests,
27}
28
29impl LocalWheel {
30    /// Return the path to the downloaded wheel's entry in the cache.
31    pub fn target(&self) -> &Path {
32        &self.archive
33    }
34
35    /// Return the [`Dist`] from which this wheel was downloaded.
36    pub fn remote(&self) -> &Dist {
37        &self.dist
38    }
39
40    /// Return the [`WheelFilename`] of this wheel.
41    pub fn filename(&self) -> &WheelFilename {
42        &self.filename
43    }
44
45    /// Read the [`ResolutionMetadata`] from a wheel.
46    pub fn metadata(&self) -> Result<ResolutionMetadata, Error> {
47        read_flat_wheel_metadata(&self.filename, &self.archive)
48            .map_err(|err| Error::WheelMetadata(self.archive.to_path_buf(), Box::new(err)))
49    }
50}
51
52impl Hashed for LocalWheel {
53    fn hashes(&self) -> &[HashDigest] {
54        self.hashes.as_slice()
55    }
56}
57
58/// Convert a [`LocalWheel`] into a [`CachedDist`].
59impl From<LocalWheel> for CachedDist {
60    fn from(wheel: LocalWheel) -> Self {
61        Self::from_remote(
62            wheel.dist,
63            wheel.filename,
64            wheel.hashes,
65            wheel.cache,
66            wheel.build,
67            wheel.archive,
68        )
69    }
70}
71
72impl std::fmt::Display for LocalWheel {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        write!(f, "{}", self.remote())
75    }
76}