Skip to content

Commit 2659dbf

Browse files
committed
Refactor components_*_msg and add Panics sections to docstrings
1 parent 6613e05 commit 2659dbf

File tree

2 files changed

+86
-67
lines changed

2 files changed

+86
-67
lines changed

src/dist/dist.rs

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,46 +42,57 @@ static TOOLCHAIN_CHANNELS: &[&str] = &[
4242
r"\d{1}\.\d{1,3}(?:\.\d{1,2})?",
4343
];
4444

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

51-
if cs.len() == 1 {
52-
let _ = writeln!(
53-
buf,
54-
"component {} is unavailable for download for channel '{}'",
55-
&cs[0].description(manifest),
56-
toolchain,
57-
);
57+
match cs {
58+
[] => panic!("`components_missing_msg` should not be called with an empty collection of unavailable components"),
59+
[c] => {
60+
_ = writeln!(
61+
buf,
62+
"component {} is unavailable for download for channel '{}'",
63+
c.description(manifest),
64+
toolchain,
65+
);
66+
67+
if toolchain.starts_with("nightly") {
68+
_ = write!(buf, "{nightly_tips}");
69+
}
5870

59-
if toolchain.starts_with("nightly") {
60-
let _ = write!(buf, "{nightly_tips}");
71+
_ = write!(
72+
buf,
73+
"If you don't need the component, you could try a minimal installation with:\n\n{suggestion}"
74+
);
6175
}
76+
cs => {
77+
let cs_str = cs
78+
.iter()
79+
.map(|c| c.description(manifest))
80+
.collect::<Vec<_>>()
81+
.join(", ");
82+
_ = write!(
83+
buf,
84+
"some components unavailable for download for channel '{toolchain}': {cs_str}"
85+
);
6286

63-
let _ = write!(
64-
buf,
65-
"If you don't need the component, you could try a minimal installation with:\n\n{suggestion}"
66-
);
67-
} else {
68-
let cs_str = cs
69-
.iter()
70-
.map(|c| c.description(manifest))
71-
.collect::<Vec<_>>()
72-
.join(", ");
73-
let _ = write!(
74-
buf,
75-
"some components unavailable for download for channel '{toolchain}': {cs_str}"
76-
);
87+
if toolchain.starts_with("nightly") {
88+
let _ = write!(buf, "{nightly_tips}");
89+
}
7790

78-
if toolchain.starts_with("nightly") {
79-
let _ = write!(buf, "{nightly_tips}");
91+
_ = write!(
92+
buf,
93+
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
94+
);
8095
}
81-
let _ = write!(
82-
buf,
83-
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
84-
);
8596
}
8697

8798
String::from_utf8(buf).unwrap()

src/errors.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -139,53 +139,61 @@ fn suggest_message(suggestion: &Option<String>) -> String {
139139
}
140140
}
141141

142+
/// Returns a error message indicating that certain [`Component`]s are unavailable.
143+
///
144+
/// See also [`component_missing_msg`](../dist/dist/fn.components_missing_msg.html)
145+
/// which generates error messages for component unavailability toolchain-wide operations.
146+
///
147+
/// # Panics
148+
/// This function will panic when the collection of unavailable components `cs` is empty.
142149
fn component_unavailable_msg(cs: &[Component], manifest: &Manifest, toolchain: &str) -> String {
143-
assert!(!cs.is_empty());
144-
145150
let mut buf = vec![];
146-
147-
if cs.len() == 1 {
148-
let _ = writeln!(
149-
buf,
150-
"component {} is unavailable for download for channel '{}'",
151-
&cs[0].description(manifest),
152-
toolchain,
153-
);
154-
if toolchain.starts_with("nightly") {
155-
let _ = write!(
151+
match cs {
152+
[] => panic!("`component_unavailable_msg` should not be called with an empty collection of unavailable components"),
153+
[c] => {
154+
_ = writeln!(
156155
buf,
157-
"Sometimes not all components are available in any given nightly. "
156+
"component {} is unavailable for download for channel '{}'",
157+
c.description(manifest),
158+
toolchain,
158159
);
159-
}
160-
} else {
161-
// More than one component
162160

163-
let same_target = cs
164-
.iter()
165-
.all(|c| c.target == cs[0].target || c.target.is_none());
166-
167-
let cs_str = if same_target {
168-
cs.iter()
169-
.map(|c| format!("'{}'", c.short_name(manifest)))
170-
.collect::<Vec<_>>()
171-
.join(", ")
172-
} else {
173-
cs.iter()
174-
.map(|c| c.description(manifest))
175-
.collect::<Vec<_>>()
176-
.join(", ")
177-
};
161+
if toolchain.starts_with("nightly") {
162+
_ = write!(
163+
buf,
164+
"Sometimes not all components are available in any given nightly. "
165+
);
166+
}
167+
}
168+
cs => {
169+
// More than one component
170+
let same_target = cs
171+
.iter()
172+
.all(|c| c.target == cs[0].target || c.target.is_none());
178173

179-
let _ = write!(
180-
buf,
181-
"some components unavailable for download for channel '{toolchain}': {cs_str}",
182-
);
174+
let cs_str = if same_target {
175+
cs.iter()
176+
.map(|c| format!("'{}'", c.short_name(manifest)))
177+
.collect::<Vec<_>>()
178+
.join(", ")
179+
} else {
180+
cs.iter()
181+
.map(|c| c.description(manifest))
182+
.collect::<Vec<_>>()
183+
.join(", ")
184+
};
183185

184-
if toolchain.starts_with("nightly") {
185186
_ = write!(
186187
buf,
187-
"Sometimes not all components are available in any given nightly. "
188+
"some components unavailable for download for channel '{toolchain}': {cs_str}"
188189
);
190+
191+
if toolchain.starts_with("nightly") {
192+
_ = write!(
193+
buf,
194+
"Sometimes not all components are available in any given nightly. "
195+
);
196+
}
189197
}
190198
}
191199

0 commit comments

Comments
 (0)