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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use super::ArgumentIdentification;
use std::iter::Peekable;
/**
 * Structure which defines how given argument should be handled. Allows for automatic parsing and validation.
 * Mutable borrow to parsable argument definition has to be registered in ArgumentList. Because of that
 * registered arguments cannot be used while those borrows exist. Either ArgumentList instance has to be dropped
 * or there are no further usages of it. This method of defining arguments is preferred as oposed to using
 * the legacy API.
 */
pub struct ParsableValueArgument<V> {
    identification: ArgumentIdentification,
    handler: Box<
        dyn Fn(&mut Peekable<&mut std::slice::Iter<'_, String>>, &mut Vec<V>) -> Result<V, String>,
    >,
    values: Vec<V>,
}

/// Unifies how parsable arguments are parsed.
pub trait HandleableArgument<'a> {
    /// Handles argument. Gets all needed values from input iterator.
    fn handle(
        &mut self,
        input_iter: &mut Peekable<&mut std::slice::Iter<'_, String>>,
    ) -> Result<(), String>;
    /// Check if this argument is identified by specified short name.
    fn is_by_short(&self, name: char) -> bool;
    /// Check if this argument is identified by specified long name.
    fn is_by_long(&self, name: &str) -> bool;
    /// Get this arguments identification.
    fn identification(&self) -> &ArgumentIdentification;
}

impl<V> ParsableValueArgument<V> {
    pub fn new<C>(identification: ArgumentIdentification, handler: C) -> ParsableValueArgument<V>
    where
        C: Fn(&mut Peekable<&mut std::slice::Iter<'_, String>>, &mut Vec<V>) -> Result<V, String>
            + 'static,
    {
        ParsableValueArgument::<V> {
            identification,
            handler: Box::new(handler),
            values: Vec::new(),
        }
    }

    pub fn first_value(&self) -> Option<&V> {
        self.values().get(0)
    }

    pub fn values(&self) -> &Vec<V> {
        &self.values
    }
}

impl ParsableValueArgument<i64> {
    fn validate_integer(v: &str) -> Option<String> {
        let mut chars_iter = v.chars().peekable();
        if let Some(c) = chars_iter.next() {
            if (c != '-' || chars_iter.peek().is_none()) && !c.is_digit(10) {
                return Option::Some(format!("Input is not a number"));
            }
        }
        for c in chars_iter {
            if !c.is_digit(10) {
                return Option::Some(format!("Input is not a number"));
            }
        }
        Option::None
    }
    /**
     * Default integer type argument value handler. Checks whether value contains only digits or starts with minus sign.
     */
    pub fn new_integer(identification: ArgumentIdentification) -> ParsableValueArgument<i64> {
        let handler = |input_iter: &mut Peekable<&mut std::slice::Iter<'_, String>>,
                       _values: &mut Vec<i64>| {
            if let Option::Some(v) = input_iter.next() {
                let validation = ParsableValueArgument::validate_integer(v);
                if let Option::Some(err) = validation {
                    return Result::Err(err);
                }
                match v.parse() {
                    Result::Ok(v) => Result::Ok(v),
                    Result::Err(err) => Result::Err(format!("{}", err)),
                }
            } else {
                Result::Err(String::from("No remaining input values."))
            }
        };
        ParsableValueArgument::new(identification, handler)
    }
}

impl ParsableValueArgument<String> {
    /**
     * Default string type argument value handler.
     */
    pub fn new_string(identification: ArgumentIdentification) -> ParsableValueArgument<String> {
        let handler = |input_iter: &mut Peekable<&mut std::slice::Iter<'_, String>>,
                       _values: &mut Vec<String>| {
            if let Some(v) = input_iter.next() {
                Result::Ok(String::from(v))
            } else {
                Result::Err(String::from("No remaining input values."))
            }
        };
        ParsableValueArgument::new(identification, handler)
    }
}

impl<'a, V> HandleableArgument<'a> for ParsableValueArgument<V> {
    fn handle(
        &mut self,
        input_iter: &mut Peekable<&mut std::slice::Iter<'_, String>>,
    ) -> Result<(), String> {
        let result = (self.handler)(input_iter, &mut self.values)?;
        self.values.push(result);
        Result::Ok(())
    }

    fn is_by_short(&self, name: char) -> bool {
        self.identification().is_by_short(name)
    }

    fn is_by_long(&self, name: &str) -> bool {
        self.identification().is_by_long(name)
    }

    fn identification(&self) -> &ArgumentIdentification {
        &self.identification
    }
}

#[cfg(test)]
mod test {
    use std::borrow::BorrowMut;

    use super::{HandleableArgument, ParsableValueArgument};

    #[test]
    fn new_parsable_value_argument_works() {
        let _arg =
            ParsableValueArgument::<i64>::new(super::ArgumentIdentification::Short('x'), |_, _| {
                Result::Ok(2)
            });
    }

    #[test]
    fn is_by_short_works() {
        let arg =
            ParsableValueArgument::<i64>::new(super::ArgumentIdentification::Short('x'), |_, _| {
                Result::Ok(2)
            });
        assert!(arg.is_by_short('x'));
        assert!(!arg.is_by_short('c'));
    }

    #[test]
    fn is_by_long_works() {
        let arg = ParsableValueArgument::<i64>::new(
            super::ArgumentIdentification::Long(String::from("path")),
            |_, _| Result::Ok(2),
        );
        assert!(arg.is_by_long("path"));
        assert!(!arg.is_by_long("directory"));
    }

    #[test]
    fn basic_integer_argument_works() {
        let mut arg =
            ParsableValueArgument::<i64>::new_integer(super::ArgumentIdentification::Short('i'));
        assert!(arg
            .handle(&mut vec![String::from("123")].iter().borrow_mut().peekable())
            .is_ok());
        assert_eq!(arg.values.get(0).unwrap(), &123);
        assert!(arg
            .handle(&mut vec![String::from("333")].iter().borrow_mut().peekable())
            .is_ok());
        assert_eq!(2, arg.values.len());
        assert_eq!(arg.values.get(0).unwrap(), &123);
        assert_eq!(arg.values.get(1).unwrap(), &333);
        assert!(arg
            .handle(&mut vec![String::from("-333")].iter().borrow_mut().peekable())
            .is_ok());
    }

    #[test]
    fn basic_integer_argument_handler_fails_invalid_number() {
        let mut arg =
            ParsableValueArgument::<i64>::new_integer(super::ArgumentIdentification::Short('i'));
        assert!(arg
            .handle(&mut vec![String::from("-")].iter().borrow_mut().peekable())
            .is_err());
        assert!(arg
            .handle(&mut vec![String::from("12a")].iter().borrow_mut().peekable())
            .is_err());
        assert!(arg
            .handle(&mut vec![String::from("123.12")].iter().borrow_mut().peekable())
            .is_err());
    }

    #[test]
    fn first_value_works() {
        let mut arg = ParsableValueArgument::new_integer(super::ArgumentIdentification::Short('i'));
        assert!(arg.first_value().is_none());
        assert!(arg
            .handle(&mut vec![String::from("123")].iter().borrow_mut().peekable())
            .is_ok());
        assert_eq!(arg.first_value().unwrap(), &123);
    }
}