Skip to content

Commit c1f8282

Browse files
committed
Remove UpgradeSelection struct
1 parent 25bedea commit c1f8282

File tree

5 files changed

+60
-107
lines changed

5 files changed

+60
-107
lines changed

crates/uv-cli/src/options.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anstream::eprintln;
22

33
use uv_cache::Refresh;
4-
use uv_configuration::UpgradeSelection;
4+
use uv_configuration::Upgrade;
55
use uv_distribution_types::{ConfigSettings, PackageConfigSettings, Requirement};
66
use uv_resolver::{ExcludeNewer, ExcludeNewerPackage, PrereleaseMode};
77
use uv_settings::{Combine, PipOptions, ResolverInstallerOptions, ResolverOptions};
@@ -334,7 +334,7 @@ pub fn resolver_options(
334334
.filter_map(Maybe::into_option)
335335
.collect()
336336
}),
337-
upgrade: UpgradeSelection::from_args(
337+
upgrade: Upgrade::from_args(
338338
flag(upgrade, no_upgrade, "no-upgrade"),
339339
upgrade_package.into_iter().map(Requirement::from).collect(),
340340
),
@@ -445,7 +445,7 @@ pub fn resolver_installer_options(
445445
.filter_map(Maybe::into_option)
446446
.collect()
447447
}),
448-
upgrade: UpgradeSelection::from_args(
448+
upgrade: Upgrade::from_args(
449449
flag(upgrade, no_upgrade, "upgrade"),
450450
upgrade_package.into_iter().map(Requirement::from).collect(),
451451
),

crates/uv-configuration/src/package_options.rs

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ impl From<Reinstall> for Refresh {
134134
}
135135
}
136136

