Convert import::Kind enum to a clap compatible arg_enum (#365)

This commit is contained in:
Yannik Sander 2021-11-23 13:58:55 +01:00 committed by GitHub
parent a28c473b57
commit dbf8c2d3a4
Failed to generate hash of commit
2 changed files with 15 additions and 37 deletions

View file

@ -25,7 +25,9 @@ struct Args {
#[structopt( #[structopt(
short, short,
long, 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 default_value
)] )]
kind: data::import::Kind, kind: data::import::Kind,

View file

@ -2,6 +2,7 @@ use std::fmt::{self, write, Display};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::{path::PathBuf, str::FromStr}; use std::{path::PathBuf, str::FromStr};
use clap::arg_enum;
use serde::de::{self, MapAccess, Visitor}; use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value; use serde_json::Value;
@ -103,20 +104,22 @@ pub struct Maintainer {
pub email: Option<String>, pub email: Option<String>,
} }
/// The type of derivation (placed in packages.<system> or apps.<system>) arg_enum! {
/// Used to command the extraction script /// The type of derivation (placed in packages.<system> or apps.<system>)
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] /// Used to command the extraction script
pub enum Kind { #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Kind {
App, App,
Package, Package,
Option, Option,
All, All,
}
} }
impl AsRef<str> for Kind { impl AsRef<str> for Kind {
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
match self { match self {
Kind::App => "app", Kind::App => "apps",
Kind::Package => "packages", Kind::Package => "packages",
Kind::Option => "options", Kind::Option => "options",
Kind::All => "all", Kind::All => "all",
@ -124,33 +127,6 @@ impl AsRef<str> 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<Self, Self::Err> {
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 { impl Default for Kind {
fn default() -> Self { fn default() -> Self {
Kind::All Kind::All