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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#[derive(Clone, Debug, PartialOrd, PartialEq)]
pub struct Vector<T>(pub Option<Vec<T>>);

impl<T> Default for Vector<T> {
	fn default() -> Self {
		Vector(None)
	}
}

impl<T> From<Vec<T>> for Vector<T> {
	fn from(vec: Vec<T>) -> Self {
		Vector(Some(vec))
	}
}

impl<T> From<Option<Vec<T>>> for Vector<T> {
	fn from(val: Option<Vec<T>>) -> Self {
		Vector(val)
	}
}

impl<T: Clone> From<&Vec<T>> for Vector<T> {
	fn from(vec: &Vec<T>) -> Self {
		Vector(Some(vec.to_vec()))
	}
}

impl<T> Vector<T> {
	pub fn new() -> Self {
		Vector::default()
	}

	pub fn with_inner(inner_vec: Vec<T>) -> Self {
		Vector(Some(inner_vec))
	}

	pub fn init(&mut self, value: Option<Vec<T>>) {
		*self = Vector(value);
	}

	pub fn with_first_elem(elem: T) -> Self {
		Vector(Some(vec![elem]))
	}

	pub fn push(&mut self, push: T) {
		match self {
			Vector(None) => {
				*self = Vector(Some(vec![push]));
			}
			Vector(Some(ref mut v)) => (*v).push(push),
		}
	}

	pub fn len(&self) -> usize {
		match self {
			Vector(None) => 0,
			Vector(Some(inner)) => inner.len(),
		}
	}

	pub fn append_vec(&mut self, mut other: Vec<T>) {
		match self {
			Vector(None) => {
				//let mut inner = vec![];

				//inner.append(other);

				// self = Vector(Some(inner));

				*self = Vector(Some(other))
			}
			Vector(Some(ref mut vec)) => (*vec).append(&mut other),
		}
	}

	pub fn insert(&mut self, index: usize, insert: T) {
		match self {
			Vector(None) => *self = Vector(Some(vec![insert])),
			Vector(Some(ref mut vec)) => (*vec).insert(index, insert),
		}
	}

	pub fn append(&mut self, mut other: Vector<T>) {
		match self {
			Vector(None) => {
				*self = other;
			}
			Vector(Some(ref mut vec)) => {
				if let Vector(Some(ref mut o_vec)) = other {
					//

					(*vec).append(o_vec);
				}
			}
		}
	}

	pub fn is_none(&self) -> bool {
		match self {
			Vector(None) => true,
			_ => false,
		}
	}

	pub fn has_inner_vec(&self) -> bool {
		match self {
			Vector(None) => false,
			_ => true,
		}
	}

	pub fn set_none(&mut self) {
		(*self) = Vector(None);
	}

	pub fn clear(&mut self) {
		match self {
			Vector(None) => {}
			Vector(Some(ref mut inner)) => (*inner).clear(),
		}
	}

	pub fn inner(&self) -> Option<&Vec<T>> {
		match &self {
			Vector(None) => None,
			Vector(Some(inner)) => Some(inner),
		}
	}

	pub fn inner_mut(&mut self) -> &mut Option<Vec<T>> {
		let Vector(inner) = self;
		inner
	}

	pub fn take(&mut self) -> Vector<T> {
		let Vector(inner) = self;
		Vector(inner.take())
	}

	pub fn inner_ref(self) -> Option<Vec<T>> {
		match self {
			Vector(None) => None,
			Vector(inner) => inner,
		}
	}

	pub fn get(&self, index: usize) -> Option<&T> {
		match self {
			Vector(None) => None,
			Vector(Some(inner)) => inner.get(index),
		}
	}
}

impl<T> From<Vector<T>> for Vector<Vector<T>> {
	fn from(f: Vector<T>) -> Vector<Vector<T>> {
		Vector(Some(vec![f]))
	}
}

impl From<String> for Vector<String> {
	fn from(f: String) -> Vector<String> {
		Vector::with_first_elem(f)
	}
}

