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
use std::fmt::Formatter;

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone)]
pub enum Key {
    Dual {
        char: char,
        s_txt: char,
        l_txt: String,
    },

    Long {
        char: char,
        txt: String,
    },

    Short {
        char: char,
        txt: char,
    },

    Sub {
        txt: String,
    },
}

impl Key {
    pub fn char(&self) -> char {
        match self {
            Key::Dual { char: c, .. } => c.clone(),
            Key::Long { char: c, .. } => c.clone(),
            Key::Short { char: c, .. } => c.clone(),
            Key::Sub { .. } => '\0',
        }
    }

    pub fn text(&self) -> String {
        match self {
            Key::Dual { l_txt: txt, .. } => txt.clone(),
            Key::Long { txt, .. } => txt.clone(),
            Key::Short { txt, .. } => txt.to_string(),
            Key::Sub { txt, .. } => txt.clone(),
        }
    }

    pub fn is_dual(&self) -> bool {
        match self {
            Key::Dual { .. } => true,
            Key::Long { .. } => false,
            Key::Short { .. } => false,
            Key::Sub { .. } => false,
        }
    }

    pub fn is_long(&self) -> bool {
        match self {
            Key::Dual { .. } => false,
            Key::Long { .. } => true,
            Key::Short { .. } => false,
            Key::Sub { .. } => false,
        }
    }

    pub fn is_short(&self) -> bool {
        match self {
            Key::Dual { .. } => false,
            Key::Long { .. } => false,
            Key::Short { .. } => true,
            Key::Sub { .. } => false,
        }
    }

    pub fn is_sub(&self) -> bool {
        match self {
            Key::Dual { .. } => false,
            Key::Long { .. } => false,
            Key::Short { .. } => false,
            Key::Sub { .. } => true,
        }
    }
}

impl From<String> for Key {
    fn from(s: String) -> Self {
        let s: String = s.to_string();
        let chars: Vec<char> = s.chars().collect();

        if !chars[0].is_alphabetic() {
            if s.len() == 2 {
                let char: char = chars[0].clone();
                let txt: char = chars[1].clone();

                Self::Short { char, txt }
            } else {
                let char: char = chars[0].clone();
                let mut txt: String = String::new();

                chars
                    .iter()
                    .skip_while(|x| **x == chars[0])
                    .for_each(|x| txt = format!("{}{}", txt, x));

                Self::Long { char, txt }
            }
        } else {
            Self::Sub { txt: s }
        }
    }
}

impl From<&str> for Key {
    fn from(s: &str) -> Self {
        s.to_string().into()
    }
}

impl<T: Clone + Into<Key>> From<[T; 2]> for Key {
    fn from(dk: [T; 2]) -> Self {
        let one: Key = dk[0].clone().into();
        let two: Key = dk[1].clone().into();

        if one.is_sub() || two.is_sub() {
            panic!("dual cannot contain a subcommand!");
        }

        if one.is_long() && two.is_short() {
            Key::Dual {
                char: one.char(),
                s_txt: two.text().parse().unwrap(),
                l_txt: one.text(),
            }
        } else {
            Key::Dual {
                char: two.char(),
                s_txt: one.text().parse().unwrap(),
                l_txt: two.text(),
            }
        }
    }
}

impl std::fmt::Display for Key {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Key::Dual {
                    char: c,
                    s_txt: s,
                    l_txt: l,
                } => format!("{}{}, {}{}{}", c, s, c, c, l),
                Key::Long { char: c, txt: t } => format!("{}{}{}", c, c, t),
                Key::Short { char: c, txt: t } => format!("{}{}", c, t),
                Key::Sub { txt: t } => format!("{}", t),
            }
        )
    }
}