1use std::iter::Iterator;
2use std::ops::Index;
3
4use crate::Arg;
5use crate::INTERNAL_ERROR_MSG;
6use crate::builder::OsStr;
7
8#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for Key {
#[inline]
fn eq(&self, other: &Key) -> bool {
self.key == other.key && self.index == other.index
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Key {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<KeyType>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for Key {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Key", "key",
&self.key, "index", &&self.index)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for Key {
#[inline]
fn clone(&self) -> Key {
Key {
key: ::core::clone::Clone::clone(&self.key),
index: ::core::clone::Clone::clone(&self.index),
}
}
}Clone)]
9pub(crate) struct Key {
10 key: KeyType,
11 index: usize,
12}
13
14#[derive(#[automatically_derived]
impl ::core::default::Default for MKeyMap {
#[inline]
fn default() -> MKeyMap {
MKeyMap {
args: ::core::default::Default::default(),
keys: ::core::default::Default::default(),
}
}
}Default, #[automatically_derived]
impl ::core::cmp::PartialEq for MKeyMap {
#[inline]
fn eq(&self, other: &MKeyMap) -> bool {
self.args == other.args && self.keys == other.keys
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for MKeyMap {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Vec<Arg>>;
let _: ::core::cmp::AssertParamIsEq<Vec<Key>>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for MKeyMap {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "MKeyMap",
"args", &self.args, "keys", &&self.keys)
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for MKeyMap {
#[inline]
fn clone(&self) -> MKeyMap {
MKeyMap {
args: ::core::clone::Clone::clone(&self.args),
keys: ::core::clone::Clone::clone(&self.keys),
}
}
}Clone)]
15pub(crate) struct MKeyMap {
16 args: Vec<Arg>,
18
19 keys: Vec<Key>,
22}
23
24#[derive(#[automatically_derived]
impl ::core::fmt::Debug for KeyType {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
KeyType::Short(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Short",
&__self_0),
KeyType::Long(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Long",
&__self_0),
KeyType::Position(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"Position", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for KeyType {
#[inline]
fn eq(&self, other: &KeyType) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(KeyType::Short(__self_0), KeyType::Short(__arg1_0)) =>
__self_0 == __arg1_0,
(KeyType::Long(__self_0), KeyType::Long(__arg1_0)) =>
__self_0 == __arg1_0,
(KeyType::Position(__self_0), KeyType::Position(__arg1_0)) =>
__self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for KeyType {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<char>;
let _: ::core::cmp::AssertParamIsEq<OsStr>;
let _: ::core::cmp::AssertParamIsEq<usize>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for KeyType {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
KeyType::Short(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
KeyType::Long(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
KeyType::Position(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
}
}
}Hash, #[automatically_derived]
impl ::core::clone::Clone for KeyType {
#[inline]
fn clone(&self) -> KeyType {
match self {
KeyType::Short(__self_0) =>
KeyType::Short(::core::clone::Clone::clone(__self_0)),
KeyType::Long(__self_0) =>
KeyType::Long(::core::clone::Clone::clone(__self_0)),
KeyType::Position(__self_0) =>
KeyType::Position(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
25pub(crate) enum KeyType {
26 Short(char),
27 Long(OsStr),
28 Position(usize),
29}
30
31impl KeyType {
32 pub(crate) fn is_position(&self) -> bool {
33 #[allow(non_exhaustive_omitted_patterns)] match self {
KeyType::Position(_) => true,
_ => false,
}matches!(self, KeyType::Position(_))
34 }
35}
36
37impl PartialEq<usize> for KeyType {
38 fn eq(&self, rhs: &usize) -> bool {
39 match self {
40 KeyType::Position(x) => x == rhs,
41 _ => false,
42 }
43 }
44}
45
46impl PartialEq<&str> for KeyType {
47 fn eq(&self, rhs: &&str) -> bool {
48 match self {
49 KeyType::Long(l) => l == rhs,
50 _ => false,
51 }
52 }
53}
54
55impl PartialEq<str> for KeyType {
56 fn eq(&self, rhs: &str) -> bool {
57 match self {
58 KeyType::Long(l) => l == rhs,
59 _ => false,
60 }
61 }
62}
63
64impl PartialEq<OsStr> for KeyType {
65 fn eq(&self, rhs: &OsStr) -> bool {
66 match self {
67 KeyType::Long(l) => l == rhs,
68 _ => false,
69 }
70 }
71}
72
73impl PartialEq<char> for KeyType {
74 fn eq(&self, rhs: &char) -> bool {
75 match self {
76 KeyType::Short(c) => c == rhs,
77 _ => false,
78 }
79 }
80}
81
82impl MKeyMap {
83 pub(crate) fn contains<K>(&self, key: K) -> bool
87 where
88 KeyType: PartialEq<K>,
89 {
90 self.keys.iter().any(|x| x.key == key)
91 }
92
93 pub(crate) fn push(&mut self, new_arg: Arg) {
95 self.args.push(new_arg);
96 }
97
98 pub(crate) fn get<K: ?Sized>(&self, key: &K) -> Option<&Arg>
102 where
103 KeyType: PartialEq<K>,
104 {
105 self.keys
106 .iter()
107 .find(|k| &k.key == key)
108 .map(|k| &self.args[k.index])
109 }
110
111 pub(crate) fn keys(&self) -> impl Iterator<Item = &KeyType> {
113 self.keys.iter().map(|x| &x.key)
114 }
115
116 pub(crate) fn args(&self) -> impl Iterator<Item = &Arg> {
118 self.args.iter()
119 }
120
121 pub(crate) fn args_mut(&mut self) -> impl Iterator<Item = &mut Arg> {
123 self.args.iter_mut()
124 }
125
126 pub(crate) fn mut_args<F>(&mut self, f: F)
128 where
129 F: FnMut(Arg) -> Arg,
130 {
131 let mut args = std::mem::take(&mut self.args);
132 self.args.extend(args.drain(..).map(f));
133 }
134
135 pub(crate) fn _build(&mut self) {
138 self.keys.reserve(self.args.len());
140 for (i, arg) in self.args.iter().enumerate() {
141 append_keys(&mut self.keys, arg, i);
142 }
143 }
144
145 pub(crate) fn remove_by_name(&mut self, name: &str) -> Option<Arg> {
148 self.args
149 .iter()
150 .position(|arg| arg.id == name)
151 .map(|i| self.args.remove(i))
153 }
154}
155
156impl Index<&'_ KeyType> for MKeyMap {
157 type Output = Arg;
158
159 fn index(&self, key: &KeyType) -> &Self::Output {
160 self.get(key).expect(INTERNAL_ERROR_MSG)
161 }
162}
163
164fn append_keys(keys: &mut Vec<Key>, arg: &Arg, index: usize) {
166 if let Some(pos_index) = arg.index {
167 let key = KeyType::Position(pos_index);
168 keys.push(Key { key, index });
169 } else {
170 if let Some(short) = arg.short {
171 let key = KeyType::Short(short);
172 keys.push(Key { key, index });
173 }
174 if let Some(long) = arg.long.clone() {
175 let key = KeyType::Long(long.into());
176 keys.push(Key { key, index });
177 }
178
179 for (short, _) in arg.short_aliases.iter() {
180 let key = KeyType::Short(*short);
181 keys.push(Key { key, index });
182 }
183 for (long, _) in arg.aliases.iter() {
184 let key = KeyType::Long(long.into());
185 keys.push(Key { key, index });
186 }
187 }
188}