Skip to main content

uv_distribution_types/
requested.rs

1use std::fmt::{Display, Formatter};
2
3use crate::{
4    Dist, DistributionId, DistributionMetadata, Identifier, InstalledDist, Name, ResourceId,
5    VersionId, VersionOrUrlRef,
6};
7use uv_normalize::PackageName;
8use uv_pep440::Version;
9
10/// A distribution that can be requested during resolution.
11///
12/// Either an already-installed distribution or a distribution that can be installed.
13#[derive(Debug, Clone)]
14#[expect(clippy::large_enum_variant)]
15pub enum RequestedDist {
16    Installed(InstalledDist),
17    Installable(Dist),
18}
19
20impl RequestedDist {
21    /// Returns the version of the distribution, if it is known.
22    pub fn version(&self) -> Option<&Version> {
23        match self {
24            Self::Installed(dist) => Some(dist.version()),
25            Self::Installable(dist) => dist.version(),
26        }
27    }
28}
29
30impl Name for RequestedDist {
31    fn name(&self) -> &PackageName {
32        match self {
33            Self::Installable(dist) => dist.name(),
34            Self::Installed(dist) => dist.name(),
35        }
36    }
37}
38
39impl DistributionMetadata for RequestedDist {
40    fn version_or_url(&self) -> VersionOrUrlRef<'_> {
41        match self {
42            Self::Installed(dist) => dist.version_or_url(),
43            Self::Installable(dist) => dist.version_or_url(),
44        }
45    }
46
47    fn version_id(&self) -> VersionId {
48        match self {
49            Self::Installed(dist) => dist.version_id(),
50            Self::Installable(dist) => dist.version_id(),
51        }
52    }
53}
54
55impl Identifier for RequestedDist {
56    fn distribution_id(&self) -> DistributionId {
57        match self {
58            Self::Installed(dist) => dist.distribution_id(),
59            Self::Installable(dist) => dist.distribution_id(),
60        }
61    }
62
63    fn resource_id(&self) -> ResourceId {
64        match self {
65            Self::Installed(dist) => dist.resource_id(),
66            Self::Installable(dist) => dist.resource_id(),
67        }
68    }
69}
70
71impl Display for RequestedDist {
72    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::Installed(dist) => dist.fmt(f),
75            Self::Installable(dist) => dist.fmt(f),
76        }
77    }
78}