Skip to content

Commit 825052c

Browse files
refactor(custom-toolchains): unify target listing functions
Listing targets/components can now use the same function, both for custom or distributable toolchains.
1 parent c2ef191 commit 825052c

File tree

2 files changed

+39
-63
lines changed

2 files changed

+39
-63
lines changed

src/cli/common.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ use super::self_update;
1919
use crate::{
2020
cli::download_tracker::DownloadTracker,
2121
config::Cfg,
22-
dist::{
23-
TargetTriple, ToolchainDesc, manifest::ComponentStatus, notifications as dist_notifications,
24-
},
22+
dist::{TargetTriple, ToolchainDesc, notifications as dist_notifications},
2523
errors::RustupError,
2624
install::UpdateStatus,
2725
notifications::Notification,
2826
process::{Process, terminalsource},
29-
toolchain::{DistributableToolchain, LocalToolchainName, Toolchain, ToolchainName},
27+
toolchain::{LocalToolchainName, Toolchain, ToolchainName},
3028
utils::{self, notifications as util_notifications, notify::NotificationLevel},
3129
};
3230

@@ -381,70 +379,26 @@ where
381379
}
382380

383381
pub(super) fn list_items(
384-
distributable: DistributableToolchain<'_>,
385-
f: impl Fn(&ComponentStatus) -> Option<&str>,
382+
items: impl Iterator<Item = (String, bool)>,
386383
installed_only: bool,
387384
quiet: bool,
388385
process: &Process,
389386
) -> Result<utils::ExitCode> {
390387
let mut t = process.stdout().terminal(process);
391-
for component in distributable.components()? {
392-
let Some(name) = f(&component) else { continue };
393-
match (component.available, component.installed, installed_only) {
394-
(false, _, _) | (_, false, true) => continue,
395-
(true, true, false) if !quiet => {
388+
for (name, installed) in items {
389+
if !installed_only || installed {
390+
if installed && !installed_only && !quiet {
396391
t.attr(terminalsource::Attr::Bold)?;
397392
writeln!(t.lock(), "{name} (installed)")?;
398393
t.reset()?;
399-
}
400-
(true, _, false) | (_, true, true) => {
394+
} else {
401395
writeln!(t.lock(), "{name}")?;
402396
}
403397
}
404398
}
405-
406399
Ok(utils::ExitCode(0))
407400
}
408401

409-
pub(super) fn list_items_custom(
410-
toolchain: Toolchain<'_>,
411-
targets: bool,
412-
installed_only: bool,
413-
quiet: bool,
414-
process: &Process,
415-
) -> Result<utils::ExitCode> {
416-
let mut t = process.stdout().terminal(process);
417-
let mut display_items = |items: Vec<String>| -> Result<utils::ExitCode> {
418-
for item in items {
419-
if !installed_only && !quiet {
420-
t.attr(terminalsource::Attr::Bold)?;
421-
writeln!(t.lock(), "{} (installed)", item)?;
422-
t.reset()?;
423-
} else {
424-
writeln!(t.lock(), "{}", item)?;
425-
}
426-
}
427-
Ok(utils::ExitCode(0))
428-
};
429-
if targets {
430-
display_items(
431-
toolchain
432-
.installed_targets()?
433-
.iter()
434-
.map(|s| s.to_string())
435-
.collect(),
436-
)
437-
} else {
438-
display_items(
439-
toolchain
440-
.installed_components()?
441-
.iter()
442-
.map(|s| s.name().to_string())
443-
.collect(),
444-
)
445-
}
446-
}
447-
448402
pub(crate) async fn list_toolchains(
449403
cfg: &Cfg<'_>,
450404
verbose: bool,

src/cli/rustup_mode.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,22 +1175,31 @@ async fn target_list(
11751175
// NB: this is decision is made based on the absence of a manifest in custom toolchains.
11761176
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
11771177
common::list_items(
1178-
distributable,
1179-
|c| {
1180-
(c.component.short_name_in_manifest() == "rust-std").then(|| {
1178+
distributable.components()?.into_iter().filter_map(|c| {
1179+
if c.component.short_name_in_manifest() == "rust-std" && c.available {
11811180
c.component
11821181
.target
11831182
.as_deref()
1184-
.expect("rust-std should have a target")
1185-
})
1186-
},
1183+
.map(|target| (target.to_string(), c.installed))
1184+
} else {
1185+
None
1186+
}
1187+
}),
11871188
installed_only,
11881189
quiet,
11891190
cfg.process,
11901191
)
11911192
} else {
11921193
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
1193-
common::list_items_custom(toolchain, true, installed_only, quiet, cfg.process)
1194+
common::list_items(
1195+
toolchain
1196+
.installed_targets()?
1197+
.iter()
1198+
.map(|s| (s.to_string(), true)),
1199+
installed_only,
1200+
quiet,
1201+
cfg.process,
1202+
)
11941203
}
11951204
}
11961205

@@ -1288,15 +1297,28 @@ async fn component_list(
12881297
// downcasting required because the toolchain files can name any toolchain
12891298
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
12901299
common::list_items(
1291-
distributable,
1292-
|c| Some(&c.name),
1300+
distributable.components()?.into_iter().filter_map(|c| {
1301+
if c.available {
1302+
Some((c.name, c.installed))
1303+
} else {
1304+
None
1305+
}
1306+
}),
12931307
installed_only,
12941308
quiet,
12951309
cfg.process,
12961310
)
12971311
} else {
12981312
let toolchain = cfg.toolchain_from_partial(toolchain).await?;
1299-
common::list_items_custom(toolchain, false, installed_only, quiet, cfg.process)
1313+
common::list_items(
1314+
toolchain
1315+
.installed_components()?
1316+
.iter()
1317+
.map(|s| (s.name().to_string(), true)),
1318+
installed_only,
1319+
quiet,
1320+
cfg.process,
1321+
)
13001322
}
13011323
}
13021324

0 commit comments

Comments
 (0)