137-
/// An upgrade selection as specified by a user on the command line or in a configuration file.
137+
/// Whether to allow package upgrades.
138138
#[derive(Debug, Default, Clone)]
139-
pub enum UpgradeSelection {
139+
pub enum Upgrade {
140140
/// Prefer pinned versions from the existing lockfile, if possible.
141141
#[default]
142142
None,
@@ -145,10 +145,10 @@ pub enum UpgradeSelection {
145145
All,
146146

147147
/// Allow package upgrades, but only for the specified packages.
148-
Packages(Vec<Requirement>),
148+
Packages(FxHashMap<PackageName, Vec<Requirement>>),
149149
}
150150

151-
impl UpgradeSelection {
151+
impl Upgrade {
152152
/// Determine the upgrade selection strategy from the command-line arguments.
153153
pub fn from_args(upgrade: Option<bool>, upgrade_package: Vec<Requirement>) -> Option<Self> {
154154
match upgrade {
@@ -157,68 +157,18 @@ impl UpgradeSelection {
157157
// packages to be upgraded. Right now, `--upgrade-package` is silently ignored.
158158
Some(false) => Some(Self::None),
159159
None if upgrade_package.is_empty() => None,
160-
None => Some(Self::Packages(upgrade_package)),
161-
}
162-
}
163-
164-
/// Combine a set of [`UpgradeSelection`] values.
165-
#[must_use]
166-
pub fn combine(self, other: Self) -> Self {
167-
match self {
168-
// Setting `--upgrade` or `--no-upgrade` should clear previous `--upgrade-package` selections.
169-
Self::All | Self::None => self,
170-
Self::Packages(self_packages) => match other {
171-
// If `--upgrade` was enabled previously, `--upgrade-package` is subsumed by upgrading all packages.
172-
Self::All => other,
173-
// If `--no-upgrade` was enabled previously, then `--upgrade-package` enables an explicit upgrade of those packages.
174-
Self::None => Self::Packages(self_packages),
175-
// If `--upgrade-package` was included twice, combine the requirements.
176-
Self::Packages(other_packages) => {
177-
let mut combined = self_packages;
178-
combined.extend(other_packages);
179-
Self::Packages(combined)
180-
}
181-
},
160+
None => Some(Self::Packages(upgrade_package.into_iter().fold(
161+
FxHashMap::default(),
162+
|mut map, requirement| {
163+
map.entry(requirement.name.clone())
164+
.or_default()
165+
.push(requirement);
166+
map
167+
},
168+
))),
182169
}
183170
}
184-
}
185-
186-
/// Whether to allow package upgrades.
187-
#[derive(Debug, Default, Clone)]
188-
pub enum Upgrade {
189-
/// Prefer pinned versions from the existing lockfile, if possible.
190-
#[default]
191-
None,
192-
193-
/// Allow package upgrades for all packages, ignoring the existing lockfile.
194-
All,
195171

196-
/// Allow package upgrades, but only for the specified packages.
197-
Packages(FxHashMap<PackageName, Vec<Requirement>>),
198-
}
199-
200-
/// Determine the [`Upgrade`] strategy from the command-line arguments.
201-
impl From<Option<UpgradeSelection>> for Upgrade {
202-
fn from(value: Option<UpgradeSelection>) -> Self {
203-
match value {
204-
None => Self::None,
205-
Some(UpgradeSelection::None) => Self::None,
206-
Some(UpgradeSelection::All) => Self::All,
207-
Some(UpgradeSelection::Packages(requirements)) => Self::Packages(
208-
requirements
209-
.into_iter()
210-
.fold(FxHashMap::default(), |mut map, requirement| {
211-
map.entry(requirement.name.clone())
212-
.or_default()
213-
.push(requirement);
214-
map
215-
}),
216-
),
217-
}
218-
}
219-
}
220-
221-
impl Upgrade {
222172
/// Create an [`Upgrade`] strategy to upgrade a single package.
223173
pub fn package(package_name: PackageName) -> Self {
224174
Self::Packages({
@@ -265,19 +215,23 @@ impl Upgrade {
265215
/// Combine a set of [`Upgrade`] values.
266216
#[must_use]
267217
pub fn combine(self, other: Self) -> Self {
268-
match (self, other) {
269-
// If both are `None`, the result is `None`.
270-
(Self::None, Self::None) => Self::None,
271-
// If either is `All`, the result is `All`.
272-
(Self::All, _) | (_, Self::All) => Self::All,
273-
// If one is `None`, the result is the other.
274-
(Self::Packages(a), Self::None) => Self::Packages(a),
275-
(Self::None, Self::Packages(b)) => Self::Packages(b),
276-
// If both are `Packages`, the result is the union of the two.
277-
(Self::Packages(mut a), Self::Packages(b)) => {
278-
a.extend(b);
279-
Self::Packages(a)
280-
}
218+
match self {
219+
// Setting `--upgrade` or `--no-upgrade` should clear previous `--upgrade-package` selections.
220+
Self::All | Self::None => self,
221+
Self::Packages(self_packages) => match other {
222+
// If `--upgrade` was enabled previously, `--upgrade-package` is subsumed by upgrading all packages.
223+
Self::All => other,
224+
// If `--no-upgrade` was enabled previously, then `--upgrade-package` enables an explicit upgrade of those packages.
225+
Self::None => Self::Packages(self_packages),
226+
// If `--upgrade-package` was included twice, combine the requirements.
227+
Self::Packages(other_packages) => {
228+
let mut combined = self_packages;
229+
for (package, requirements) in other_packages {
230+
combined.entry(package).or_default().extend(requirements);
231+
}
232+
Self::Packages(combined)
233+
}
234+
},
281235
}
282236
}
283237
}

crates/uv-settings/src/combine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use url::Url;
55

66
use uv_configuration::{
77
ExportFormat, IndexStrategy, KeyringProviderType, RequiredVersion, TargetTriple,
8-
TrustedPublishing, UpgradeSelection,
8+
TrustedPublishing, Upgrade,
99
};
1010
use uv_distribution_types::{
1111
ConfigSettings, ExtraBuildVariables, Index, IndexUrl, PackageConfigSettings, PipExtraIndex,
@@ -181,7 +181,7 @@ impl Combine for Option<PackageConfigSettings> {
181181
}
182182
}
183183

184-
impl Combine for Option<UpgradeSelection> {
184+
impl Combine for Option<Upgrade> {
185185
fn combine(self, other: Self) -> Self {
186186
match (self, other) {
187187
(Some(a), Some(b)) => Some(a.combine(b)),

crates/uv-settings/src/settings.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55
use uv_cache_info::CacheKey;
66
use uv_configuration::{
77
IndexStrategy, KeyringProviderType, PackageNameSpecifier, RequiredVersion, TargetTriple,
8-
TrustedHost, TrustedPublishing, UpgradeSelection,
8+
TrustedHost, TrustedPublishing, Upgrade,
99
};
1010
use uv_distribution_types::{
1111
ConfigSettings, ExtraBuildVariables, Index, IndexUrl, IndexUrlError, PackageConfigSettings,
@@ -369,7 +369,7 @@ pub struct ResolverOptions {
369369
pub config_settings_package: Option<PackageConfigSettings>,
370370
pub exclude_newer: ExcludeNewer,
371371
pub link_mode: Option<LinkMode>,
372-
pub upgrade: Option<UpgradeSelection>,
372+
pub upgrade: Option<Upgrade>,
373373
pub no_build: Option<bool>,
374374
pub no_build_package: Option<Vec<PackageName>>,
375375
pub no_binary: Option<bool>,
@@ -407,7 +407,7 @@ pub struct ResolverInstallerOptions {
407407
pub link_mode: Option<LinkMode>,
408408
pub compile_bytecode: Option<bool>,
409409
pub no_sources: Option<bool>,
410-
pub upgrade: Option<UpgradeSelection>,
410+
pub upgrade: Option<Upgrade>,
411411
pub reinstall: Option<bool>,
412412
pub reinstall_package: Option<Vec<PackageName>>,
413413
pub no_build: Option<bool>,
@@ -473,7 +473,7 @@ impl From<ResolverInstallerSchema> for ResolverInstallerOptions {
473473
link_mode,
474474
compile_bytecode,
475475
no_sources,
476-
upgrade: UpgradeSelection::from_args(
476+
upgrade: Upgrade::from_args(
477477
upgrade,
478478
upgrade_package
479479
.into_iter()
@@ -1886,7 +1886,7 @@ impl From<ResolverInstallerSchema> for ResolverOptions {
18861886
.collect(),
18871887
),
18881888
link_mode: value.link_mode,
1889-
upgrade: UpgradeSelection::from_args(
1889+
upgrade: Upgrade::from_args(
18901890
value.upgrade,
18911891
value
18921892
.upgrade_package

crates/uv/src/settings.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use uv_configuration::{
2424
BuildOptions, Concurrency, DependencyGroups, DryRun, EditableMode, ExportFormat,
2525
ExtrasSpecification, HashCheckingMode, IndexStrategy, InstallOptions, KeyringProviderType,
2626
NoBinary, NoBuild, Preview, ProjectBuildBackend, Reinstall, RequiredVersion, SourceStrategy,
27-
TargetTriple, TrustedHost, TrustedPublishing, Upgrade, UpgradeSelection, VersionControlSystem,
27+
TargetTriple, TrustedHost, TrustedPublishing, Upgrade, VersionControlSystem,
2828
};
2929
use uv_distribution_types::{
3030
ConfigSettings, DependencyMetadata, ExtraBuildVariables, Index, IndexLocations, IndexUrl,
@@ -2852,7 +2852,7 @@ impl From<ResolverOptions> for ResolverSettings {
28522852
exclude_newer: value.exclude_newer,
28532853
link_mode: value.link_mode.unwrap_or_default(),
28542854
sources: SourceStrategy::from_args(value.no_sources.unwrap_or_default()),
2855-
upgrade: Upgrade::from(value.upgrade),
2855+
upgrade: value.upgrade.unwrap_or_default(),
28562856
build_options: BuildOptions::new(
28572857
NoBinary::from_args(value.no_binary, value.no_binary_package.unwrap_or_default()),
28582858
NoBuild::from_args(value.no_build, value.no_build_package.unwrap_or_default()),
@@ -2943,7 +2943,7 @@ impl From<ResolverInstallerOptions> for ResolverInstallerSettings {
29432943
prerelease: value.prerelease.unwrap_or_default(),
29442944
resolution: value.resolution.unwrap_or_default(),
29452945
sources: SourceStrategy::from_args(value.no_sources.unwrap_or_default()),
2946-
upgrade: Upgrade::from(value.upgrade),
2946+
upgrade: value.upgrade.unwrap_or_default(),
29472947
},
29482948
compile_bytecode: value.compile_bytecode.unwrap_or_default(),
29492949
reinstall: Reinstall::from_args(
@@ -3317,24 +3317,23 @@ impl PipSettings {
33173317
args.no_sources.combine(no_sources).unwrap_or_default(),
33183318
),
33193319
strict: args.strict.combine(strict).unwrap_or_default(),
3320-
upgrade: Upgrade::from(
3321-
UpgradeSelection::from_args(
3322-
args.upgrade,
3323-
args.upgrade_package
3324-
.into_iter()
3325-
.flatten()
3326-
.map(Requirement::from)
3327-
.collect(),
3328-
)
3329-
.combine(UpgradeSelection::from_args(
3330-
upgrade,
3331-
upgrade_package
3332-
.into_iter()
3333-
.flatten()
3334-
.map(Requirement::from)
3335-
.collect(),
3336-
)),
3337-
),
3320+
upgrade: Upgrade::from_args(
3321+
args.upgrade,
3322+
args.upgrade_package
3323+
.into_iter()
3324+
.flatten()
3325+
.map(Requirement::from)
3326+
.collect(),
3327+
)
3328+
.combine(Upgrade::from_args(
3329+
upgrade,
3330+
upgrade_package
3331+
.into_iter()
3332+
.flatten()
3333+
.map(Requirement::from)
3334+
.collect(),
3335+
))
3336+
.unwrap_or_default(),
33383337
reinstall: Reinstall::from_args(
33393338
args.reinstall.combine(reinstall),
33403339
args.reinstall_package

0 commit comments

Comments
 (0)