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
use crate::{build::Arg, util::Id, INTERNAL_ERROR_MSG};
use std::{
ffi::{OsStr, OsString},
ops::Index,
};
#[derive(PartialEq, Debug, Clone)]
pub(crate) struct Key {
pub(crate) key: KeyType,
pub(crate) index: usize,
}
#[derive(Default, PartialEq, Debug, Clone)]
pub(crate) struct MKeyMap<'help> {
pub(crate) keys: Vec<Key>,
pub(crate) args: Vec<Arg<'help>>,
built: bool,
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub(crate) enum KeyType {
Short(char),
Long(OsString),
Position(u64),
}
impl KeyType {
pub(crate) fn is_position(&self) -> bool {
match *self {
KeyType::Position(_) => true,
_ => false,
}
}
}
impl PartialEq<&str> for KeyType {
fn eq(&self, rhs: &&str) -> bool {
match self {
KeyType::Long(ref l) => l == OsStr::new(rhs),
_ => false,
}
}
}
impl PartialEq<char> for KeyType {
fn eq(&self, rhs: &char) -> bool {
match self {
KeyType::Short(c) => c == rhs,
_ => false,
}
}
}
impl<'help> MKeyMap<'help> {
pub(crate) fn contains<K>(&self, key: K) -> bool
where
KeyType: PartialEq<K>,
{
self.keys.iter().any(|x| x.key == key)
}
pub(crate) fn push(&mut self, value: Arg<'help>) -> usize {
if self.built {
panic!("Cannot add Args to the map after the map is built");
}
let index = self.args.len();
self.args.push(value);
index
}
pub(crate) fn get(&self, key: &KeyType) -> Option<&Arg<'help>> {
self.keys
.iter()
.find(|k| k.key == *key)
.map(|k| &self.args[k.index])
}
pub(crate) fn is_empty(&self) -> bool {
self.keys.is_empty() && self.args.is_empty()
}
pub(crate) fn _build(&mut self) {
self.built = true;
for (i, arg) in self.args.iter_mut().enumerate() {
for k in _get_keys(arg) {
self.keys.push(Key { key: k, index: i });
}
}
}
pub(crate) fn remove_by_name(&mut self, name: &Id) -> Option<Arg<'help>> {
if self.built {
panic!("Cannot remove args after being built");
}
self.args
.iter()
.position(|arg| arg.id == *name)
.map(|i| self.args.swap_remove(i))
}
}
impl<'help> Index<&'_ KeyType> for MKeyMap<'help> {
type Output = Arg<'help>;
fn index(&self, key: &KeyType) -> &Self::Output {
self.get(key).expect(INTERNAL_ERROR_MSG)
}
}
fn _get_keys(arg: &Arg) -> Vec<KeyType> {
if let Some(index) = arg.index {
return vec![KeyType::Position(index)];
}
let mut keys = vec![];
for short in arg.short_aliases.iter().map(|(c, _)| KeyType::Short(*c)) {
keys.push(short);
}
if let Some(c) = arg.short {
keys.push(KeyType::Short(c));
}
for long in arg
.aliases
.iter()
.map(|(a, _)| KeyType::Long(OsString::from(a)))
{
keys.push(long);
}
if let Some(long) = arg.long {
keys.push(KeyType::Long(OsString::from(long)));
}
keys
}