From a3df139b7bdfe6f572f1596bd631abe697ed6d83 Mon Sep 17 00:00:00 2001 From: rami3l Date: Mon, 21 Aug 2023 11:22:22 +0800 Subject: [PATCH] Refactor `components_missing_msg` and add a `Panics` section to its docstring --- src/dist/dist.rs | 70 +++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/src/dist/dist.rs b/src/dist/dist.rs index 97093a21c1b..9390fa942de 100644 --- a/src/dist/dist.rs +++ b/src/dist/dist.rs @@ -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::>() + .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::>() - .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()