forked from Lutetium-Vanadium/requestty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.rs
112 lines (110 loc) · 3.71 KB
/
macros.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
109
110
111
112
fn main() {
let phone_validator = regex::RegexBuilder::new(r"^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$")
.case_insensitive(true)
.build()
.unwrap();
let questions = requestty::questions! [
// Use std::array::IntoIter instead of allocating a Vec if available (>= v1.51)
inline
Confirm {
name: "to_be_delivered",
message: "Is this for delivery?",
default: false,
},
Input {
name: "phone",
message: "What's your phone number?",
validate: |value, _| {
if phone_validator.is_match(value) {
Ok(())
} else {
Err("Please enter a valid phone number".into())
}
},
},
Select {
name: "size",
message: "What size do you need?",
choices: ["Large", "Medium", "Small"],
},
Int {
name: "quantity",
message: "How many do you need?",
validate: |ans, _| {
if ans > 0 {
Ok(())
} else {
Err("You need to order at least 1 pizza".into())
}
},
},
Confirm {
name: "custom_toppings",
message: "Do you want to customize the toppings?",
default: false,
},
Expand {
name: "toppings",
message: "What about the toppings?",
when: |answers: &requestty::Answers| {
!answers["custom_toppings"].as_bool().unwrap()
},
choices: [
('p', "Pepperoni and cheese"),
('a', "All dressed"),
('w', "Hawaiian"),
],
},
MultiSelect {
name: "toppings",
message: "Select toppings",
when: |answers: &requestty::Answers| {
answers["custom_toppings"].as_bool().unwrap()
},
// Array style choices (`[...]`) have special parsing
choices: [
// Use 'separator' or 'sep' for separators
separator " = The Meats = ",
// Otherwise they are considered choices
"Pepperoni",
"Ham",
"Ground Meat",
"Bacon",
separator " = The Cheeses = ",
// Use `<choice> default <value>` to give default value (only for MultiSelect)
"Mozzarella" default true,
"Cheddar",
"Parmesan",
separator " = The usual = ",
"Mushroom",
"Tomato",
separator " = The extras = ",
"Pineapple",
"Olives",
"Extra cheese",
// Specifying nothing in front of the separator will give a default
// separator
separator,
],
},
RawSelect {
name: "beverage",
message: "You also get a free 2L beverage",
choices: ["Pepsi", "7up", "Coke"],
},
Input {
name: "comments",
message: "Any comments on your purchase experience?",
default: "Nope, all good!",
},
Select {
name: "prize",
message: "For leaving a comment, you get a freebie",
choices: ["cake", "fries"],
when: |answers: &requestty::Answers| {
answers["comments"].as_string().unwrap() != "Nope, all good!"
},
},
];
println!("{:#?}", requestty::prompt(questions));
}