1 #[cfg(feature = "suggestions")]
2 use std::cmp::Ordering;
3 
4 // Internal
5 use crate::build::App;
6 
7 /// Produces multiple strings from a given list of possible values which are similar
8 /// to the passed in value `v` within a certain confidence by least confidence.
9 /// Thus in a list of possible values like ["foo", "bar"], the value "fop" will yield
10 /// `Some("foo")`, whereas "blark" would yield `None`.
11 #[cfg(feature = "suggestions")]
did_you_mean<T, I>(v: &str, possible_values: I) -> Vec<String> where T: AsRef<str>, I: IntoIterator<Item = T>,12 pub(crate) fn did_you_mean<T, I>(v: &str, possible_values: I) -> Vec<String>
13 where
14     T: AsRef<str>,
15     I: IntoIterator<Item = T>,
16 {
17     let mut candidates: Vec<(f64, String)> = possible_values
18         .into_iter()
19         .map(|pv| (strsim::jaro_winkler(v, pv.as_ref()), pv.as_ref().to_owned()))
20         .filter(|(confidence, _)| *confidence > 0.8)
21         .collect();
22     candidates.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
23     candidates.into_iter().map(|(_, pv)| pv).collect()
24 }
25 
26 #[cfg(not(feature = "suggestions"))]
did_you_mean<T, I>(_: &str, _: I) -> Vec<String> where T: AsRef<str>, I: IntoIterator<Item = T>,27 pub(crate) fn did_you_mean<T, I>(_: &str, _: I) -> Vec<String>
28 where
29     T: AsRef<str>,
30     I: IntoIterator<Item = T>,
31 {
32     Vec::new()
33 }
34 
35 /// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
did_you_mean_flag<I, T>( arg: &str, remaining_args: &[&str], longs: I, subcommands: &mut [App], ) -> Option<(String, Option<String>)> where T: AsRef<str>, I: IntoIterator<Item = T>,36 pub(crate) fn did_you_mean_flag<I, T>(
37     arg: &str,
38     remaining_args: &[&str],
39     longs: I,
40     subcommands: &mut [App],
41 ) -> Option<(String, Option<String>)>
42 where
43     T: AsRef<str>,
44     I: IntoIterator<Item = T>,
45 {
46     use crate::mkeymap::KeyType;
47 
48     match did_you_mean(arg, longs).pop() {
49         Some(candidate) => Some((candidate, None)),
50         None => subcommands
51             .iter_mut()
52             .filter_map(|subcommand| {
53                 subcommand._build();
54 
55                 let longs = subcommand.args.keys().filter_map(|a| {
56                     if let KeyType::Long(v) = a {
57                         Some(v.to_string_lossy().into_owned())
58                     } else {
59                         None
60                     }
61                 });
62 
63                 let subcommand_name = subcommand.get_name();
64 
65                 let candidate = did_you_mean(arg, longs).pop()?;
66                 let score = remaining_args.iter().position(|x| *x == subcommand_name)?;
67                 Some((score, (candidate, Some(subcommand_name.to_string()))))
68             })
69             .min_by_key(|(x, _)| *x)
70             .map(|(_, suggestion)| suggestion),
71     }
72 }
73 
74 #[cfg(all(test, features = "suggestions"))]
75 mod test {
76     use super::*;
77 
78     #[test]
possible_values_match()79     fn possible_values_match() {
80         let p_vals = ["test", "possible", "values"];
81         assert_eq!(did_you_mean("tst", p_vals.iter()), Some("test"));
82     }
83 
84     #[test]
possible_values_match()85     fn possible_values_match() {
86         let p_vals = ["test", "temp"];
87         assert_eq!(did_you_mean("te", p_vals.iter()), Some("test"));
88     }
89 
90     #[test]
possible_values_nomatch()91     fn possible_values_nomatch() {
92         let p_vals = ["test", "possible", "values"];
93         assert!(did_you_mean("hahaahahah", p_vals.iter()).is_none());
94     }
95 
96     #[test]
flag()97     fn flag() {
98         let p_vals = ["test", "possible", "values"];
99         assert_eq!(
100             did_you_mean_flag("tst", p_vals.iter(), []),
101             Some(("test", None))
102         );
103     }
104 }
105