Skip to content

Commit c6c66b9

Browse files
Use FromStr
1 parent 5bd3946 commit c6c66b9

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/main.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::process::{Command, Stdio};
2+
use std::str::FromStr;
23

34
use clap::Parser;
45
use indoc::indoc;
@@ -90,18 +91,22 @@ enum RustChannel {
9091
Version(String),
9192
}
9293

93-
fn parse_channel_str(channel: &str) -> Result<RustChannel, &'static str> {
94-
let nightly_pattern = Regex::new(r"^nightly-\d{4}-\d{2}-\d{2}$").unwrap();
95-
let version_pattern = Regex::new(r"^1\.\d+(?:\.\d+)?$").unwrap();
96-
match channel {
97-
"stable" => Ok(RustChannel::Stable),
98-
"beta" => Ok(RustChannel::Beta),
99-
"nightly" => Ok(RustChannel::Nightly),
100-
_ if nightly_pattern.is_match(channel) => {
101-
Ok(RustChannel::DatedNightly(channel[8..].to_owned()))
94+
impl FromStr for RustChannel {
95+
type Err = &'static str;
96+
97+
fn from_str(channel: &str) -> Result<Self, Self::Err> {
98+
let nightly_pattern = Regex::new(r"^nightly-\d{4}-\d{2}-\d{2}$").unwrap();
99+
let version_pattern = Regex::new(r"^1\.\d+(?:\.\d+)?$").unwrap();
100+
match channel {
101+
"stable" => Ok(RustChannel::Stable),
102+
"beta" => Ok(RustChannel::Beta),
103+
"nightly" => Ok(RustChannel::Nightly),
104+
_ if nightly_pattern.is_match(channel) => {
105+
Ok(RustChannel::DatedNightly(channel[8..].to_owned()))
106+
}
107+
_ if version_pattern.is_match(channel) => Ok(RustChannel::Version(channel.to_owned())),
108+
_ => Err(INVALID_CHANNEL_MSG),
102109
}
103-
_ if version_pattern.is_match(channel) => Ok(RustChannel::Version(channel.to_owned())),
104-
_ => Err(INVALID_CHANNEL_MSG),
105110
}
106111
}
107112

@@ -110,7 +115,7 @@ fn parse_channel_str(channel: &str) -> Result<RustChannel, &'static str> {
110115
struct Opt {
111116
/// The rust release channel to pull. Possible values: ["stable", "beta", "nightly",
112117
/// "nightly-YYYY-mm-dd", "1.x.y"]
113-
#[clap(default_value = "stable", value_parser(parse_channel_str))]
118+
#[clap(default_value = "stable")]
114119
channel: RustChannel,
115120

116121
/// The shell to open. Passed to 'nix-shell --command'

0 commit comments

Comments
 (0)