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
/// The current state of the parser.
///
/// `args[index]` is the current element being parsed.
/// `args[index][point]` is the current character being parsed.
///
/// After parsing is complete (that is, [`Opt(None, None)`](struct.Opt.html) is returned), any
/// remaining arguments can be found in `args` beginning at `index`.
///
/// # Example
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use getopt::prelude::*;
///
/// // args = ["program", "-abc", "foo"];
/// # let args: Vec<String> = vec!["program", "-abc", "foo"]
/// #     .into_iter()
/// #     .map(String::from)
/// #     .collect();
/// let optstring = "ab:c";
/// let mut state = State::new();
///
/// getopt(&args, optstring, &mut state)?;
/// assert_eq!(State { index: 1, point: 2 }, state);
/// getopt(&args, optstring, &mut state)?;
/// assert_eq!(State { index: 2, point: 0 }, state);
/// getopt(&args, optstring, &mut state)?;
/// assert_eq!(State { index: 2, point: 0 }, state);
/// assert_eq!("foo", args[state.index]);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct State {
    pub index: usize,
    pub point: usize,
}

impl Default for State {
    fn default() -> State {
        State { index: 1, point: 0 }
    }
}

impl State {
    /// Constructs a new `State`, with `index` and `point` initialized to 1 and
    /// 0, respectively.
    pub fn new() -> State {
        State::default()
    }
}