Refactor mkeymap (#1618)

* refactor(mkeymap): Use iterators

* refactor(mkeymap): Implement contains method

* refactor(mkeymap): Deprecate methods

Co-authored-by: Dylan DPC <dylan.dpc@gmail.com>
This commit is contained in:
Jeremy Stucki 2020-01-09 18:30:22 +01:00 committed by Dylan DPC
parent 8b5ecf92e4
commit 2e1acb648c
2 changed files with 32 additions and 33 deletions

View file

@ -1769,7 +1769,8 @@ impl<'b> App<'b> {
if !self.is_set(AppSettings::Built) { if !self.is_set(AppSettings::Built) {
panic!("If App::_build hasn't been called, manually search through Arg shorts"); panic!("If App::_build hasn't been called, manually search through Arg shorts");
} }
self.args.contains_short(s)
self.args.contains(s)
} }
pub fn is_set(&self, s: AppSettings) -> bool { pub fn is_set(&self, s: AppSettings) -> bool {

View file

@ -55,9 +55,19 @@ impl<'b> MKeyMap<'b> {
//TODO ::from(x), ::with_capacity(n) etc //TODO ::from(x), ::with_capacity(n) etc
//? set theory ops? //? set theory ops?
pub fn contains_long(&self, l: &str) -> bool { self.keys.iter().any(|x| x.key == l) } #[deprecated(since="3.0.0", note="Use `contains` instead")]
pub fn contains_long(&self, l: &str) -> bool {
self.contains(l)
}
pub fn contains_short(&self, c: char) -> bool { self.keys.iter().any(|x| x.key == c) } #[deprecated(since="3.0.0", note="Use `contains` instead")]
pub fn contains_short(&self, c: char) -> bool {
self.contains(c)
}
pub fn contains<K>(&self, key: K) -> bool where KeyType: PartialEq<K> {
self.keys.iter().any(|x| x.key == key)
}
pub fn insert(&mut self, key: KeyType, value: Arg<'b>) -> usize { pub fn insert(&mut self, key: KeyType, value: Arg<'b>) -> usize {
let index = self.push(value); let index = self.push(value);
@ -89,12 +99,10 @@ impl<'b> MKeyMap<'b> {
// ! Arg mutation functionality // ! Arg mutation functionality
pub fn get(&self, key: &KeyType) -> Option<&Arg<'b>> { pub fn get(&self, key: &KeyType) -> Option<&Arg<'b>> {
for k in &self.keys { self.keys
if &k.key == key { .iter()
return Some(&self.args[k.index]); .find(|k| k.key == *key)
} .map(|k| &self.args[k.index])
}
None
} }
//TODO ::get_first([KeyA, KeyB]) //TODO ::get_first([KeyA, KeyB])
@ -108,14 +116,13 @@ impl<'b> MKeyMap<'b> {
} }
} }
pub fn is_empty(&self) -> bool { self.keys.is_empty() && self.args.is_empty() } pub fn is_empty(&self) -> bool { self.keys.is_empty() && self.args.is_empty() }
pub fn remove_key(&mut self, key: &KeyType) { pub fn remove_key(&mut self, key: &KeyType) {
let idx = self.keys.iter().enumerate().find(|(_,k)| k.key == *key).and_then(|(i, _)| Some(i)); self.keys
if let Some(id) = idx { .iter()
self.keys.swap_remove(id); .position(|k| k.key == *key)
} .map(|i| self.keys.swap_remove(i));
} }
pub fn insert_key_by_name(&mut self, key: KeyType, name: &str) { pub fn insert_key_by_name(&mut self, key: KeyType, name: &str) {
@ -175,13 +182,11 @@ impl<'b> MKeyMap<'b> {
if self.built { if self.built {
panic!("Cannot remove args after being built"); panic!("Cannot remove args after being built");
} }
let mut idx = None;
for k in self.keys.iter() { let idx = self.keys
if &k.key == key { .iter()
idx = Some(k.index); .position(|k| k.key == *key);
break;
}
}
if let Some(idx) = idx { if let Some(idx) = idx {
let arg = self.args.swap_remove(idx); let arg = self.args.swap_remove(idx);
for key in _get_keys(&arg) { for key in _get_keys(&arg) {
@ -200,18 +205,11 @@ impl<'b> MKeyMap<'b> {
if self.built { if self.built {
panic!("Cannot remove args after being built"); panic!("Cannot remove args after being built");
} }
let mut index = None;
for (i, arg) in self.args.iter().enumerate() { self.args
if arg.id == _name { .iter()
index = Some(i); .position(|arg| arg.id == _name)
break; .map(|i| self.args.swap_remove(i))
}
}
if let Some(i) = index {
Some(self.args.swap_remove(i))
} else {
None
}
} }
} }