inquire/type_aliases.rs
1//! General type aliases.
2
3use crate::error::CustomUserError;
4
5/// Type alias to represent the function used to filter options.
6///
7/// The function receives:
8/// - Current user input, filter value
9/// - Current option being evaluated, with type preserved
10/// - String value of the current option
11/// - Index of the current option in the original list
12///
13/// The return type should be whether the current option should be displayed to the user.
14///
15/// # Examples
16///
17/// ```
18/// use inquire::type_aliases::Filter;
19///
20/// let filter: Filter<str> = &|filter, _, string_value, _| -> bool {
21/// let filter = filter.to_lowercase();
22///
23/// string_value.to_lowercase().starts_with(&filter)
24/// };
25/// assert_eq!(false, filter("san", "New York", "New York", 0));
26/// assert_eq!(false, filter("san", "Los Angeles", "Los Angeles", 1));
27/// assert_eq!(false, filter("san", "Chicago", "Chicago", 2));
28/// assert_eq!(false, filter("san", "Houston", "Houston", 3));
29/// assert_eq!(false, filter("san", "Phoenix", "Phoenix", 4));
30/// assert_eq!(false, filter("san", "Philadelphia", "Philadelphia", 5));
31/// assert_eq!(true, filter("san", "San Antonio", "San Antonio", 6));
32/// assert_eq!(true, filter("san", "San Diego", "San Diego", 7));
33/// assert_eq!(false, filter("san", "Dallas", "Dallas", 8));
34/// assert_eq!(true, filter("san", "San Francisco", "San Francisco", 9));
35/// assert_eq!(false, filter("san", "Austin", "Austin", 10));
36/// assert_eq!(false, filter("san", "Jacksonville", "Jacksonville", 11));
37/// assert_eq!(true, filter("san", "San Jose", "San Jose", 12));
38/// ```
39pub type Filter<'a, T> = &'a dyn Fn(&str, &T, &str, usize) -> bool;
40
41/// Type alias to represent the function used to retrieve text input suggestions.
42/// The function receives the current input and should return a collection of strings
43/// containing the suggestions to be made to the user.
44pub type Suggester<'a> = &'a dyn Fn(&str) -> Result<Vec<String>, CustomUserError>;
45
46/// Type alias to represent the function used to retrieve an optional autocompletion suggestion.
47/// The function receives the current input and should return the suggestion (if any)
48/// that will replace the current input.
49pub type Completer<'a> = &'a dyn Fn(&str) -> Result<Option<String>, CustomUserError>;