1use std::iter::FromIterator;
2use crate::{ Instance, ArgumentType, Argument };
3use std::ops::{ Deref, DerefMut };
4
5#[derive(Debug, Clone)]
30pub struct Raw(Vec<String>);
31
32impl Raw {
33    #[doc(hidden)]
34    #[inline]
35    pub fn push(&mut self, ele: String) {
36        (self.0).push(ele);
37    }
38
39    #[doc(hidden)]
40    #[inline]
41    pub fn remove(&mut self, idx: usize) -> String {
42        (self.0).remove(idx)
43    }
44
45    #[doc(hidden)]
46    #[inline]
47    pub fn new(v: Vec<String>) -> Raw {
48        Raw(v)
49    }
50
51    #[doc(hidden)]
52    #[inline]
53    pub fn is_empty(&self) -> bool {
54        (self.0).len() == 0
55    }
56
57    #[doc(hidden)]
58    pub fn divide_cmd(ins: &Instance, args: &Vec<Argument>) -> Vec<Raw> {
59        let mut raws = vec![];
60        let mut iter = ins.args.iter();
61
62        for arg in args {
63            let ty = &arg.ty;
64
65            match ty {
66                ArgumentType::RequiredSingle => {
67                    let mut raw = Raw::new(vec![]);
68
69if let Some(raw_str) = iter.next() {
73                        raw.push(raw_str.clone());
74                    }
75                    raws.push(raw);
76                },
77                ArgumentType::OptionalSingle => {
78                    let mut raw = Raw::new(vec![]);
79
80                    if let Some(raw_str) = iter.next() {
81                        raw.push(raw_str.clone());
82                    }
83                    raws.push(raw);
84                },
85                ArgumentType::RequiredMultiple => {
86                    let mut raw = Raw::new(vec![]);
87
88while let Some(raw_str) = iter.next() {
92                        raw.push(raw_str.clone());
93                    }
94                    raws.push(raw);
95                },
96                ArgumentType::OptionalMultiple => {
97                    let mut raw = Raw::new(vec![]);
98
99                    while let Some(raw_str) = iter.next() {
100                        raw.push(raw_str.clone());
101                    }
102                    raws.push(raw);
103                }
104            }
105        }
106
107        raws
108    }
109
110    #[doc(hidden)]
111    pub fn divide_opt(ins: &Instance, arg: &Option<Argument>) -> Raw {
112        if let Some(arg) = arg {
113            let mut iter = ins.args.iter();
114
115            match arg.ty {
116                ArgumentType::RequiredSingle => {
117                    let mut raw = Raw::new(vec![]);
118
119if let Some(raw_str) = iter.next() {
123                        raw.push(raw_str.clone());
124                    }
125
126                    raw
127                },
128                ArgumentType::OptionalSingle => {
129                    let mut raw = Raw::new(vec![]);
130
131                    if let Some(raw_str) = iter.next() {
132                        raw.push(raw_str.clone());
133                    }
134
135                    raw
136                },
137                ArgumentType::RequiredMultiple => {
138                    let mut raw = Raw::new(vec![]);
139
140while let Some(raw_str) = iter.next() {
145                        raw.push(raw_str.clone());
146                    }
147
148                    raw
149                },
150                ArgumentType::OptionalMultiple => {
151                    let mut raw = Raw::new(vec![]);
152
153                    while let Some(raw_str) = iter.next() {
154                        raw.push(raw_str.clone());
155                    }
156
157                    raw
158                }
159            }
160        } else {
161            Raw::new(vec![])
162        }
163    }
164}
165
166impl FromIterator<String> for Raw {
167    fn from_iter<I: IntoIterator<Item=String>>(iter: I) -> Raw {
168        let mut v = vec![];
169
170        for item in iter.into_iter() {
171            v.push(item);
172        }
173
174        Raw::new(v)
175    }
176}
177
178impl Deref for Raw {
179    type Target = Vec<String>;
180
181    fn deref(&self) -> &Vec<String> {
182        &(self.0)
183    }
184}
185
186impl DerefMut for Raw {
187    fn deref_mut(&mut self) -> &mut Vec<String> {
188        &mut (self.0)
189    }
190}
191
192macro_rules! impl_primitive {
193    ($($ty: ty),*) => {
194        $(
195            impl From<Raw> for $ty {
196                fn from(raw: Raw) -> $ty {
197                    raw.get(0)
198                        .unwrap_or(&String::from("0"))
199                        .parse()
200                        .unwrap_or(<$ty>::default())
201                }
202            }
203        )*
204    };
205}
206
207macro_rules! impl_option {
208    ($($ty: ty),*) => {
209        $(
210            impl From<Raw> for Option<$ty> {
211                fn from(raw: Raw) -> Option<$ty> {
212                    if raw.is_empty() {
213                        None
214                    } else {
215                        Some(<$ty>::from(raw))
216                    }
217                }
218            }
219        )*
220    };
221}
222
223macro_rules! impl_vec {
224    ($($ty: ty),*) => {
225        $(
226            impl From<Raw> for Vec<$ty> {
227                fn from(raw: Raw) -> Vec<$ty> {
228                    raw.iter().map(|i| i.parse().unwrap_or(<$ty>::default())).collect()
229                }
230            }
231        )*
232    };
233}
234
235macro_rules! impl_option_vec {
236    ($($ty: ty),*) => {
237        $(
238            impl From<Raw> for Option<Vec<$ty>> {
239                fn from(raw: Raw) -> Option<Vec<$ty>> {
240                    if raw.is_empty() {
241                        None
242                    } else {
243                        Some(<Vec<$ty>>::from(raw))
244                    }
245                }
246            }
247        )*
248    };
249}
250
251macro_rules! impl_all {
252    ($($ty: ty),*) => {
253        impl_primitive![$($ty),*];
254        impl_option![$($ty),*];
255        impl_vec![$($ty),*];
256        impl_option_vec![$($ty),*];
257    };
258}
259
260impl_all![i8, i16, i32, i64, i128, isize];
261impl_all![u8, u16, u32, u64, u128, usize];
262impl_all![f32, f64, bool, char];
263
264
265impl From<Raw> for String {
266    fn from(raw: Raw) -> String {
267        raw.get(0).unwrap_or(&String::new()).clone()
268    }
269}
270
271impl From<Raw> for Vec<String> {
272    fn from(raw: Raw) -> Vec<String> {
273        raw.iter().map(|s| s.clone()).collect()
274    }
275}
276
277impl From<Raw> for Option<String> {
278    fn from(raw: Raw) -> Option<String> {
279        if raw.is_empty() {
280            None
281        } else {
282            Some(String::from(raw))
283        }
284    }
285}
286
287impl From<Raw> for Option<Vec<String>> {
288    fn from(raw: Raw) -> Option<Vec<String>> {
289        if raw.is_empty() {
290            None
291        } else {
292            Some(<Vec<String>>::from(raw))
293        }
294    }
295}