Skip to content

Commit d2b2775

Browse files
committed
Simplify
Remove enum, pull out function and dependent function
1 parent 624ce68 commit d2b2775

File tree

3 files changed

+149
-150
lines changed

3 files changed

+149
-150
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 82 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -170,103 +170,97 @@ fn install_one(
170170

171171
let dst = root.join("bin").into_path_unlocked();
172172

173-
let dependency = {
174-
if let Some(krate) = krate {
175-
let vers = if source_id.is_git() || source_id.is_path() {
176-
// We explicitly ignore the version flag for these source types.
177-
None
178-
} else if let Some(vers_flag) = vers {
179-
Some(parse_semver_flag(vers_flag)?.to_string())
180-
} else {
181-
if source_id.is_registry() {
182-
// Avoid pre-release versions from crate.io
183-
// unless explicitly asked for
184-
Some(String::from("*"))
173+
let pkg = {
174+
let dep = {
175+
if let Some(krate) = krate {
176+
let vers = if let Some(vers_flag) = vers {
177+
Some(parse_semver_flag(vers_flag)?.to_string())
185178
} else {
186-
None
187-
}
188-
};
189-
Some(Dependency::parse_no_deprecated(
190-
krate,
191-
vers.as_deref(),
192-
source_id,
193-
)?)
194-
} else {
195-
None
196-
}
197-
};
198-
199-
let pkg = if source_id.is_git() {
200-
let dep_or_list_all_fn = if let Some(dep) = dependency {
201-
DependencyOrListAllFn::Dependency(dep)
202-
} else {
203-
DependencyOrListAllFn::ListAllFn(|git: &mut GitSource<'_>| git.read_packages())
179+
if source_id.is_registry() {
180+
// Avoid pre-release versions from crate.io
181+
// unless explicitly asked for
182+
Some(String::from("*"))
183+
} else {
184+
None
185+
}
186+
};
187+
Some(Dependency::parse_no_deprecated(
188+
krate,
189+
vers.as_deref(),
190+
source_id,
191+
)?)
192+
} else {
193+
None
194+
}
204195
};
205-
select_pkg(
206-
&mut GitSource::new(source_id, config)?,
207-
dep_or_list_all_fn,
208-
config,
209-
true,
210-
)?
211-
} else if source_id.is_path() {
212-
let mut src = path_source(source_id, config)?;
213-
if !src.path().is_dir() {
214-
bail!(
215-
"`{}` is not a directory. \
216-
--path must point to a directory containing a Cargo.toml file.",
217-
src.path().display()
218-
)
219-
}
220-
if !src.path().join("Cargo.toml").exists() {
221-
if from_cwd {
196+
197+
if source_id.is_git() {
198+
let mut source = GitSource::new(source_id, config)?;
199+
select_pkg(
200+
&mut source,
201+
dep,
202+
|git: &mut GitSource<'_>| git.read_packages(),
203+
config,
204+
)?
205+
} else if source_id.is_path() {
206+
let mut src = path_source(source_id, config)?;
207+
if !src.path().is_dir() {
222208
bail!(
223-
"`{}` is not a crate root; specify a crate to \
209+
"`{}` is not a directory. \
210+
--path must point to a directory containing a Cargo.toml file.",
211+
src.path().display()
212+
)
213+
}
214+
if !src.path().join("Cargo.toml").exists() {
215+
if from_cwd {
216+
bail!(
217+
"`{}` is not a crate root; specify a crate to \
224218
install from crates.io, or use --path or --git to \
225219
specify an alternate source",
226-
src.path().display()
227-
);
228-
} else {
229-
bail!(
230-
"`{}` does not contain a Cargo.toml file. \
220+
src.path().display()
221+
);
222+
} else {
223+
bail!(
224+
"`{}` does not contain a Cargo.toml file. \
231225
--path must point to a directory containing a Cargo.toml file.",
232-
src.path().display()
233-
)
226+
src.path().display()
227+
)
228+
}
234229
}
235-
}
236-
src.update()?;
237-
238-
let dep_or_list_all_fn = if let Some(dep) = dependency {
239-
DependencyOrListAllFn::Dependency(dep)
230+
select_pkg(
231+
&mut src,
232+
dep,
233+
|path: &mut PathSource<'_>| path.read_packages(),
234+
config,
235+
)?
240236
} else {
241-
DependencyOrListAllFn::ListAllFn(|path: &mut PathSource<'_>| path.read_packages())
242-
};
243-
select_pkg(&mut src, dep_or_list_all_fn, config, false)?
244-
} else {
245-
if !dependency.is_some() {
246-
bail!(
247-
"must specify a crate to install from \
237+
if let Some(dep) = dep {
238+
let mut source = map.load(source_id, &HashSet::new())?;
239+
if let Ok(Some(pkg)) = installed_exact_package(
240+
dep.clone(),
241+
&mut source,
242+
config,
243+
opts,
244+
root,
245+
&dst,
246+
force,
247+
) {
248+
let msg = format!(
249+
"package `{}` is already installed, use --force to override",
250+
pkg
251+
);
252+
config.shell().status("Ignored", &msg)?;
253+
return Ok(true);
254+
}
255+
select_dep_pkg(&mut source, dep, config, needs_update_if_source_is_index)?
256+
} else {
257+
bail!(
258+
"must specify a crate to install from \
248259
crates.io, or use --path or --git to \
249260
specify alternate source"
250-
)
251-
}
252-
let dep = dependency.unwrap(); // Verified with is_some/bail above.
253-
let mut source = map.load(source_id, &HashSet::new())?;
254-
if let Ok(Some(pkg)) =
255-
installed_exact_package(dep.clone(), &mut source, config, opts, root, &dst, force)
256-
{
257-
let msg = format!(
258-
"package `{}` is already installed, use --force to override",
259-
pkg
260-
);
261-
config.shell().status("Ignored", &msg)?;
262-
return Ok(true);
261+
)
262+
}
263263
}
264-
select_pkg(
265-
&mut source,
266-
DependencyOrListAllFn::<fn(&mut Box<dyn Source>) -> CargoResult<Vec<Package>>>::Dependency(dep),
267-
config,
268-
needs_update_if_source_is_index,
269-
)?
270264
};
271265

272266
let git_package = if source_id.is_git() {
@@ -578,12 +572,7 @@ where
578572
// expensive network call in the case that the package is already installed.
579573
// If this fails, the caller will possibly do an index update and try again, this is just a
580574
// best-effort check to see if we can avoid hitting the network.
581-
if let Ok(pkg) = select_pkg(
582-
source,
583-
DependencyOrListAllFn::<fn(&mut T) -> CargoResult<Vec<Package>>>::Dependency(dep),
584-
config,
585-
false,
586-
) {
575+
if let Ok(pkg) = select_dep_pkg(source, dep, config, false) {
587576
let (_ws, rustc, target) =
588577
make_ws_rustc_target(&config, opts, &source.source_id(), pkg.clone())?;
589578
if let Ok(true) = is_installed(&pkg, config, opts, &rustc, &target, root, &dst, force) {
@@ -644,6 +633,7 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
644633
),
645634
}
646635
} else {
636+
println!("DWH: Converting...");
647637
match v.to_semver() {
648638
Ok(v) => Ok(VersionReq::exact(&v)),
649639
Err(e) => {

src/cargo/ops/cargo_uninstall.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ fn uninstall_cwd(root: &Filesystem, bins: &[String], config: &Config) -> CargoRe
8888
let mut src = path_source(source_id, config)?;
8989
let pkg = select_pkg(
9090
&mut src,
91-
DependencyOrListAllFn::ListAllFn(|path: &mut PathSource<'_>| path.read_packages()),
91+
None,
92+
|path: &mut PathSource<'_>| path.read_packages(),
9293
config,
93-
true,
9494
)?;
9595
let pkgid = pkg.package_id();
9696
uninstall_pkgid(root, tracker, pkgid, bins, config)

src/cargo/ops/common_for_install_and_uninstall.rs

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -519,23 +519,15 @@ pub fn path_source(source_id: SourceId, config: &Config) -> CargoResult<PathSour
519519
Ok(PathSource::new(&path, source_id, config))
520520
}
521521

522-
/// Either the specific dependency to be selected, or a function which can be executed to list all
523-
/// packages known in a source so that one can be selected from that list.
524-
pub enum DependencyOrListAllFn<F> {
525-
Dependency(Dependency),
526-
ListAllFn(F),
527-
}
528-
529522
/// Gets a Package based on command-line requirements.
530-
pub fn select_pkg<T, F>(
523+
pub fn select_dep_pkg<T>(
531524
source: &mut T,
532-
dep_or_list_all_fn: DependencyOrListAllFn<F>,
525+
dep: Dependency,
533526
config: &Config,
534527
needs_update: bool,
535528
) -> CargoResult<Package>
536529
where
537530
T: Source,
538-
F: FnMut(&mut T) -> CargoResult<Vec<Package>>,
539531
{
540532
// This operation may involve updating some sources or making a few queries
541533
// which may involve frobbing caches, as a result make sure we synchronize
@@ -546,54 +538,71 @@ where
546538
source.update()?;
547539
}
548540

549-
match dep_or_list_all_fn {
550-
DependencyOrListAllFn::Dependency(dep) => {
551-
let deps = source.query_vec(&dep)?;
552-
match deps.iter().map(|p| p.package_id()).max() {
553-
Some(pkgid) => {
554-
let pkg = Box::new(source).download_now(pkgid, config)?;
555-
Ok(pkg)
556-
}
557-
None => bail!(
558-
"could not find `{}` in {} with version `{}`",
559-
dep.package_name(),
560-
source.source_id(),
561-
dep.version_req(),
562-
),
563-
}
541+
let deps = source.query_vec(&dep)?;
542+
match deps.iter().map(|p| p.package_id()).max() {
543+
Some(pkgid) => {
544+
let pkg = Box::new(source).download_now(pkgid, config)?;
545+
Ok(pkg)
564546
}
565-
DependencyOrListAllFn::ListAllFn(mut list_all) => {
566-
let candidates = list_all(source)?;
567-
let binaries = candidates
568-
.iter()
569-
.filter(|cand| cand.targets().iter().filter(|t| t.is_bin()).count() > 0);
570-
let examples = candidates
571-
.iter()
572-
.filter(|cand| cand.targets().iter().filter(|t| t.is_example()).count() > 0);
573-
let pkg = match one(binaries, |v| multi_err("binaries", v))? {
547+
None => bail!(
548+
"could not find `{}` in {} with version `{}`",
549+
dep.package_name(),
550+
source.source_id(),
551+
dep.version_req(),
552+
),
553+
}
554+
}
555+
556+
pub fn select_pkg<T, F>(
557+
source: &mut T,
558+
dep: Option<Dependency>,
559+
mut list_all: F,
560+
config: &Config,
561+
) -> CargoResult<Package>
562+
where
563+
T: Source,
564+
F: FnMut(&mut T) -> CargoResult<Vec<Package>>,
565+
{
566+
// This operation may involve updating some sources or making a few queries
567+
// which may involve frobbing caches, as a result make sure we synchronize
568+
// with other global Cargos
569+
let _lock = config.acquire_package_cache_lock()?;
570+
571+
source.update()?;
572+
573+
return if let Some(dep) = dep {
574+
select_dep_pkg(source, dep, config, false)
575+
} else {
576+
let candidates = list_all(source)?;
577+
let binaries = candidates
578+
.iter()
579+
.filter(|cand| cand.targets().iter().filter(|t| t.is_bin()).count() > 0);
580+
let examples = candidates
581+
.iter()
582+
.filter(|cand| cand.targets().iter().filter(|t| t.is_example()).count() > 0);
583+
let pkg = match one(binaries, |v| multi_err("binaries", v))? {
584+
Some(p) => p,
585+
None => match one(examples, |v| multi_err("examples", v))? {
574586
Some(p) => p,
575-
None => match one(examples, |v| multi_err("examples", v))? {
576-
Some(p) => p,
577-
None => bail!(
578-
"no packages found with binaries or \
579-
examples"
580-
),
581-
},
582-
};
583-
return Ok(pkg.clone());
584-
585-
fn multi_err(kind: &str, mut pkgs: Vec<&Package>) -> String {
586-
pkgs.sort_unstable_by_key(|a| a.name());
587-
format!(
588-
"multiple packages with {} found: {}",
589-
kind,
590-
pkgs.iter()
591-
.map(|p| p.name().as_str())
592-
.collect::<Vec<_>>()
593-
.join(", ")
594-
)
595-
}
596-
}
587+
None => bail!(
588+
"no packages found with binaries or \
589+
examples"
590+
),
591+
},
592+
};
593+
Ok(pkg.clone())
594+
};
595+
596+
fn multi_err(kind: &str, mut pkgs: Vec<&Package>) -> String {
597+
pkgs.sort_unstable_by_key(|a| a.name());
598+
format!(
599+
"multiple packages with {} found: {}",
600+
kind,
601+
pkgs.iter()
602+
.map(|p| p.name().as_str())
603+
.collect::<Vec<_>>()
604+
.join(", ")
605+
)
597606
}
598607
}
599608

0 commit comments

Comments
 (0)