use crate::{Argument, Opt};
use core::fmt::{Debug, Display, Formatter};
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Arg<A: Argument> {
Short(A::ShortOpt),
Long(A),
Positional(A),
}
impl<A: Argument> Arg<A> {
pub fn opt(self) -> Option<Opt<A>> {
match self {
Self::Short(short) => Some(Opt::Short(short)),
Self::Long(long) => Some(Opt::Long(long)),
_ => None,
}
}
pub fn positional(self) -> Option<A> {
match self {
Self::Positional(arg) => Some(arg),
_ => None,
}
}
}
impl<A: Argument> From<Opt<A>> for Arg<A> {
fn from(opt: Opt<A>) -> Self {
match opt {
Opt::Short(short) => Self::Short(short),
Opt::Long(long) => Self::Long(long),
}
}
}
impl<A: Argument> From<A> for Arg<A> {
fn from(arg: A) -> Self {
Self::Positional(arg)
}
}
impl<S: Display, A: Argument<ShortOpt = S> + Display> Display for Arg<A> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Self::Short(short) => write!(f, "-{}", short),
Self::Long(long) => write!(f, "--{}", long),
Self::Positional(arg) => Display::fmt(arg, f),
}
}
}