Skip to content

Commit 1776933

Browse files
FranciscoTGouveiarami3l
authored andcommitted
fix(toolchain): avoid unwrapping when parsing a toolchain name
1 parent ef3d77e commit 1776933

File tree

1 file changed

+30
-33
lines changed

1 file changed

+30
-33
lines changed

src/dist/mod.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -311,42 +311,39 @@ impl FromStr for ParsedToolchainDesc {
311311
.unwrap()
312312
});
313313

314-
let d = TOOLCHAIN_CHANNEL_RE.captures(desc).map(|c| {
315-
fn fn_map(s: &str) -> Option<String> {
316-
if s.is_empty() {
317-
None
318-
} else {
319-
Some(s.to_owned())
320-
}
321-
}
322-
323-
// These versions don't have v2 manifests, but they don't have point releases either,
324-
// so to make the two-part version numbers work for these versions, specially turn
325-
// them into their corresponding ".0" version.
326-
let channel = match c.get(1).unwrap().as_str() {
327-
"1.0" => "1.0.0",
328-
"1.1" => "1.1.0",
329-
"1.2" => "1.2.0",
330-
"1.3" => "1.3.0",
331-
"1.4" => "1.4.0",
332-
"1.5" => "1.5.0",
333-
"1.6" => "1.6.0",
334-
"1.7" => "1.7.0",
335-
"1.8" => "1.8.0",
336-
other => other,
337-
};
314+
let d = TOOLCHAIN_CHANNEL_RE
315+
.captures(desc)
316+
.ok_or_else(|| RustupError::InvalidToolchainName(desc.to_string()))?;
338317

339-
Self {
340-
channel: Channel::from_str(channel).unwrap(),
341-
date: c.get(2).map(|s| s.as_str()).and_then(fn_map),
342-
target: c.get(3).map(|s| s.as_str()).and_then(fn_map),
318+
fn fn_map(s: &str) -> Option<String> {
319+
if s.is_empty() {
320+
None
321+
} else {
322+
Some(s.to_owned())
343323
}
344-
});
345-
346-
match d {
347-
Some(d) => Ok(d),
348-
None => Err(RustupError::InvalidToolchainName(desc.to_string()).into()),
349324
}
325+
326+
// These versions don't have v2 manifests, but they don't have point releases either,
327+
// so to make the two-part version numbers work for these versions, specially turn
328+
// them into their corresponding ".0" version.
329+
let channel = match d.get(1).unwrap().as_str() {
330+
"1.0" => "1.0.0",
331+
"1.1" => "1.1.0",
332+
"1.2" => "1.2.0",
333+
"1.3" => "1.3.0",
334+
"1.4" => "1.4.0",
335+
"1.5" => "1.5.0",
336+
"1.6" => "1.6.0",
337+
"1.7" => "1.7.0",
338+
"1.8" => "1.8.0",
339+
other => other,
340+
};
341+
342+
Ok(Self {
343+
channel: Channel::from_str(channel)?,
344+
date: d.get(2).map(|s| s.as_str()).and_then(fn_map),
345+
target: d.get(3).map(|s| s.as_str()).and_then(fn_map),
346+
})
350347
}
351348
}
352349

0 commit comments

Comments
 (0)