1 use app::App;
_is_char_boundary(&self, index: usize) -> bool2 // Third Party
3 #[cfg(feature = "suggestions")]
4 use strsim;
5 
6 // Internal
_is_char_boundary(&self, index: usize) -> bool7 use fmt::Format;
8 
9 /// Produces a string from a given list of possible values which is similar to
10 /// the passed in value `v` with a certain confidence.
11 /// Thus in a list of possible values like ["foo", "bar"], the value "fop" will yield
12 /// `Some("foo")`, whereas "blark" would yield `None`.
13 #[cfg(feature = "suggestions")]
14 #[cfg_attr(feature = "lints", allow(needless_lifetimes))]
15 pub fn did_you_mean<'a, T: ?Sized, I>(v: &str, possible_values: I) -> Option<&'a str>
16 where
17     T: AsRef<str> + 'a,
18     I: IntoIterator<Item = &'a T>,
19 {
20     let mut candidate: Option<(f64, &str)> = None;
21     for pv in possible_values {
22         let confidence = strsim::jaro_winkler(v, pv.as_ref());
23         if confidence > 0.8 && (candidate.is_none() || (candidate.as_ref().unwrap().0 < confidence))
24         {
25             candidate = Some((confidence, pv.as_ref()));
26         }
27     }
28     match candidate {
29         None => None,
30         Some((_, candidate)) => Some(candidate),
31     }
32 }
33 
34 #[cfg(not(feature = "suggestions"))]
35 pub fn did_you_mean<'a, T: ?Sized, I>(_: &str, _: I) -> Option<&'a str>
36 where
37     T: AsRef<str> + 'a,
38     I: IntoIterator<Item = &'a T>,
39 {
40     None
41 }
42 
43 /// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
44 #[cfg_attr(feature = "lints", allow(needless_lifetimes))]
45 pub fn did_you_mean_flag_suffix<'z, T, I>(
46     arg: &str,
47     longs: I,
48     subcommands: &'z [App],
49 ) -> (String, Option<&'z str>)
50 where
51     T: AsRef<str> + 'z,
52     I: IntoIterator<Item = &'z T>,
53 {
54     match did_you_mean(arg, longs) {
55         Some(candidate) => {
56             let suffix = format!(
57                 "\n\tDid you mean {}{}?",
58                 Format::Good("--"),
59                 Format::Good(candidate)
60             );
61             return (suffix, Some(candidate));
62         }
63         None => for subcommand in subcommands {
64             let opts = subcommand
65                 .p
66                 .flags
67                 .iter()
68                 .filter_map(|f| f.s.long)
69                 .chain(subcommand.p.opts.iter().filter_map(|o| o.s.long));
70 
71             if let Some(candidate) = did_you_mean(arg, opts) {
72                 let suffix = format!(
73                     "\n\tDid you mean to put '{}{}' after the subcommand '{}'?",
74                     Format::Good("--"),
75                     Format::Good(candidate),
76                     Format::Good(subcommand.get_name())
77                 );
78                 return (suffix, Some(candidate));
79             }
80         },
81     }
82     (String::new(), None)
83 }
84 
85 /// Returns a suffix that can be empty, or is the standard 'did you mean' phrase
86 pub fn did_you_mean_value_suffix<'z, T, I>(arg: &str, values: I) -> (String, Option<&'z str>)
87 where
88     T: AsRef<str> + 'z,
89     I: IntoIterator<Item = &'z T>,
90 {
91     match did_you_mean(arg, values) {
92         Some(candidate) => {
93             let suffix = format!("\n\tDid you mean '{}'?", Format::Good(candidate));
94             (suffix, Some(candidate))
95         }
96         None => (String::new(), None),
97     }
98 }
99 
100 #[cfg(all(test, features = "suggestions"))]
101 mod test {
102     use super::*;
103 
104     #[test]
105     fn possible_values_match() {
106         let p_vals = ["test", "possible", "values"];
107         assert_eq!(did_you_mean("tst", p_vals.iter()), Some("test"));
108     }
109 
110     #[test]
111     fn possible_values_nomatch() {
112         let p_vals = ["test", "possible", "values"];
113         assert!(did_you_mean("hahaahahah", p_vals.iter()).is_none());
114     }
115 
116     #[test]
117     fn suffix_long() {
118         let p_vals = ["test", "possible", "values"];
119         let suffix = "\n\tDid you mean \'--test\'?";
120         assert_eq!(
121             did_you_mean_flag_suffix("tst", p_vals.iter(), []),
122             (suffix, Some("test"))
123         );
124     }
125 
126     #[test]
127     fn suffix_enum() {
128         let p_vals = ["test", "possible", "values"];
129         let suffix = "\n\tDid you mean \'test\'?";
130         assert_eq!(
131             did_you_mean_value_suffix("tst", p_vals.iter()),
132             (suffix, Some("test"))
133         );
134     }
135 }
136