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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use ui::backend::Backend;
use super::{Float, Int};
use crate::question::Options;
macro_rules! builder {
($(#[$meta:meta])* struct $builder_name:ident : $type:ident -> $inner_ty:ty, $litral:expr;
declare = $declare:expr;
default = $default:expr;
filter = $filter:expr;
validate = $validate:expr;
validate_on_key = $validate_on_key:expr;
) => {
$(#[$meta])*
#[derive(Debug)]
pub struct $builder_name<'a> {
opts: Options<'a>,
inner: $type<'a>,
}
impl<'a> $builder_name<'a> {
pub(crate) fn new(name: String) -> Self {
$builder_name {
opts: Options::new(name),
inner: Default::default(),
}
}
crate::impl_options_builder! {
message
/// # Examples
///
/// ```
/// use requestty::Question;
///
#[doc = $declare]
/// .message("Please enter a number")
/// .build();
/// ```
when
/// # Examples
///
/// ```
/// use requestty::{Question, Answers};
///
#[doc = $declare]
/// .when(|previous_answers: &Answers| match previous_answers.get("ask_number") {
/// Some(ans) => ans.as_bool().unwrap(),
/// None => true,
/// })
/// .build();
/// ```
ask_if_answered
/// # Examples
///
/// ```
/// use requestty::{Question, Answers};
///
#[doc = $declare]
/// .ask_if_answered(true)
/// .build();
/// ```
on_esc
/// # Examples
///
/// ```
/// use requestty::{Question, Answers, OnEsc};
///
#[doc = $declare]
/// .on_esc(OnEsc::Terminate)
/// .build();
/// ```
}
/// Set a default value
///
/// If the input text is empty, the `default` is taken as the answer.
///
/// If `default` is used, validation is skipped, but `filter` is still called.
///
/// # Examples
///
/// ```
/// use requestty::Question;
///
#[doc = $declare]
#[doc = $default]
/// .build();
/// ```
pub fn default(mut self, default: $inner_ty) -> Self {
let default_str = default.to_string();
assert!(default_str.is_ascii());
self.inner.default = Some((default, default_str));
self
}
crate::impl_filter_builder! {
/// # Examples
///
/// ```
/// use requestty::Question;
///
#[doc = $declare]
#[doc = $filter]
/// .build();
/// ```
$inner_ty; inner
}
crate::impl_validate_builder! {
/// # Examples
///
/// ```
/// use requestty::Question;
///
#[doc = $declare]
/// .validate(|n, previous_answers| {
#[doc = $validate]
/// Ok(())
/// } else {
/// Err("Please enter a positive number".to_owned())
/// }
/// })
/// .build();
/// ```
by val $inner_ty; inner
}
crate::impl_validate_on_key_builder! {
/// Note, the input will be showed in red if the number cannot be parsed even if this
/// function is not supplied.
///
/// # Examples
///
/// ```
/// use requestty::Question;
///
#[doc = $declare]
#[doc = $validate_on_key]
/// // Still required as this is the final validation and validate_on_key is purely cosmetic
/// .validate(|n, previous_answers| {
#[doc = $validate]
/// Ok(())
/// } else {
/// Err("Please enter a positive number".to_owned())
/// }
/// })
/// .build();
/// ```
by val $inner_ty; inner
}
crate::impl_transform_builder! {
/// # Examples
///
/// ```
/// use requestty::Question;
///
#[doc = $declare]
/// .transform(|n, previous_answers, backend| {
/// write!(backend, "{:e}", n)
/// })
/// .build();
/// ```
by val $inner_ty; inner
}
/// Consumes the builder returning a [`Question`]
///
/// [`Question`]: crate::question::Question
pub fn build(self) -> crate::question::Question<'a> {
crate::question::Question::new(self.opts, crate::question::QuestionKind::$type(self.inner))
}
}
impl<'a> From<$builder_name<'a>> for crate::question::Question<'a> {
/// Consumes the builder returning a [`Question`]
///
/// [`Question`]: crate::question::Question
fn from(builder: $builder_name<'a>) -> Self {
builder.build()
}
}
};
}
builder! {
/// The builder for an [`int`] prompt.
///
/// The number is parsed using [`from_str`].
///
/// <img
/// src="https://raw.githubusercontent.com/lutetium-vanadium/requestty/master/assets/int.gif"
/// style="max-height: 11rem"
/// />
///
/// See the various methods for more details on each available option.
///
/// # Examples
///
/// ```
/// use requestty::Question;
///
/// let int = Question::int("age")
/// .message("What is your age?")
/// .validate(|age, previous_answers| {
/// if age > 0 && age < 130 {
/// Ok(())
/// } else {
/// Err(format!("You cannot be {} years old!", age))
/// }
/// })
/// .build();
/// ```
///
/// [`from_str`]: https://doc.rust-lang.org/std/primitive.i64.html#method.from_str
/// [`int`]: crate::question::Question::int
struct IntBuilder: Int -> i64, 10;
declare = r#"let int = Question::int("int")"#;
default = " .default(10)";
filter = " .filter(|n, previous_answers| n + 10)";
validate = " if n.is_positive() {";
validate_on_key = " .validate_on_key(|n, previous_answers| n.is_positive())";
}
builder! {
/// The builder for a [`float`] prompt.
///
/// The number is parsed using [`from_str`], but cannot be `NaN`.
///
/// <img
/// src="https://raw.githubusercontent.com/lutetium-vanadium/requestty/master/assets/float.gif"
/// style="max-height: 11rem"
/// />
///
/// See the various methods for more details on each available option.
///
/// # Examples
///
/// ```
/// use requestty::Question;
///
/// let float = Question::float("number")
/// .message("What is your favourite number?")
/// .validate(|num, previous_answers| {
/// if num.is_finite() {
/// Ok(())
/// } else {
/// Err("Please enter a finite number".to_owned())
/// }
/// })
/// .build();
/// ```
///
/// [`float`]: crate::question::Question::float
/// [`from_str`]: https://doc.rust-lang.org/std/primitive.f64.html#method.from_str
struct FloatBuilder: Float -> f64, 10.0;
declare = r#"let float = Question::float("float")"#;
default = " .default(10.0)";
filter = " .filter(|n, previous_answers| (n * 10000.0).round() / 10000.0)";
validate = " if n.is_sign_positive() {";
validate_on_key = " .validate_on_key(|n, previous_answers| n.is_sign_positive())";
}