Skip to content

Commit

Permalink
Refactor components_missing_msg and add a Panics section to its d…
Browse files Browse the repository at this point in the history
…ocstring
  • Loading branch information
rami3l committed Aug 21, 2023
1 parent 6b5cb9f commit a3df139
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,46 +42,56 @@ static TOOLCHAIN_CHANNELS: &[&str] = &[
r"\d{1}\.\d{1,3}(?:\.\d{1,2})?",
];

/// Returns a error message indicating that certain [`Component`]s are missing in a toolchain distribution.
/// This message is currently used exclusively in toolchain-wide operations,
/// otherwise [`component_unavailable_msg`](`crate::errors::component_unavailable_msg`) will be used.
///
/// # Panics
/// This function will panic when the collection of unavailable components `cs` is empty.
fn components_missing_msg(cs: &[Component], manifest: &ManifestV2, toolchain: &str) -> String {
assert!(!cs.is_empty());
let mut buf = vec![];
let suggestion = format!(" rustup toolchain add {toolchain} --profile minimal");
let nightly_tips = "Sometimes not all components are available in any given nightly. ";

if cs.len() == 1 {
let _ = writeln!(
buf,
"component {} is unavailable for download for channel '{}'",
&cs[0].description(manifest),
toolchain,
);
match cs {
[] => panic!("`components_missing_msg` should not be called with an empty collection of unavailable components"),
[c] => {
_ = writeln!(
buf,
"component {} is unavailable for download for channel '{}'",
c.description(manifest),
toolchain,
);

if toolchain.starts_with("nightly") {
_ = write!(buf, "{nightly_tips}");
}

if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
_ = write!(
buf,
"If you don't need the component, you could try a minimal installation with:\n\n{suggestion}"
);
}
cs => {
let cs_str = cs
.iter()
.map(|c| c.description(manifest))
.collect::<Vec<_>>()
.join(", ");
_ = write!(
buf,
"some components unavailable for download for channel '{toolchain}': {cs_str}"
);

let _ = write!(
buf,
"If you don't need the component, you could try a minimal installation with:\n\n{suggestion}"
);
} else {
let cs_str = cs
.iter()
.map(|c| c.description(manifest))
.collect::<Vec<_>>()
.join(", ");
let _ = write!(
buf,
"some components unavailable for download for channel '{toolchain}': {cs_str}"
);
if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
}

if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
_ = write!(
buf,
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
);
}
let _ = write!(
buf,
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
);
}

String::from_utf8(buf).unwrap()
Expand Down

0 comments on commit a3df139

Please sign in to comment.