use std::{cmp::Ordering, ffi::OsStr, fmt, str::FromStr};
use crate::{
flag::{self, Flag},
private::Context,
value::ArgValue,
};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Color {
Never,
#[default]
Auto,
Always,
}
impl ArgValue for Color {
const CHOICES: &[&str] = &["never", "auto", "always"];
type Error = InvalidColor;
fn parse(s: &OsStr, cx: &Context) -> Result<Self, Self::Error> {
let color = match s.as_encoded_bytes() {
b"never" => Self::Never,
b"auto" => Self::Auto,
b"always" => Self::Always,
_ => return Err(InvalidColor { _p: () }),
};
cx.color.set(color);
Ok(color)
}
}
impl FromStr for Color {
type Err = InvalidColor;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"never" => Ok(Self::Never),
"auto" => Ok(Self::Auto),
"always" => Ok(Self::Always),
_ => Err(InvalidColor { _p: () }),
}
}
}
#[derive(Debug)]
pub struct InvalidColor {
_p: (),
}
impl fmt::Display for InvalidColor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid colorization choice (allowed are 'never', 'auto' and 'always')")
}
}
impl std::error::Error for InvalidColor {}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Verbosity(i32);
impl Verbosity {
pub const ZERO: Self = Self(0);
#[inline]
pub fn get(self) -> i32 {
self.0
}
}
impl PartialEq<i32> for Verbosity {
#[inline]
fn eq(&self, other: &i32) -> bool {
self.0.eq(other)
}
}
impl PartialOrd<i32> for Verbosity {
#[inline]
fn partial_cmp(&self, other: &i32) -> Option<Ordering> {
self.0.partial_cmp(other)
}
}
impl flag::Sealed for Verbosity {}
impl flag::FlagInternal for Verbosity {
#[inline]
fn set(&mut self, cx: &Context) {
self.0 = self.0.saturating_add(1);
cx.verbosity.set(*self);
}
}
impl flag::InvertibleFlag for Verbosity {
#[inline]
fn unset(&mut self, cx: &Context) {
self.0 = self.0.saturating_sub(1);
cx.verbosity.set(*self);
}
}
impl Flag for Verbosity {}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrintVersion;
impl flag::Sealed for PrintVersion {}
impl flag::FlagInternal for PrintVersion {
#[inline]
fn set(&mut self, cx: &Context) {
cx.version_requested.set(true);
}
}
impl Flag for PrintVersion {}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrintHelp;
impl flag::Sealed for PrintHelp {}
impl flag::FlagInternal for PrintHelp {
#[inline]
fn set(&mut self, cx: &Context) {
cx.help_requested.set(true);
}
}
impl Flag for PrintHelp {}