impl From<&str> for Vector<String> {
	fn from(str: &str) -> Self {
		Vector::with_first_elem(str.into())
	}
}

impl From<Option<String>> for Vector<String> {
	fn from(t: Option<String>) -> Self {
		match t {
			None => Vector(None),
			Some(t) => Vector::with_first_elem(t),
		}
	}
}

impl<T> Vector<Vector<T>> {
	pub fn sum_of_length(&self) -> usize {
		match self {
			Vector(None) => 0,
			Vector(Some(list)) => list.iter().fold(0, |acc, item| acc + item.len()),
		}
	}

	pub fn length_of_last(&self) -> usize {
		match self {
			Vector(None) => 0,
			Vector(Some(list)) => {
				if let Some(last) = list.last() {
					last.len()
				} else {
					0
				}
			}
		}
	}
}

pub mod flag {
	use super::Vector;
	use crate::Flag;

	#[derive(Debug, Clone)]
	pub enum LongFound<T> {
		Name(T),
		Long(T),
		None,
	}

	pub trait FlagSearch {
		fn find_long_flag(&self, name_or_alias: &str) -> LongFound<&Flag>;
		fn find_short_flag(&self, name_or_alias: &char) -> Option<&Flag>;
		fn find(&self, name_or_alias: &str) -> Option<&Flag>;
	}

	impl FlagSearch for Vector<Flag> {
		fn find_long_flag(&self, name_or_alias: &str) -> LongFound<&Flag> {
			match &self {
				Vector(None) => LongFound::None,
				Vector(Some(flags)) => match flags.iter().find(|flag| flag.is(name_or_alias)) {
					None => match flags.iter().find(|flag| flag.is_long(name_or_alias)) {
						None => LongFound::None,
						Some(f) => LongFound::Long(f),
					},
					Some(f) => LongFound::Name(f),
				},
			}
		}

		fn find_short_flag(&self, short_alias: &char) -> Option<&Flag> {
			match &self {
				Vector(Some(flags)) => flags.iter().find(|flag| flag.is_short(short_alias)),
				Vector(None) => None,
			}
		}

		fn find(&self, flag_name: &str) -> Option<&Flag> {
			match &self {
				Vector(Some(flags)) => match flags.iter().find(|flag| flag.is(flag_name)) {
					Some(f) => Some(f),
					None => None,
				},
				Vector(None) => None,
			}
		}
	}

	impl<T: FlagSearch> FlagSearch for Vector<T> {
		fn find_long_flag(&self, name_or_alias: &str) -> LongFound<&Flag> {
			match &self {
				Vector(None) => LongFound::None,
				Vector(Some(flags_list)) => {
					let mut iter = flags_list.iter();
					return loop {
						let flags = iter.next_back();
						if let Some(flags) = flags {
							match flags.find_long_flag(name_or_alias) {
								LongFound::None => {}
								val => {
									break val;
								}
							}
						} else {
							break LongFound::None;
						}
					};
				}
			}
		}

		fn find_short_flag(&self, name_or_alias: &char) -> Option<&Flag> {
			match &self {
				Vector(None) => None,
				Vector(Some(flags_list)) => {
					let mut iter = flags_list.iter();
					return loop {
						let flags = iter.next_back();
						if let Some(flags) = flags {
							match flags.find_short_flag(name_or_alias) {
								None => {}
								val => {
									break val;
								}
							}
						} else {
							break None;
						}
					};
				}
			}
		}

		fn find(&self, name_or_alias: &str) -> Option<&Flag> {
			match &self {
				Vector(None) => None,
				Vector(Some(flags_list)) => {
					let mut iter = flags_list.iter();
					return loop {
						let flags = iter.next_back();
						if let Some(flags) = flags {
							match flags.find(name_or_alias) {
								None => {}
								val => {
									break val;
								}
							}
						} else {
							break None;
						}
					};
				}
			}
		}
	}
}