Skip to main content

uv_resolver/
upgrade.rs

1use rustc_hash::FxHashSet;
2
3use uv_configuration::Upgrade;
4use uv_normalize::PackageName;
5
6/// The resolved set of packages that should be upgraded.
7///
8/// This combines `--upgrade` and `--upgrade-package` into a single `contains` check.
9#[derive(Debug, Default, Clone)]
10pub struct UpgradePackages {
11    /// Whether all packages should be upgraded.
12    all: bool,
13    /// The specific packages to upgrade.
14    packages: FxHashSet<PackageName>,
15}
16
17impl UpgradePackages {
18    /// Create an [`UpgradePackages`] for non-project commands (e.g., `pip compile`, `pip install`)
19    /// where dependency groups are not supported.
20    pub fn for_non_project(upgrade: &Upgrade) -> Self {
21        match (upgrade.is_all(), upgrade.packages()) {
22            (true, _) => Self {
23                all: true,
24                packages: FxHashSet::default(),
25            },
26            (false, Some(packages)) => Self {
27                all: false,
28                packages: packages.clone(),
29            },
30            (false, None) => Self::default(),
31        }
32    }
33
34    /// Returns `true` if the given package should be upgraded.
35    pub fn contains(&self, package_name: &PackageName) -> bool {
36        self.all || self.packages.contains(package_name)
37    }
38}