use core::marker::PhantomData;
use core::str::FromStr;
use std::boxed::Box;
use ::clap::builder::PossibleValue;
use ::clap::builder::StringValueParser;
use ::clap::builder::TypedValueParser;
use ::clap::Arg;
use ::clap::Command;
use ::clap::Error;
use crate::__FromDeclared;
use crate::Variants;
pub trait ArgType: Variants + FromStr + __FromDeclared + Clone + Send + Sync + 'static {
fn value_parser() -> Parser<Self> {
Parser::new()
}
}
impl<T> ArgType for T where T: Variants + FromStr + __FromDeclared + Clone + Send + Sync + 'static {}
#[derive(Clone, Copy, Debug)]
pub struct Parser<T> {
_ty: PhantomData<fn() -> T>,
}
impl<T> Parser<T> {
#[must_use]
pub const fn new() -> Self {
Self { _ty: PhantomData }
}
}
impl<T> Default for Parser<T> {
fn default() -> Self {
Self::new()
}
}
impl<T> TypedValueParser for Parser<T>
where
T: Variants + FromStr + __FromDeclared + Clone + Send + Sync + 'static,
T::Err: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
type Value = T;
fn parse_ref(
&self,
cmd: &Command,
arg: Option<&Arg>,
value: &std::ffi::OsStr,
) -> Result<T, Error> {
StringValueParser::new()
.try_map(|s: std::string::String| T::__from_declared(&s))
.parse_ref(cmd, arg, value)
}
fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>> {
Some(Box::new(T::LABELS.iter().copied().map(PossibleValue::new)))
}
}