uv_distribution_types/
requested.rs

1use std::fmt::{Display, Formatter};
2
3use crate::{
4    Dist, DistributionId, DistributionMetadata, Identifier, InstalledDist, Name, ResourceId,
5    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#[allow(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
48impl Identifier for RequestedDist {
49    fn distribution_id(&self) -> DistributionId {
50        match self {
51            Self::Installed(dist) => dist.distribution_id(),
52            Self::Installable(dist) => dist.distribution_id(),
53        }
54    }
55
56    fn resource_id(&self) -> ResourceId {
57        match self {
58            Self::Installed(dist) => dist.resource_id(),
59            Self::Installable(dist) => dist.resource_id(),
60        }
61    }
62}
63
64impl Display for RequestedDist {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        match self {
67            Self::Installed(dist) => dist.fmt(f),
68            Self::Installable(dist) => dist.fmt(f),
69        }
70    }
71}