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
use crate::{CalledType, Flag};

#[derive(Clone, Debug)]
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 init(&mut self, value: Option<Vec<T>>) {
        *self = Vector(value);
    }

    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 append(&mut self, other: &mut Vec<T>) {
        match self {
            Vector(None) => {
                let mut inner = vec![];
                inner.append(other);
            }
            Vector(Some(ref mut vec)) => {
                (*vec).append(other);
            }
        }
    }

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

    pub fn 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>> {
        let Vector(inner) = self;
        inner
    }
}

impl Vector<Flag> {
    pub fn find_long_flag(&self, alias_or_name: &str) -> (CalledType, Option<&Flag>) {
        match &self {
            Vector(None) => (CalledType::None, None),
            Vector(Some(flags)) => match flags.iter().find(|flag| flag.is(alias_or_name)) {
                None => match flags.iter().find(|flag| flag.any_long(alias_or_name)) {
                    None => match flags.iter().find(|flag| flag.any_short(alias_or_name)) {
                        None => (CalledType::None, None),
                        Some(f) => (CalledType::Short, Some(f)),
                    },
                    Some(f) => (CalledType::Long, Some(f)),
                },
                Some(f) => (CalledType::Name, Some(f)),
            },
        }
    }

    pub fn find_short_flag(&self, alias_or_name: &str) -> (CalledType, Option<&Flag>) {
        match &self {
            Vector(None) => (CalledType::None, None),
            Vector(Some(flags)) => match flags.iter().find(|flag| flag.any_short(alias_or_name)) {
                None => match flags.iter().find(|flag| flag.is(alias_or_name)) {
                    None => match flags.iter().find(|flag| flag.any_long(alias_or_name)) {
                        None => (CalledType::None, None),
                        Some(f) => (CalledType::Long, Some(f)),
                    },
                    Some(f) => (CalledType::Name, Some(f)),
                },
                Some(f) => (CalledType::Short, Some(f)),
            },
        }
    }
}