Skip to content

Commit 7f86b75

Browse files
rami3ldjc
authored andcommitted
Refactor components_*_msg
1 parent e814458 commit 7f86b75

File tree

2 files changed

+72
-67
lines changed

2 files changed

+72
-67
lines changed

src/dist/dist.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,49 @@ static TOOLCHAIN_CHANNELS: &[&str] = &[
4444
];
4545

4646
fn components_missing_msg(cs: &[Component], manifest: &ManifestV2, toolchain: &str) -> String {
47-
assert!(!cs.is_empty());
4847
let mut buf = vec![];
4948
let suggestion = format!(" rustup toolchain add {toolchain} --profile minimal");
5049
let nightly_tips = "Sometimes not all components are available in any given nightly. ";
5150

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

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

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

79-
if toolchain.starts_with("nightly") {
80-
let _ = write!(buf, "{nightly_tips}");
85+
let _ = write!(
86+
buf,
87+
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
88+
);
8189
}
82-
let _ = write!(
83-
buf,
84-
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
85-
);
8690
}
8791

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

src/errors.rs

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -148,52 +148,53 @@ fn suggest_message(suggestion: &Option<String>) -> String {
148148
}
149149

150150
fn component_unavailable_msg(cs: &[Component], manifest: &Manifest, toolchain: &str) -> String {
151-
assert!(!cs.is_empty());
152-
153151
let mut buf = vec![];
154-
155-
if cs.len() == 1 {
156-
let _ = writeln!(
157-
buf,
158-
"component {} is unavailable for download for channel '{}'",
159-
&cs[0].description(manifest),
160-
toolchain,
161-
);
162-
if toolchain.starts_with("nightly") {
163-
let _ = write!(
152+
match cs {
153+
[] => panic!("`component_unavailable_msg` should not be called with an empty collection of unavailable components"),
154+
[c] => {
155+
let _ = writeln!(
164156
buf,
165-
"Sometimes not all components are available in any given nightly. "
157+
"component {} is unavailable for download for channel '{}'",
158+
c.description(manifest),
159+
toolchain,
166160
);
167-
}
168-
} else {
169-
// More than one component
170-
171-
let same_target = cs
172-
.iter()
173-
.all(|c| c.target == cs[0].target || c.target.is_none());
174161

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

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

192-
if toolchain.starts_with("nightly") {
193187
let _ = write!(
194188
buf,
195-
"Sometimes not all components are available in any given nightly. "
189+
"some components unavailable for download for channel '{toolchain}': {cs_str}"
196190
);
191+
192+
if toolchain.starts_with("nightly") {
193+
let _ = write!(
194+
buf,
195+
"Sometimes not all components are available in any given nightly. "
196+
);
197+
}
197198
}
198199
}
199200

0 commit comments

Comments
 (0)