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
//! General type aliases.
use crateCustomUserError;
/// Type alias to represent the function used to Score and filter options.
///
/// The function receives:
/// - Current user input, filter value
/// - Current option being evaluated, with type preserved
/// - String value of the current option
/// - Index of the current option in the original list
///
/// The return type should be a score determining the order options should be displayed to the user.
/// The greater the score, the higher on the list it will be displayed.
///
/// # Examples
///
/// ```
/// use inquire::type_aliases::Scorer;
///
/// // Implement simpler 'contains' filter that maintains the current order
/// // If all scores are the same, no sorting will occur
/// let filter: Scorer<str> =
/// &|input, _option, string_value, _idx| -> Option<i64> {
/// let filter = input.to_lowercase();
/// match string_value.to_lowercase().contains(&filter) {
/// true => Some(0),
/// false => None,
/// }
/// };
///
/// assert_eq!(None, filter("sa", "New York", "New York", 0));
/// assert_eq!(None, filter("sa", "Los Angeles", "Los Angeles", 1));
/// assert_eq!(None, filter("sa", "Chicago", "Chicago", 2));
/// assert_eq!(None, filter("sa", "Houston", "Houston", 3));
/// assert_eq!(None, filter("sa", "Phoenix", "Phoenix", 4));
/// assert_eq!(None, filter("sa", "Philadelphia", "Philadelphia", 5));
/// assert_eq!(Some(0), filter("sa", "San Antonio", "San Antonio", 6));
/// assert_eq!(Some(0), filter("sa", "San Diego", "San Diego", 7));
/// assert_eq!(None, filter("sa", "Dallas", "Dallas", 8));
/// assert_eq!(Some(0), filter("sa", "San Francisco", "San Francisco", 9));
/// assert_eq!(None, filter("sa", "Austin", "Austin", 10));
/// assert_eq!(None, filter("sa", "Jacksonville", "Jacksonville", 11));
/// assert_eq!(Some(0), filter("sa", "San Jose", "San Jose", 12));
///```
///
///
///
/// Default implementation for fuzzy search (almost)
///```
/// use inquire::type_aliases::Scorer;
/// use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
///
/// pub const DEFAULT_SCORER: Scorer<str> =
/// &|input, _option, string_value, _idx| -> Option<i64> {
/// let matcher = SkimMatcherV2::default().ignore_case();
/// matcher.fuzzy_match(string_value, input)
/// };
///
/// assert_eq!(None, DEFAULT_SCORER("sa", &"New York", "New York", 0));
/// assert_eq!(Some(49), DEFAULT_SCORER("sa", &"Sacramento", "Sacramento", 1));
/// assert_eq!(Some(35), DEFAULT_SCORER("sa", &"Kansas", "Kansas", 2));
/// assert_eq!(Some(35), DEFAULT_SCORER("sa", &"Mesa", "Mesa", 3));
/// assert_eq!(None, DEFAULT_SCORER("sa", &"Phoenix", "Phoenix", 4));
/// assert_eq!(None, DEFAULT_SCORER("sa", &"Philadelphia", "Philadelphia", 5));
/// assert_eq!(Some(49), DEFAULT_SCORER("sa", &"San Antonio", "San Antonio", 6));
/// assert_eq!(Some(49), DEFAULT_SCORER("sa", &"San Diego", "San Diego", 7));
/// assert_eq!(None, DEFAULT_SCORER("sa", &"Dallas", "Dallas", 8));
/// assert_eq!(Some(49), DEFAULT_SCORER("sa", &"San Francisco", "San Francisco", 9));
/// assert_eq!(None, DEFAULT_SCORER("sa", &"Austin", "Austin", 10));
/// assert_eq!(None, DEFAULT_SCORER("sa", &"Jacksonville", "Jacksonville", 11));
/// assert_eq!(Some(49), DEFAULT_SCORER("sa", &"San Jose", "San Jose", 12));
/// ```
pub type Scorer<'a, T> = &'a dyn Fn ;
/// Type alias to represent the function used to sort a slice of (index, score) tuples.
///
/// The function receives a mutable slice of tuples, where each tuple contains:
/// - The index of the option in the original list
/// - The score assigned to that option
pub type Sorter<'a> = &'a dyn Fn;
/// Type alias to represent the function used to retrieve text input suggestions.
/// The function receives the current input and should return a collection of strings
/// containing the suggestions to be made to the user.
pub type Suggester<'a> = &'a dyn Fn ;
/// Type alias to represent the function used to retrieve an optional autocompletion suggestion.
/// The function receives the current input and should return the suggestion (if any)
/// that will replace the current input.
pub type Completer<'a> = &'a dyn Fn ;