diff --git a/src/cli/common.rs b/src/cli/common.rs index f74e70b943..d1967417c8 100644 --- a/src/cli/common.rs +++ b/src/cli/common.rs @@ -21,6 +21,7 @@ use crate::currentprocess::{ varsource::VarSource, }; use crate::dist::dist::{TargetTriple, ToolchainDesc}; +use crate::dist::manifest::ComponentStatus; use crate::install::UpdateStatus; use crate::utils::notifications as util_notifications; use crate::utils::notify::NotificationLevel; @@ -375,88 +376,55 @@ where Ok(utils::ExitCode(0)) } -pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result { - let mut t = process().stdout().terminal(); - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - let components = manifest.query_components(distributable.desc(), &config)?; - for component in components { - if component.component.short_name_in_manifest() == "rust-std" { - let target = component - .component - .target - .as_ref() - .expect("rust-std should have a target"); - if component.installed { - let _ = t.attr(terminalsource::Attr::Bold); - let _ = writeln!(t.lock(), "{target} (installed)"); - let _ = t.reset(); - } else if component.available { - let _ = writeln!(t.lock(), "{target}"); - } - } - } - - Ok(utils::ExitCode(0)) +pub(crate) fn list_targets( + distributable: DistributableToolchain<'_>, + installed_only: bool, +) -> Result { + list_items( + distributable, + |c| { + (c.component.short_name_in_manifest() == "rust-std").then(|| { + c.component + .target + .as_deref() + .expect("rust-std should have a target") + }) + }, + installed_only, + ) } -pub(crate) fn list_installed_targets( +pub(crate) fn list_components( distributable: DistributableToolchain<'_>, + installed_only: bool, ) -> Result { - let t = process().stdout(); - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - let components = manifest.query_components(distributable.desc(), &config)?; - for component in components { - if component.component.short_name_in_manifest() == "rust-std" { - let target = component - .component - .target - .as_ref() - .expect("rust-std should have a target"); - if component.installed { - writeln!(t.lock(), "{target}")?; - } - } - } - Ok(utils::ExitCode(0)) + list_items(distributable, |c| Some(&c.name), installed_only) } -pub(crate) fn list_components( +fn list_items( distributable: DistributableToolchain<'_>, + f: impl Fn(&ComponentStatus) -> Option<&str>, + installed_only: bool, ) -> Result { let mut t = process().stdout().terminal(); - - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - let components = manifest.query_components(distributable.desc(), &config)?; - for component in components { - let name = component.name; - if component.installed { - t.attr(terminalsource::Attr::Bold)?; - writeln!(t.lock(), "{name} (installed)")?; - t.reset()?; - } else if component.available { - writeln!(t.lock(), "{name}")?; + for component in distributable.components()? { + let Some(name) = f(&component) else { continue }; + match (component.available, component.installed, installed_only) { + (false, _, _) | (_, false, true) => continue, + (true, true, false) => { + t.attr(terminalsource::Attr::Bold)?; + writeln!(t.lock(), "{name} (installed)")?; + t.reset()?; + } + (true, _, false) | (_, true, true) => { + writeln!(t.lock(), "{name}")?; + } } } Ok(utils::ExitCode(0)) } -pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> { - let t = process().stdout(); - for component in distributable.components()? { - if component.installed { - writeln!(t.lock(), "{}", component.name)?; - } - } - Ok(()) -} - fn print_toolchain_path( cfg: &Cfg, toolchain: &str, diff --git a/src/cli/rustup_mode.rs b/src/cli/rustup_mode.rs index 5d0096af03..e724944cca 100644 --- a/src/cli/rustup_mode.rs +++ b/src/cli/rustup_mode.rs @@ -1084,14 +1084,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result { // active_toolchain will carry the reason we don't have one in its detail. let active_targets = if let Ok(ref at) = active_toolchain { if let Ok(distributable) = DistributableToolchain::try_from(&at.0) { - let components = (|| { - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - manifest.query_components(distributable.desc(), &config) - })(); - - match components { + match distributable.components() { Ok(cs_vec) => cs_vec .into_iter() .filter(|c| c.component.short_name_in_manifest() == "rust-std") @@ -1266,12 +1259,7 @@ fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result { let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?; // downcasting required because the toolchain files can name any toolchain let distributable = (&toolchain).try_into()?; - - if m.get_flag("installed") { - common::list_installed_targets(distributable) - } else { - common::list_targets(distributable) - } + common::list_targets(distributable, m.get_flag("installed")) } fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result { @@ -1282,11 +1270,7 @@ fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result { // list_components *and* add_component would both be inappropriate for // custom toolchains. let distributable = DistributableToolchain::try_from(&toolchain)?; - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - let components = manifest.query_components(distributable.desc(), &config)?; - + let components = distributable.components()?; let mut targets: Vec<_> = m .get_many::("target") .unwrap() @@ -1364,12 +1348,7 @@ fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result { let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?; // downcasting required because the toolchain files can name any toolchain let distributable = (&toolchain).try_into()?; - - if m.get_flag("installed") { - common::list_installed_components(distributable)?; - } else { - common::list_components(distributable)?; - } + common::list_components(distributable, m.get_flag("installed"))?; Ok(utils::ExitCode(0)) } @@ -1564,11 +1543,8 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result { let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?; if let Ok(distributable) = DistributableToolchain::try_from(&toolchain) { - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - let components = manifest.query_components(distributable.desc(), &config)?; - if let [_] = components + if let [_] = distributable + .components()? .into_iter() .filter(|cstatus| { cstatus.component.short_name_in_manifest() == "rust-docs" && !cstatus.installed diff --git a/src/dist/manifest.rs b/src/dist/manifest.rs index 1c37a63ec9..ab49528826 100644 --- a/src/dist/manifest.rs +++ b/src/dist/manifest.rs @@ -594,12 +594,7 @@ impl Component { distributable: &DistributableToolchain<'_>, fallback_target: Option<&TargetTriple>, ) -> Result { - let manifestation = distributable.get_manifestation()?; - let config = manifestation.read_config()?.unwrap_or_default(); - let manifest = distributable.get_manifest()?; - let manifest_components = manifest.query_components(distributable.desc(), &config)?; - - for component_status in manifest_components { + for component_status in distributable.components()? { let short_name = component_status.component.short_name_in_manifest(); let target = component_status.component.target.as_ref();