/
enum_variant_names.rs
108 lines (95 loc) · 2.11 KB
/
enum_variant_names.rs
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
104
105
106
107
108
#[macro_use]
extern crate strum_macros;
#[macro_use]
extern crate structopt;
extern crate strum;
use strum::VariantNames;
#[test]
fn simple() {
#[allow(dead_code)]
#[derive(EnumVariantNames)]
enum Color {
Red,
Blue,
Yellow,
}
assert_eq!(Color::VARIANTS, &["Red", "Blue", "Yellow"]);
}
#[test]
fn variant_names_trait() {
#[allow(dead_code)]
#[derive(EnumVariantNames)]
enum Color {
Red,
Blue,
Yellow,
}
fn generic_function<T: VariantNames>() {
assert_eq!(T::VARIANTS, &["Red", "Blue", "Yellow"]);
}
generic_function::<Color>();
}
#[test]
fn plain_kebab() {
#[allow(dead_code)]
#[derive(EnumVariantNames)]
#[strum(serialize_all = "kebab_case")]
enum Color {
Red,
Blue,
Yellow,
RebeccaPurple,
}
assert_eq!(
Color::VARIANTS,
&["red", "blue", "yellow", "rebecca-purple"]
);
}
#[test]
fn non_plain_camel() {
#[allow(dead_code)]
#[derive(EnumVariantNames)]
#[strum(serialize_all = "kebab_case")]
enum Color {
DeepPink,
GreenYellow,
CornflowerBlue,
Other { r: u8, g: u8, b: u8 },
}
assert_eq!(
Color::VARIANTS,
&["deep-pink", "green-yellow", "cornflower-blue", "other"]
);
}
#[test]
fn clap_and_structopt() {
#[derive(Debug, EnumString, EnumVariantNames)]
#[strum(serialize_all = "kebab_case")]
enum Color {
Red,
Blue,
Yellow,
RebeccaPurple,
}
assert_eq!(
Color::VARIANTS,
&["red", "blue", "yellow", "rebecca-purple"]
);
let _clap_example = clap::App::new("app").arg(
clap::Arg::with_name("color")
.long("color")
.possible_values(Color::VARIANTS)
.case_insensitive(true),
);
#[derive(Debug, StructOpt)]
#[allow(unused)]
struct StructOptExample {
/// The main color
#[structopt(
long = "color",
default_value = "Color::Blue",
raw(possible_values = "Color::VARIANTS")
)]
color: Color,
}
}