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(
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,

View file

@ -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,6 +104,7 @@ pub struct Maintainer {
pub email: Option<String>,
}
arg_enum! {
/// The type of derivation (placed in packages.<system> or apps.<system>)
/// Used to command the extraction script
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
@ -112,11 +114,12 @@ pub enum Kind {
Option,
All,
}
}
impl AsRef<str> 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<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 {
fn default() -> Self {
Kind::All