Skip to content

Commit 9caf4fc

Browse files
committed
Avoid code duplication for printing target/component items
1 parent 170ce65 commit 9caf4fc

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/cli/common.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::currentprocess::{
2121
varsource::VarSource,
2222
};
2323
use crate::dist::dist::{TargetTriple, ToolchainDesc};
24+
use crate::dist::manifest::ComponentStatus;
2425
use crate::install::UpdateStatus;
2526
use crate::utils::notifications as util_notifications;
2627
use crate::utils::notify::NotificationLevel;
@@ -379,38 +380,35 @@ pub(crate) fn list_targets(
379380
distributable: DistributableToolchain<'_>,
380381
installed_only: bool,
381382
) -> Result<utils::ExitCode> {
382-
let mut t = process().stdout().terminal();
383-
for component in distributable.components()? {
384-
if component.component.short_name_in_manifest() == "rust-std" {
385-
let target = component
386-
.component
387-
.target
388-
.as_ref()
389-
.expect("rust-std should have a target");
390-
match (component.available, component.installed, installed_only) {
391-
(false, _, _) | (_, false, true) => continue,
392-
(true, true, false) => {
393-
t.attr(terminalsource::Attr::Bold)?;
394-
writeln!(t.lock(), "{target} (installed)")?;
395-
t.reset()?;
396-
}
397-
(true, _, false) | (_, true, true) => {
398-
writeln!(t.lock(), "{target}")?;
399-
}
400-
}
401-
}
402-
}
403-
404-
Ok(utils::ExitCode(0))
383+
list_items(
384+
distributable,
385+
|c| {
386+
(c.component.short_name_in_manifest() == "rust-std").then(|| {
387+
c.component
388+
.target
389+
.as_deref()
390+
.expect("rust-std should have a target")
391+
})
392+
},
393+
installed_only,
394+
)
405395
}
406396

407397
pub(crate) fn list_components(
408398
distributable: DistributableToolchain<'_>,
409399
installed_only: bool,
400+
) -> Result<utils::ExitCode> {
401+
list_items(distributable, |c| Some(&c.name), installed_only)
402+
}
403+
404+
fn list_items(
405+
distributable: DistributableToolchain<'_>,
406+
f: impl Fn(&ComponentStatus) -> Option<&str>,
407+
installed_only: bool,
410408
) -> Result<utils::ExitCode> {
411409
let mut t = process().stdout().terminal();
412410
for component in distributable.components()? {
413-
let name = component.name;
411+
let Some(name) = f(&component) else { continue };
414412
match (component.available, component.installed, installed_only) {
415413
(false, _, _) | (_, false, true) => continue,
416414
(true, true, false) => {

0 commit comments

Comments
 (0)