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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//! Simple crate for parsing user input.
//!
//! # Examples
//!
//! Rust type inference is used to know what to return.
//!
//! ```no_run
//! let username: String = casual::prompt("Please enter your name: ").get();
//! ```
//!
//! [`FromStr`] is used to parse the input, so you can read any type that
//! implements [`FromStr`].
//!
//! ```no_run
//! let age: u32 = casual::prompt("Please enter your age: ").get();
//! ```
//!
//! [`.matches()`] can be used to validate the input data.
//!
//! ```no_run
//! let age: u32 = casual::prompt("Please enter your age again: ")
//!     .matches(|x| *x < 120)
//!     .get();
//! ```
//!
//! A convenience function [`confirm`] is provided for getting a yes or no
//! answer.
//!
//! ```no_run
//! if casual::confirm("Are you sure you want to continue?") {
//!     // continue
//! } else {
//!     panic!("Aborted!");
//! }
//! ```
//!
//! [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
//! [`.matches()`]: struct.Input.html#method.matches
//! [`confirm`]: fn.confirm.html

use std::fmt::{self, Debug, Display};
use std::io::{self, Write};
use std::str::FromStr;

/////////////////////////////////////////////////////////////////////////
// Definitions
/////////////////////////////////////////////////////////////////////////

/// A validator for user input.
struct Validator<T> {
    raw: Box<dyn Fn(&T) -> bool + 'static>,
}

/// An input builder.
pub struct Input<T> {
    prompt: Option<String>,
    prefix: Option<String>,
    suffix: Option<String>,
    default: Option<T>,
    validator: Option<Validator<T>>,
}

/////////////////////////////////////////////////////////////////////////
// Implementations
/////////////////////////////////////////////////////////////////////////

impl<T> Validator<T> {
    /// Construct a new `Validator`.
    fn new<F>(raw: F) -> Self
    where
        F: Fn(&T) -> bool + 'static,
    {
        Self { raw: Box::new(raw) }
    }

    /// Run the validator on the given input.
    fn run(&self, input: &T) -> bool {
        (self.raw)(input)
    }
}

impl<T: Debug> Debug for Input<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Input")
            .field("prefix", &self.prefix)
            .field("prompt", &self.prompt)
            .field("suffix", &self.suffix)
            .field("default", &self.default)
            .finish() // FIXME rust-lang/rust#67364:
                      // use .finish_non_exhaustive() when it's stabilized
    }
}

