Skip to content

Commit 28af53c

Browse files
committed
chore: use value parser to parse list subcommand
1 parent 9786a96 commit 28af53c

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/command/list/mod.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,10 @@ enum Filter {
199199

200200
#[derive(clap::Args)]
201201
pub(crate) struct List {
202-
// Note: we implement the subcommand as an `Option<String>` instead of an
203-
// `Option<Subcommand>` with `impl FromStr for Subcommand` for `StructOpt`
204-
// because StructOpt does not currently support custom parsing for enum
205-
// variants (as detailed in commit 5f9214ae).
206202
/// The tool to lookup - `all`, `node`, `npm`, `yarn`, `pnpm`, or the name
207203
/// of a package or binary.
208-
#[arg(value_name = "tool")]
209-
subcommand: Option<String>,
204+
#[arg(value_name = "tool", value_parser = parse_subcommand)]
205+
subcommand: Option<Subcommand>,
210206

211207
/// Specify the output format.
212208
///
@@ -226,6 +222,7 @@ pub(crate) struct List {
226222
}
227223

228224
/// Which tool should we look up?
225+
#[derive(Clone)]
229226
enum Subcommand {
230227
/// Show every item in the toolchain.
231228
All,
@@ -246,17 +243,15 @@ enum Subcommand {
246243
PackageOrTool { name: String },
247244
}
248245

249-
impl From<&str> for Subcommand {
250-
fn from(s: &str) -> Self {
251-
match s {
252-
"all" => Subcommand::All,
253-
"node" => Subcommand::Node,
254-
"npm" => Subcommand::Npm,
255-
"pnpm" => Subcommand::Pnpm,
256-
"yarn" => Subcommand::Yarn,
257-
s => Subcommand::PackageOrTool { name: s.into() },
258-
}
259-
}
246+
fn parse_subcommand(s: &str) -> Result<Subcommand, std::convert::Infallible> {
247+
Ok(match s {
248+
"all" => Subcommand::All,
249+
"node" => Subcommand::Node,
250+
"npm" => Subcommand::Npm,
251+
"pnpm" => Subcommand::Pnpm,
252+
"yarn" => Subcommand::Yarn,
253+
s => Subcommand::PackageOrTool { name: s.into() },
254+
})
260255
}
261256

262257
impl List {
@@ -271,10 +266,6 @@ impl List {
271266
Format::Plain
272267
})
273268
}
274-
275-
fn subcommand(&self) -> Option<Subcommand> {
276-
self.subcommand.as_ref().map(|s| s.as_str().into())
277-
}
278269
}
279270

280271
impl Command for List {
@@ -295,7 +286,7 @@ impl Command for List {
295286
_ => Filter::None,
296287
};
297288

298-
let toolchain = match self.subcommand() {
289+
let toolchain = match self.subcommand {
299290
// For no subcommand, show the user's current toolchain
300291
None => Toolchain::active(project, default_platform)?,
301292
Some(Subcommand::All) => Toolchain::all(project, default_platform)?,

0 commit comments

Comments
 (0)