Repository: astral-sh/uv
File: crates/uv/src/commands/pip/operations.rs (Rust)

Code before:
    }
}

impl From<CachedDist> for ChangedDist {
    fn from(dist: CachedDist) -> Self {
        Self::from(LocalDist::from(dist))
    }
}

impl From<Arc<Dist>> for ChangedDist {
    fn from(dist: Arc<Dist>) -> Self {
        Self::Remote(dist)
    }
}

impl Name for ChangedDist {
    fn name(&self) -> &PackageName {
        match self {
            Self::Local(dist) => dist.name(),
            Self::Remote(dist) => dist.name(),
        }
    }
}

/// Zero version number
static ZERO_VERSION: LazyLock<uv_pep440::Version> = LazyLock::new(|| uv_pep440::Version::new([0]));

// This is a dirty hack to allow ChangedDist to be smuggled through a
// ChangeEvent
impl InstalledMetadata for ChangedDist {
    fn installed_version(&self) -> InstalledVersion<'_> {
        self.installed_version()
            .unwrap_or(InstalledVersion::Version(&ZERO_VERSION))
    }
}

impl ChangedDist {
    /// Returns the version of the distribution, if it is known.
    pub(crate) fn installed_version(&self) -> Option<InstalledVersion<'_>> {
        match self {
            Self::Local(dist) => Some(dist.installed_version()),
            Self::Remote(dist) => dist.version().map(InstalledVersion::Version),
        }
    }
}

/// A summary of the changes made to the environment during an installation.
#[derive(Debug, Clone, Default)]
pub(crate) struct Changelog {
    /// The distributions that were installed.
    pub(crate) installed: HashSet<ChangedDist>,

Review comment (suggestion):
This is awful, it's a direct side effect of `ChangeEvent` requiring this type implements `InstalledMetadata`. I think that potentially, since `ChangeEvent` is only used (in this way - actually not sure anymore) in the logger and in uv-sync, it may make sense to use a different type and then this whole thing can be avoided.

Code after:

/// The [`InstalledVersion`] or [`VerbatimUrl`] for a changed dist.
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub(crate) enum LongSpecifier<'a> {
    InstalledVersion(InstalledVersion<'a>),
    Url(&'a VerbatimUrl),
}

impl std::fmt::Display for LongSpecifier<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InstalledVersion(version) => version.fmt(f),
            Self::Url(url) => write!(f, " @ {url}"),
        }
    }
}

impl ChangedDist {
    pub(crate) fn short_specifier(&self) -> ShortSpecifier<'_> {
        match self {
            Self::Local(dist) => ShortSpecifier::Version(dist.installed_version().version()),
            Self::Remote(dist) => match dist.version_or_url() {
                VersionOrUrlRef::Version(version) => ShortSpecifier::Version(version),
                VersionOrUrlRef::Url(url) => ShortSpecifier::Url(url),
            },
        }
    }

    pub(crate) fn long_specifier(&self) -> LongSpecifier<'_> {
        match self {
            Self::Local(dist) => LongSpecifier::InstalledVersion(dist.installed_version()),
            Self::Remote(dist) => match dist.version_or_url() {
                VersionOrUrlRef::Version(version) => {
                    LongSpecifier::InstalledVersion(InstalledVersion::Version(version))
                }
                VersionOrUrlRef::Url(url) => LongSpecifier::Url(url),
            },
        }
    }

    pub(crate) fn version(&self) -> Option<&Version> {
        match self {
            Self::Local(dist) => Some(dist.installed_version().version()),
            Self::Remote(dist) => dist.version(),
        }
    }
}

/// A summary of the changes made to the environment during an installation.
#[derive(Debug, Clone, Default)]
pub(crate) struct Changelog {