From dbf8c2d3a4bddc8410656971655db5d20770a0ef Mon Sep 17 00:00:00 2001 From: Yannik Sander Date: Tue, 23 Nov 2021 13:58:55 +0100 Subject: [PATCH] Convert import::Kind enum to a clap compatible arg_enum (#365) --- flake-info/src/bin/flake-info.rs | 4 ++- flake-info/src/data/import.rs | 48 ++++++++------------------------ 2 files changed, 15 insertions(+), 37 deletions(-) diff --git a/flake-info/src/bin/flake-info.rs b/flake-info/src/bin/flake-info.rs index 5d4dc7a..37ec618 100644 --- a/flake-info/src/bin/flake-info.rs +++ b/flake-info/src/bin/flake-info.rs @@ -25,7 +25,9 @@ struct Args { #[structopt( short, long, - help = "Kind of data to extract (packages|options|apps|all)", + help = "Kind of data to extract", + possible_values = &data::import::Kind::variants(), + case_insensitive = true, default_value )] kind: data::import::Kind, diff --git a/flake-info/src/data/import.rs b/flake-info/src/data/import.rs index 1cf1ebd..70dc60b 100644 --- a/flake-info/src/data/import.rs +++ b/flake-info/src/data/import.rs @@ -2,6 +2,7 @@ use std::fmt::{self, write, Display}; use std::marker::PhantomData; use std::{path::PathBuf, str::FromStr}; +use clap::arg_enum; use serde::de::{self, MapAccess, Visitor}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_json::Value; @@ -103,20 +104,22 @@ pub struct Maintainer { pub email: Option, } -/// The type of derivation (placed in packages. or apps.) -/// Used to command the extraction script -#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] -pub enum Kind { - App, - Package, - Option, - All, +arg_enum! { + /// The type of derivation (placed in packages. or apps.) + /// Used to command the extraction script + #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] + pub enum Kind { + App, + Package, + Option, + All, + } } impl AsRef for Kind { fn as_ref(&self) -> &str { match self { - Kind::App => "app", + Kind::App => "apps", Kind::Package => "packages", Kind::Option => "options", Kind::All => "all", @@ -124,33 +127,6 @@ impl AsRef for Kind { } } -impl Display for Kind { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.as_ref()) - } -} - -#[derive(Debug, Error)] -pub enum ParseKindError { - #[error("Failed to parse kind: {0}")] - UnknownKind(String), -} - -impl FromStr for Kind { - type Err = ParseKindError; - - fn from_str(s: &str) -> Result { - let kind = match s { - "app" => Kind::App, - "packages" => Kind::Package, - "options" => Kind::Option, - "all" => Kind::All, - _ => return Err(ParseKindError::UnknownKind(s.into())), - }; - Ok(kind) - } -} - impl Default for Kind { fn default() -> Self { Kind::All