impl<T> Default for Input<T> {
    /// Construct a new empty `Input`.
    ///
    /// Identical to [`Input::new()`](struct.Input.html#method.new).
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Input<T> {
    /// Construct a new empty `Input`.
    ///
    /// Identical to [`Input::default()`](struct.Input.html#impl-Default).
    pub fn new() -> Self {
        Self {
            prefix: None,
            prompt: None,
            suffix: None,
            default: None,
            validator: None,
        }
    }

    /// Set the prompt to display before waiting for user input.
    pub fn prompt<S: Into<String>>(mut self, prompt: S) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    /// Set the prompt prefix.
    pub fn prefix<S: Into<String>>(mut self, prefix: S) -> Self {
        self.prefix = Some(prefix.into());
        self
    }

    /// Set the prompt suffix.
    pub fn suffix<S: Into<String>>(mut self, suffix: S) -> Self {
        self.suffix = Some(suffix.into());
        self
    }

    /// Set the default value.
    ///
    /// If set, this will be returned in the event the user enters an empty
    /// input.
    pub fn default(mut self, default: T) -> Self {
        self.default = Some(default);
        self
    }

    /// Check input values.
    ///
    /// If set, this function will be called on the parsed user input and only
    /// if it passes will we return the value.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use casual::Input;
    /// let num: u32 = Input::new().matches(|x| *x != 10).get();
    /// ```
    pub fn matches<F>(mut self, matches: F) -> Self
    where
        F: Fn(&T) -> bool + 'static,
    {
        self.validator = Some(Validator::new(matches));
        self
    }
}

fn read_line(prompt: &Option<String>) -> io::Result<String> {
    if let Some(prompt) = prompt {
        let mut stdout = io::stdout();
        stdout.write_all(prompt.as_bytes())?;
        stdout.flush()?;
    }
    let mut result = String::new();
    io::stdin().read_line(&mut result)?;
    Ok(result)
}

impl<T> Input<T>
where
    T: FromStr,
    <T as FromStr>::Err: Display,
{
    fn try_get_with<F>(self, read_line: F) -> io::Result<T>
    where
        F: Fn(&Option<String>) -> io::Result<String>,
    {
        let Self {
            prompt,
            prefix,
            suffix,
            default,
            validator,
        } = self;

        let prompt = prompt.map(move |prompt| {
            let mut p = String::new();
            if let Some(prefix) = prefix {
                p.push_str(&prefix);
            }
            p.push_str(&prompt);
            if let Some(suffix) = suffix {
                p.push_str(&suffix);
            }
            p
        });

        Ok(loop {
            match read_line(&prompt)?.trim() {
                "" => {
                    if let Some(default) = default {
                        break default;
                    } else {
                        continue;
                    }
                }
                raw => match raw.parse() {
                    Ok(result) => {
                        if let Some(validator) = &validator {
                            if !validator.run(&result) {
                                println!("Error: invalid input");
                                continue;
                            }
                        }
                        break result;
                    }
                    Err(err) => {
                        println!("Error: {}", err);
                        continue;
                    }
                },
            }
        })
    }

    #[inline]
    fn try_get(self) -> io::Result<T> {
        self.try_get_with(read_line)
    }

    /// Consumes the `Input` and reads the input from the user.
    ///
    /// This function uses [`FromStr`] to parse the input data.
    ///
    /// ```no_run
    /// # use casual::Input;
    /// let num: u32 = Input::new().prompt("Enter a number: ").get();
    /// ```
    ///
    /// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
    pub fn get(self) -> T {
        self.try_get().unwrap()
    }

    /// Consumes the `Input` and applies the given function to it.
    ///
    /// This function uses [`FromStr`] to parse the input data. The result is
    /// then fed to the given closure.
    ///
    /// ```no_run
    /// # use casual::Input;
    /// let value = Input::new().map(|s: String| &s.to_lowercase() == "yes");
    /// ```
    ///
    /// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
    pub fn map<F, U>(self, map: F) -> U
    where
        F: Fn(T) -> U,
    {
        map(self.get())
    }
}

/////////////////////////////////////////////////////////////////////////
// Shortcut functions
/////////////////////////////////////////////////////////////////////////

/// Returns a new empty `Input`.
///
/// # Examples
///
/// Read in something without any prompt.
///
/// ```no_run
/// # use casual::input;
/// let data: String = input().get();
/// ```
pub fn input<T>() -> Input<T> {
    Input::new()
}

/// Returns an `Input` that prompts the user for input.
///
/// # Examples
///
/// Read in a simple string:
///
/// ```no_run
/// # use casual::prompt;
/// let username: String = prompt("Please enter your name: ").get();
/// ```
///
/// Types that implement [`FromStr`] will be automatically parsed.
///
/// ```no_run
/// # use casual::prompt;
/// let years = prompt("How many years have you been coding Rust: ")
///     .default(0)
///     .get();
/// ```
///
/// [`FromStr`]: https://doc.rust-lang.org/std/str/trait.FromStr.html
pub fn prompt<S, T>(text: S) -> Input<T>
where
    S: Into<String>,
{
    Input::new().prompt(text)
}

/// Prompts the user for confirmation (yes/no).
///
/// # Examples
///
/// ```no_run
/// # use casual::confirm;
/// if confirm("Are you sure you want to continue?") {
///     // continue
/// } else {
///     panic!("Aborted!");
/// }
/// ```
pub fn confirm<S: Into<String>>(text: S) -> bool {
    prompt(text)
        .suffix(" [y/N] ")
        .default("n".to_string())
        .matches(|s| matches!(&*s.trim().to_lowercase(), "n" | "no" | "y" | "yes"))
        .map(|s| matches!(&*s.to_lowercase(), "y" | "yes"))
}