1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::builder::PossibleValue;
use crate::derive::ValueEnum;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ColorChoice {
#[cfg_attr(not(feature = "color"), doc = " ```ignore")]
#[cfg_attr(feature = "color", doc = " ```no_run")]
Auto,
#[cfg_attr(not(feature = "color"), doc = " ```ignore")]
#[cfg_attr(feature = "color", doc = " ```no_run")]
Always,
#[cfg_attr(not(feature = "color"), doc = " ```ignore")]
#[cfg_attr(feature = "color", doc = " ```no_run")]
Never,
}
impl Default for ColorChoice {
fn default() -> Self {
Self::Auto
}
}
impl std::fmt::Display for ColorChoice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
}
}
impl std::str::FromStr for ColorChoice {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
for variant in Self::value_variants() {
if variant.to_possible_value().unwrap().matches(s, false) {
return Ok(*variant);
}
}
Err(format!("Invalid variant: {}", s))
}
}
impl ValueEnum for ColorChoice {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Auto, Self::Always, Self::Never]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
Some(match self {
Self::Auto => {
PossibleValue::new("auto").help("Use colored output if writing to a terminal/TTY")
}
Self::Always => PossibleValue::new("always").help("Always use colored output"),
Self::Never => PossibleValue::new("never").help("Never use colored output"),
})
}
}