cute/
lib.rs

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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#![doc = include_str!("../README.md")]
pub mod err;
pub mod opt;
pub mod val;

pub mod prelude {
    pub use crate::opt::*;
    pub use crate::val::*;
    pub use crate::Cute;
}

pub use err::Error;

use std::collections::HashMap;

use opt::{State, StateOpt};
use val::ValueParser;

#[derive(Default)]
/// The main parser struct that holds options and their values.
///
/// # Type Parameters
/// * `S` - The state type that implements the `State` trait
pub struct Cute<S: State> {
    opts: Vec<Box<dyn StateOpt<S = S>>>,
    values: HashMap<S, Vec<String>>,
}

impl<S: State> Cute<S> {
    /// Creates a new Cute instance with empty options and values.
    pub fn new() -> Self {
        Self {
            opts: vec![],
            values: HashMap::default(),
        }
    }

    /// Adds a new option to the parser.
    ///
    /// # Arguments
    /// * `arg` - The option to add, must implement `StateOpt`
    pub fn add(&mut self, arg: impl StateOpt<S = S> + 'static) -> &mut Self {
        self.opts.push(Box::new(arg));
        self
    }

    /// Gets an option by its state.
    ///
    /// # Arguments
    /// * `s` - The state to search for
    ///
    /// # Returns
    /// Option containing a reference to the option if found
    pub fn get(&self, s: S) -> Option<&dyn StateOpt<S = S>> {
        self.opts.iter().find(|v| v.state() == &s).map(|v| &**v)
    }

    pub fn has(&self, s: S) -> bool {
        self.opts.iter().any(|v| v.state() == &s)
    }

    /// Gets a parsed value for a given state.
    ///
    /// # Type Parameters
    /// * `V` - The value parser type
    ///
    /// # Arguments
    /// * `s` - The state to get the value for
    ///
    /// # Returns
    /// Result containing the parsed value or an error
    pub fn value<V: ValueParser>(&self, s: S) -> Result<V::Out<'_>, V::Error> {
        let val = self.values.get(&s).and_then(|v| v.first());

        V::parse(val)
    }

    pub fn pop_raw_value(&mut self, s: S) -> Result<String, Error> {
        self.values
            .get_mut(&s)
            .and_then(|v| v.pop())
            .ok_or_else(|| Error::Value(format!("{s:?}")))
    }

    pub fn raw_values(&self, s: S) -> Result<&[String], Error> {
        self.values
            .get(&s)
            .map(|v| v.as_ref())
            .ok_or_else(|| Error::Value(format!("{s:?}")))
    }

    /// Parses command-line arguments from an iterator.
    ///
    /// # Type Parameters
    /// * `I` - The iterator type
    ///
    /// # Arguments
    /// * `iter` - The iterator of arguments to parse
    ///
    /// # Returns
    /// Result containing unprocessed arguments or an error
    pub fn parse<I: Iterator>(&mut self, iter: I) -> Result<Vec<String>, Error>
    where
        I::Item: ToString,
    {
        let mut iter = iter.map(|v| v.to_string());
        let mut rets = vec![];

        while let Some(item) = iter.next() {
            let mut matched = false;

            for opt in self.opts.iter_mut() {
                if let opt::Match::Okay(val) = opt.r#match(&item) {
                    matched = true;

                    let val = if opt.consume() {
                        if let Some(val) = val {
                            val.to_string()
                        } else {
                            iter.next()
                                .ok_or_else(|| Error::Argument(opt.name().to_string()))?
                        }
                    } else {
                        String::default()
                    };

                    self.values
                        .entry(opt.state().clone())
                        .or_default()
                        .push(val);
                }
            }
            if !matched {
                rets.push(item);
            }
        }
        Ok(rets)
    }

    /// Parses arguments from the environment.
    ///
    /// # Returns
    /// Result containing unprocessed arguments or an error
    pub fn parse_env(&mut self) -> Result<Vec<String>, Error> {
        self.parse(std::env::args())
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        opt::{option, switch},
        Error,
    };

    #[test]
    fn opt_test() {
        assert!(opt_test_impl().is_ok());
    }

    fn opt_test_impl() -> Result<(), Error> {
        use super::*;

        #[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
        enum TestState {
            Cmd,
            Bool,
            Option,
            Help,
            #[default]
            Unkown,
        }

        let mut cute = Cute::new();

        cute.add(switch("-bool", TestState::Bool));
        cute.add(option("-opt", TestState::Option));
        cute.add(switch("/?", TestState::Help));
        cute.add(switch("cmd", TestState::Cmd));

        let args: Vec<String> = ["cmd", "-bool", "-opt", "value", "/?"]
            .iter()
            .map(|data| String::from(*data))
            .collect();

        assert!(cute.parse(&mut args.into_iter()).is_ok(),);
        assert!(cute.value::<bool>(TestState::Bool)?,);
        assert!(cute.value::<bool>(TestState::Help)?,);
        assert!(cute.value::<bool>(TestState::Cmd)?,);
        assert_eq!(
            cute.value::<String>(TestState::Option)?,
            String::from("value")
        );
        Ok(())
    }
}