      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
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176

    use std::str::Chars;

    pub enum UsageToken<'u> {
        Name(&'u str, Option<bool>),
        Short(char),
        Long(&'u str),
        Help(&'u str),
        Multiple,
    }

    pub struct UsageParser<'u> {
        usage: &'u str,
        chars: Chars<'u>,
        s: usize,
        e: usize,
    }

    impl<'u> UsageParser<'u> {
        pub fn with_usage(u: &'u str) -> UsageParser<'u> {
            UsageParser {
                usage: u,
                chars: u.chars(),
                s: 0,
                e: 0,
            }
        }

        #[cfg_attr(feature = "lints", allow(while_let_on_iterator))]
        fn name(&mut self, c: char) -> Option<UsageToken<'u>> {
            if self.e != 0 {
                self.e += 1;
            }
            self.s = self.e + 1;
            let closing = match c {
                '[' => ']',
                '<' => '>',
                _ => unreachable!(),
            };
            while let Some(c) = self.chars.next() {
                self.e += 1;
                if c == closing {
                    break;
                }
            }
            if self.e > self.usage.len() {
                return None;
            }

            let name = &self.usage[self.s..self.e];

            Some(UsageToken::Name(name,
                                  if c == '<' {
                                      Some(true)
                                  } else {
                                      None
                                  }))
        }

        #[cfg_attr(feature = "lints", allow(while_let_on_iterator))]
        fn help(&mut self) -> Option<UsageToken<'u>> {
            self.s = self.e + 2;
            self.e = self.usage.len() - 1;

            while let Some(_) = self.chars.next() {
                continue;
            }

            Some(UsageToken::Help(&self.usage[self.s..self.e]))
        }

        #[cfg_attr(feature = "lints", allow(while_let_on_iterator))]
        fn long_arg(&mut self) -> Option<UsageToken<'u>> {
            if self.e != 1 {
                self.e += 1;
            }

            self.s = self.e + 1;

            while let Some(c) = self.chars.next() {
                self.e += 1;
                if c == ' ' || c == '=' || c == '.' {
                    break;
                }
            }

            if self.e > self.usage.len() {
                return None;
            } else if self.e == self.usage.len() - 1 {
                return Some(UsageToken::Long(&self.usage[self.s..]));
            }

            Some(UsageToken::Long(&self.usage[self.s..self.e]))
        }

        fn short_arg(&mut self, c: char) -> Option<UsageToken<'u>> {
            // When short is first don't increment e
            if self.e != 1 {
                self.e += 1;
            }
            if !c.is_alphanumeric() {
                return None;
            }
            Some(UsageToken::Short(c))
        }

        fn multiple(&mut self) -> bool {
            self.e += 1;
            let mut mult = false;
            for _ in 0..2 {
                self.e += 1;
                match self.chars.next() {
                    // longs consume one '.' so they match '.. ' whereas shorts can
                    // match '...'
                    Some('.') | Some(' ') => {
                        mult = true;
                    }
                    _ => {
                        // if there is no help or following space all we can match is '..'
                        if self.e == self.usage.len() - 1 {
                            mult = true;
                        }
                        break;
                    }
                }
            }

            mult
        }
    }

    impl<'u> Iterator for UsageParser<'u> {
        type Item = UsageToken<'u>;

        fn next(&mut self) -> Option<UsageToken<'u>> {
            loop {
                match self.chars.next() {
                    Some(c) if c == '[' || c == '<' => {
                        return self.name(c);
                    }
                    Some('\'') => {
                        return self.help();
                    }
                    Some('-') => {
                        self.e += 1;
                        match self.chars.next() {
                            Some('-') => {
                                return self.long_arg();
                            }
                            Some(c) => {
                                return self.short_arg(c);
                            }
                            _ => {
                                return None;
                            }
                        }
                    }
                    Some('.') => {
                        if self.multiple() {
                            return Some(UsageToken::Multiple);
                        }
                    }
                    Some(' ') | Some('=') | Some(']') | Some('>') | Some('\t') | Some(',') => {
                        self.e += 1;
                        continue;
                    }
                    None => {
                        return None;
                    }
                    Some(c) => panic!("Usage parser error, unexpected \
                                      \"{}\" at \"{}\", check from_usage call",
                                      c,
                                      self.usage),
                }
            }
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)