inquire/
parser.rs

1//! Type aliases and default implementations for parsers called in prompts
2//! that need to parse user input, such as [Confirm](crate::Confirm) or
3//! [`CustomType`](crate::CustomType).
4//!
5//! Parsers receive the user input to a given prompt and return either
6//! a successful result ([Ok]) containing the parsed value or an empty [Err]
7//! if a value could not be parsed.
8
9/// Type alias for parsers used in [Confirm](crate::Confirm) prompts.
10///
11/// [`BoolParser`]s receive the user input to a given prompt and return either
12/// a successful result ([Ok]) containing the parsed `bool` or an empty [Err]
13/// if a value could not be parsed.
14///
15/// # Examples
16///
17/// ```
18/// use inquire::parser::BoolParser;
19///
20/// let parser: BoolParser = &|ans| match ans {
21///     "si" => Ok(true),
22///     "no" => Ok(false),
23///     _ => Err(()),
24/// };
25/// assert_eq!(Ok(true), parser("si"));
26/// assert_eq!(Ok(false), parser("no"));
27/// assert_eq!(Err(()), parser("yes"));
28/// assert_eq!(Err(()), parser("não"));
29/// ```
30pub type BoolParser<'a> = &'a dyn Fn(&str) -> Result<bool, ()>;
31
32/// Type alias for parsers used in [Confirm](crate::Confirm) prompts.
33///
34/// [`CustomTypeParser`]s receive the user input to a given prompt and return either
35/// a successful result ([Ok]) containing the parsed `bool` or an empty [Err]
36/// if a value could not be parsed.
37///
38/// # Examples
39///
40/// ```
41/// use inquire::parser::CustomTypeParser;
42///
43/// let parser: CustomTypeParser<bool> = &|val| match val {
44///     "si" => Ok(true),
45///     "no" => Ok(false),
46///     _ => Err(()),
47/// };
48/// assert_eq!(Ok(true), parser("si"));
49/// assert_eq!(Ok(false), parser("no"));
50/// assert_eq!(Err(()), parser("yes"));
51/// assert_eq!(Err(()), parser("não"));
52/// ```
53pub type CustomTypeParser<'a, T> = &'a dyn Fn(&str) -> Result<T, ()>;
54
55/// Bool formatter used  by default in [Confirm](crate::Confirm) prompts.
56pub const DEFAULT_BOOL_PARSER: BoolParser = &|ans| {
57    if ans.len() > 3 {
58        return Err(());
59    }
60
61    let ans = ans.to_lowercase();
62
63    match ans.as_str() {
64        "y" | "yes" => Ok(true),
65        "n" | "no" => Ok(false),
66        _ => Err(()),
67    }
68};
69
70#[macro_export]
71#[cfg(feature = "macros")]
72/// Built-in parser creator that checks whether the answer is able to be successfully
73/// parsed to a given type, such as `f64`.
74/// [The given type must implement the FromStr trait.](https://doc.rust-lang.org/stable/std/primitive.str.html#method.parse)
75///
76/// # Arguments
77///
78/// * `$type` - Target type of the parsing operation.
79///
80/// # Examples
81///
82/// ```
83/// use inquire::parse_type;
84/// use inquire::parser::CustomTypeParser;
85///
86/// let parser: CustomTypeParser<f64> = parse_type!(f64);
87/// assert_eq!(Ok(32.44f64), parser("32.44"));
88/// assert_eq!(Ok(11e15f64), parser("11e15"));
89/// assert_eq!(Err(()), parser("32f"));
90/// assert_eq!(Err(()), parser("11^2"));
91/// ```
92macro_rules! parse_type {
93    ($type:ty) => {{
94        &|a| a.parse::<$type>().map_err(|_| ())
95    }};
96}