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
use std::{
    fmt::Debug,
    io::{stdin, stdout, Write},
    str::FromStr,
};

/// get input with the given output, similar to how input function works in python
///
/// crashes if failed to flush or read line
pub fn input(output: &str) -> String {
    print!("{}", output);
    stdout().flush().expect("Flush Failed");

    let mut input = String::new();
    stdin().read_line(&mut input).expect("Unable to read line!");
    input
}

pub fn input_trim(output: &str) -> String {
    let str = input(output);
    str.trim().to_string()
}

/// input with the given type, crashes if failed to convert
pub fn input_other<T>(output: &str) -> T
where
    T: FromStr,
    T::Err: Debug,
{
    input(output)
        .trim()
        .parse::<T>()
        .expect("Failed to convert value!")
}

/// repeat forever until the user gets the correct input
pub fn input_other_repeat<T>(output: &str) -> T
where
    T: FromStr,
{
    let mut input_str = input(output);
    loop {
        match input_str.trim().parse::<T>() {
            Ok(value) => return value,
            Err(_) => {
                input_str =
                    input(format!("{} is not a valid input! Try again: ", input_str).as_str());
                continue;
            }
        }
    }
}

/// get input or if failed then return with the default value
pub fn input_other_or<T>(output: &str, default: T) -> T
where
    T: FromStr,
{
    input(output).trim().parse::<T>().unwrap_or(default)
}

/// get input or if failed then return with the default value
pub fn input_other_or_else<T, F: FnOnce() -> T>(output: &str, func: F) -> T
where
    T: FromStr,
{
    input_other_or(output, func())
}