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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use std::iter::FromIterator;
use crate::{ Instance, ArgumentType, Argument };
use std::ops::{ Deref, DerefMut };

/// Encapsulation of `Vec<String>`.
///
/// For example, when you defined a command or option that accepts a argument.
/// ```ignore
/// #[option(-o, --output <output_dir>)]
/// #[command(parse <dir> [dirs...])]
/// fn parse(dir: Path, dirs: Vec<Path>, cli: Cli) {
///     let output: Raw = cli.get("output");
///
///     if output.is_empty() {
///         // no output directories inputted.
///     } else {
///         // output directories inputted
///     }
/// }
/// ```
/// When you input a sequence of String, and you want to using these String.
/// But for the most time, we don't only using String. For instance, we want to using `Path`.
/// This time, we can use `Raw` to convert it to `Path` or others. Any type that implements `From<Raw>` can do it.
/// That's why types of parameters for command processing methods should have implemented `From<Raw>`
///
/// Being used for type converting.
/// Any type that implements `From<Raw>` can be used as a parameter type of command processing methods.
///
#[derive(Debug, Clone)]
pub struct Raw(Vec<String>);

impl Raw {
    #[doc(hidden)]
    #[inline]
    pub fn push(&mut self, ele: String) {
        (self.0).push(ele);
    }

    #[doc(hidden)]
    #[inline]
    pub fn remove(&mut self, idx: usize) -> String {
        (self.0).remove(idx)
    }

    #[doc(hidden)]
    #[inline]
    pub fn new(v: Vec<String>) -> Raw {
        Raw(v)
    }

    #[doc(hidden)]
    #[inline]
    pub fn is_empty(&self) -> bool {
        (self.0).len() == 0
    }

    #[doc(hidden)]
    pub fn divide_cmd(ins: &Instance, args: &Vec<Argument>) -> Vec<Raw> {
        let mut raws = vec![];
        let mut iter = ins.args.iter();

        for arg in args {
            let ty = &arg.ty;

            match ty {
                ArgumentType::RequiredSingle => {
                    let mut raw = Raw::new(vec![]);

//                    if len == 0 {
//                        error(ARG_DONT_MATCH);
//                    }
                    if let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }
                    raws.push(raw);
                },
                ArgumentType::OptionalSingle => {
                    let mut raw = Raw::new(vec![]);

                    if let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }
                    raws.push(raw);
                },
                ArgumentType::RequiredMultiple => {
                    let mut raw = Raw::new(vec![]);

//                    if len == 0 {
//                        error(ARG_DONT_MATCH);
//                    }
                    while let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }
                    raws.push(raw);
                },
                ArgumentType::OptionalMultiple => {
                    let mut raw = Raw::new(vec![]);

                    while let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }
                    raws.push(raw);
                }
            }
        }

        raws
    }

    #[doc(hidden)]
    pub fn divide_opt(ins: &Instance, arg: &Option<Argument>) -> Raw {
        if let Some(arg) = arg {
            let mut iter = ins.args.iter();

            match arg.ty {
                ArgumentType::RequiredSingle => {
                    let mut raw = Raw::new(vec![]);

//                    if len == 0 {
//                        error(ARG_DONT_MATCH);
//                    }
                    if let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }

                    raw
                },
                ArgumentType::OptionalSingle => {
                    let mut raw = Raw::new(vec![]);

                    if let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }

                    raw
                },
                ArgumentType::RequiredMultiple => {
                    let mut raw = Raw::new(vec![]);

//                    if len == 0 {
//                        error(ARG_DONT_MATCH);
//                    }

                    while let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }

                    raw
                },
                ArgumentType::OptionalMultiple => {
                    let mut raw = Raw::new(vec![]);

                    while let Some(raw_str) = iter.next() {
                        raw.push(raw_str.clone());
                    }

                    raw
                }
            }
        } else {
            Raw::new(vec![])
        }
    }
}

impl FromIterator<String> for Raw {
    fn from_iter<I: IntoIterator<Item=String>>(iter: I) -> Raw {
        let mut v = vec![];

        for item in iter.into_iter() {
            v.push(item);
        }

        Raw::new(v)
    }
}

impl Deref for Raw {
    type Target = Vec<String>;

    fn deref(&self) -> &Vec<String> {
        &(self.0)
    }
}

impl DerefMut for Raw {
    fn deref_mut(&mut self) -> &mut Vec<String> {
        &mut (self.0)
    }
}

macro_rules! impl_primitive {
    ($($ty: ty),*) => {
        $(
            impl From<Raw> for $ty {
                fn from(raw: Raw) -> $ty {
                    raw.get(0)
                        .unwrap_or(&String::from("0"))
                        .parse()
                        .unwrap_or(<$ty>::default())
                }
            }
        )*
    };
}

macro_rules! impl_option {
    ($($ty: ty),*) => {
        $(
            impl From<Raw> for Option<$ty> {
                fn from(raw: Raw) -> Option<$ty> {
                    if raw.is_empty() {
                        None
                    } else {
                        Some(<$ty>::from(raw))
                    }
                }
            }
        )*
    };
}

macro_rules! impl_vec {
    ($($ty: ty),*) => {
        $(
            impl From<Raw> for Vec<$ty> {
                fn from(raw: Raw) -> Vec<$ty> {
                    raw.iter().map(|i| i.parse().unwrap_or(<$ty>::default())).collect()
                }
            }
        )*
    };
}

macro_rules! impl_option_vec {
    ($($ty: ty),*) => {
        $(
            impl From<Raw> for Option<Vec<$ty>> {
                fn from(raw: Raw) -> Option<Vec<$ty>> {
                    if raw.is_empty() {
                        None
                    } else {
                        Some(<Vec<$ty>>::from(raw))
                    }
                }
            }
        )*
    };
}

macro_rules! impl_all {
    ($($ty: ty),*) => {
        impl_primitive![$($ty),*];
        impl_option![$($ty),*];
        impl_vec![$($ty),*];
        impl_option_vec![$($ty),*];
    };
}

impl_all![i8, i16, i32, i64, i128, isize];
impl_all![u8, u16, u32, u64, u128, usize];
impl_all![f32, f64, bool, char];


impl From<Raw> for String {
    fn from(raw: Raw) -> String {
        raw.get(0).unwrap_or(&String::new()).clone()
    }
}

impl From<Raw> for Vec<String> {
    fn from(raw: Raw) -> Vec<String> {
        raw.iter().map(|s| s.clone()).collect()
    }
}

impl From<Raw> for Option<String> {
    fn from(raw: Raw) -> Option<String> {
        if raw.is_empty() {
            None
        } else {
            Some(String::from(raw))
        }
    }
}

impl From<Raw> for Option<Vec<String>> {
    fn from(raw: Raw) -> Option<Vec<String>> {
        if raw.is_empty() {
            None
        } else {
            Some(<Vec<String>>::from(raw))
        }
    }
}