Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplication for show commands #3813

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 35 additions & 67 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -375,88 +376,55 @@ where
Ok(utils::ExitCode(0))
}

pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<utils::ExitCode> {
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(
rami3l marked this conversation as resolved.
Show resolved Hide resolved
distributable: DistributableToolchain<'_>,
installed_only: bool,
) -> Result<utils::ExitCode> {
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<utils::ExitCode> {
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<utils::ExitCode> {
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,
Expand Down
36 changes: 6 additions & 30 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,14 +1084,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
// 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")
Expand Down Expand Up @@ -1266,12 +1259,7 @@ fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
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<utils::ExitCode> {
Expand All @@ -1282,11 +1270,7 @@ fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
// 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::<String>("target")
.unwrap()
Expand Down Expand Up @@ -1364,12 +1348,7 @@ fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
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))
}

Expand Down Expand Up @@ -1564,11 +1543,8 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
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
Expand Down
7 changes: 1 addition & 6 deletions src/dist/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,7 @@ impl Component {
distributable: &DistributableToolchain<'_>,
fallback_target: Option<&TargetTriple>,
) -> Result<Self> {
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();

Expand Down
Loading