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
use super::*;
use std::str::FromStr;

#[cfg(feature = "nightly")]
/// Blanket impl for `FromStr` types. Re-prompts until `FromStr` parsing succeeds.
default impl<T> Promptable for T
where
    T: FromStr + Display,
    <T as FromStr>::Err: ::std::error::Error,
{
    /// Prompt until the input parses into the specified type
    ///
    /// ```no_run
    /// use promptly::Promptable;
    /// u32::prompt("Enter your age")?;
    /// # Result::<_,Box<std::error::Error>>::Ok(())
    /// ```
    fn prompt<S: AsRef<str>>(msg: S) -> Result<Self> {
        prompt_parse(msg)
    }

    /// Prompt for an optional, parseable value.
    ///
    /// Returns `None` if empty, otherwise prompts until input parses into specified type.
    ///
    /// ```no_run
    /// # use std::net::IpAddr;
    /// use promptly::Promptable;
    /// IpAddr::prompt_opt("Enter your IP Address (optional)")?;
    /// # Result::<_,Box<std::error::Error>>::Ok(())
    /// ```
    fn prompt_opt<S: AsRef<str>>(msg: S) -> Result<Option<Self>> {
        prompt_parse_opt(msg)
    }

    /// Prompt for a parseable value with a provided fallback value if empty.
    ///
    /// ```no_run
    /// use promptly::Promptable;
    /// u32::prompt_default("Enter the year", 2018)?;
    /// # Result::<_,Box<std::error::Error>>::Ok(())
    /// ```
    ///
    /// Default value is visible in the prompt as: `(default=USA)`
    fn prompt_default<S: AsRef<str>>(msg: S, default: Self) -> Result<Self> {
        let msg = format!("{} (default={})", msg.as_ref(), default);
        prompt_parse_opt(msg).unwrap_or(default)
    }
}

impl Promptable for String {
    /// Prompt until you get a non-empty string
    ///
    /// ```no_run
    /// use promptly::Promptable;
    /// String::prompt("Enter your name")?;
    /// # Result::<_,Box<std::error::Error>>::Ok(())
    /// ```
    fn prompt<S: AsRef<str>>(msg: S) -> Result<Self> {
        Prompter::new().prompt_nonempty(msg)
    }

    /// Prompt for an optional string
    ///
    /// ```no_run
    /// use promptly::Promptable;
    /// String::prompt_opt("Enter your phone number (optional)")?;
    /// # Result::<_,Box<std::error::Error>>::Ok(())
    /// ```
    fn prompt_opt<S: AsRef<str>>(msg: S) -> Result<Option<Self>> {
        Prompter::new().prompt_opt(msg)
    }

    /// Prompt for a string with a provided fallback value if empty.
    ///
    /// ```no_run
    /// use promptly::Promptable;
    /// String::prompt_default("Enter your country", "USA".into())?;
    /// # Result::<_,Box<std::error::Error>>::Ok(())
    /// ```
    ///
    /// Default value is visible in the prompt as: `(default=USA)`
    fn prompt_default<S: AsRef<str>>(msg: S, default: Self) -> Result<Self> {
        let msg = format!("{} (default={})", msg.as_ref(), default);
        Ok(Prompter::new().prompt_opt(msg)?.unwrap_or(default))
    }
}

// Macro to provide Promptable implementations until specialization stabilizes
macro_rules! impl_promptable_from_str {
    ($t:ty) => {
        impl Promptable for $t {
            fn prompt<S: AsRef<str>>(msg: S) -> Result<Self> {
                prompt_parse(msg)
            }

            fn prompt_opt<S: AsRef<str>>(msg: S) -> Result<Option<Self>> {
                prompt_parse_opt(msg)
            }

            fn prompt_default<S: AsRef<str>>(msg: S, default: Self) -> Result<Self> {
                let msg = format!("{} (default={})", msg.as_ref(), default);
                Ok(prompt_parse_opt(msg)?.unwrap_or(default))
            }
        }
    };
}

impl_promptable_from_str!(char);
impl_promptable_from_str!(u8);
impl_promptable_from_str!(u16);
impl_promptable_from_str!(u32);
impl_promptable_from_str!(u64);
impl_promptable_from_str!(u128);
impl_promptable_from_str!(usize);
impl_promptable_from_str!(i8);
impl_promptable_from_str!(i16);
impl_promptable_from_str!(i32);
impl_promptable_from_str!(i64);
impl_promptable_from_str!(i128);
impl_promptable_from_str!(isize);
impl_promptable_from_str!(f32);
impl_promptable_from_str!(f64);
impl_promptable_from_str!(::std::net::IpAddr);
impl_promptable_from_str!(::std::net::Ipv4Addr);
impl_promptable_from_str!(::std::net::Ipv6Addr);
impl_promptable_from_str!(::std::net::SocketAddrV4);
impl_promptable_from_str!(::std::net::SocketAddrV6);
impl_promptable_from_str!(::std::num::NonZeroI128);
impl_promptable_from_str!(::std::num::NonZeroI64);
impl_promptable_from_str!(::std::num::NonZeroI32);
impl_promptable_from_str!(::std::num::NonZeroI16);
impl_promptable_from_str!(::std::num::NonZeroI8);
impl_promptable_from_str!(::std::num::NonZeroIsize);
impl_promptable_from_str!(::std::num::NonZeroU128);
impl_promptable_from_str!(::std::num::NonZeroU64);
impl_promptable_from_str!(::std::num::NonZeroU32);
impl_promptable_from_str!(::std::num::NonZeroU16);
impl_promptable_from_str!(::std::num::NonZeroU8);
impl_promptable_from_str!(::std::num::NonZeroUsize);

#[cfg(feature = "url")]
impl_promptable_from_str!(url::Url);

fn prompt_parse<T, S>(msg: S) -> Result<T>
where
    T: FromStr,
    <T as FromStr>::Err: ::std::error::Error,
    S: AsRef<str>,
{
    Prompter::new().prompt_then(msg, |s| T::from_str(s.as_ref()).map_err(|e| e.to_string()))
}

fn prompt_parse_opt<T, S>(msg: S) -> Result<Option<T>>
where
    T: FromStr,
    <T as FromStr>::Err: ::std::error::Error,
    S: AsRef<str>,
{
    Prompter::new().prompt_then(msg, |s| match s.trim() {
        "" => Ok(None),
        _ => match T::from_str(s.as_ref()) {
            Ok(n) => Ok(Some(n)),
            Err(e) => Err(e.to_string()),
        },
    })
}