1
0
Fork 0

fix(keymaps): Apply `clippy` suggestions

This commit is contained in:
Benedikt Peetz 2023-11-07 20:00:20 +01:00
parent 831831cd1c
commit 8d3a421e4c
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
3 changed files with 30 additions and 21 deletions

View File

@ -70,7 +70,7 @@ impl FromStr for KeyValue {
f_str f_str
// FIXME(@soispha): This should check the full string, as something // FIXME(@soispha): This should check the full string, as something
// like 'FA12' would also match this case <2023-11-07> // like 'FA12' would also match this case <2023-11-07>
if f_str.starts_with("F") if f_str.starts_with('F')
&& f_str.ends_with(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) => && f_str.ends_with(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) =>
{ {
Ok(Self::F(f_str.trim_matches('F').parse()?)) Ok(Self::F(f_str.trim_matches('F').parse()?))
@ -79,14 +79,14 @@ impl FromStr for KeyValue {
"DASH" => Ok(Self::Char('-')), "DASH" => Ok(Self::Char('-')),
"ANGULAR_BRACKET_OPEN" | "ABO" => Ok(Self::Char('<')), "ANGULAR_BRACKET_OPEN" | "ABO" => Ok(Self::Char('<')),
"ANGULAR_BRACKET_CLOSE" | "ABC" => Ok(Self::Char('>')), "ANGULAR_BRACKET_CLOSE" | "ABC" => Ok(Self::Char('>')),
other_str => return Err(Self::Err::NoMatch(other_str.to_owned())), other_str => Err(Self::Err::NoMatch(other_str.to_owned())),
} }
} }
} }
impl Display for KeyValue { impl Display for KeyValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut w = |str| return f.write_str(str); let mut w = |str| f.write_str(str);
match self { match self {
KeyValue::Backspace => w("<BACKSPACE>"), KeyValue::Backspace => w("<BACKSPACE>"),
KeyValue::Enter => w("<ENTER>"), KeyValue::Enter => w("<ENTER>"),

View File

@ -48,12 +48,11 @@ impl Iterator for KeysIter {
type Item = Key; type Item = Key;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let output; let output = if self.keys.0.len() <= self.index {
if self.keys.0.len() <= self.index { None
output = None;
} else { } else {
output = Some(self.keys.0[self.index]); Some(self.keys.0[self.index])
} };
self.index += 1; self.index += 1;
output output
} }
@ -75,8 +74,7 @@ impl<'a> Iterator for KeysIterB<'a> {
type Item = &'a Key; type Item = &'a Key;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let output; let output = self.keys.0.get(self.index);
output = self.keys.0.get(self.index);
self.index += 1; self.index += 1;
output output
} }

View File

@ -14,6 +14,12 @@ pub struct Node<V: std::fmt::Display + ToOwned<Owned = V>> {
is_child: bool, is_child: bool,
} }
impl<V: std::fmt::Display + ToOwned<Owned = V>> Default for Node<V> {
fn default() -> Self {
Self::new()
}
}
impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> { impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
pub fn new() -> Node<V> { pub fn new() -> Node<V> {
Node { Node {
@ -44,7 +50,7 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
/// empty vector /// empty vector
/// The boolean denotes if the returned node is a true end or just a waypoint. It should be /// The boolean denotes if the returned node is a true end or just a waypoint. It should be
/// called, when the bool is true /// called, when the bool is true
pub fn get(&self, keys: &Keys) -> Option<(Vec<&Box<Node<V>>>, bool)> { pub fn get(&self, keys: &Keys) -> Option<(Vec<&Node<V>>, bool)> {
let mut current_node = self; let mut current_node = self;
let mut old_node = None; let mut old_node = None;
for char in keys { for char in keys {
@ -64,7 +70,7 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
// r->a->a->c: Some([c]) -> c.is_child() && c.is_terminal() ? *call c* : key input pending // r->a->a->c: Some([c]) -> c.is_child() && c.is_terminal() ? *call c* : key input pending
// //
// r->a: Some([a, b]) -> key input pending // r->a: Some([a, b]) -> key input pending
if let Some(node) = current_node.children.get(&char) { if let Some(node) = current_node.children.get(char) {
old_node = Some((current_node, char)); old_node = Some((current_node, char));
current_node = node; current_node = node;
} else { } else {
@ -77,12 +83,15 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
Some(( Some((
vec![on vec![on
.children .children
.get(&char) .get(char)
.expect("This should be some, as this was checked above")], .expect("This should be some, as this was checked above")],
true, true,
)) ))
} else { } else {
Some((current_node.children.values().collect(), false)) Some((
current_node.children.values().map(|a| a.as_ref()).collect(),
false,
))
} }
} }
@ -110,7 +119,7 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
.expect("This should be set") .expect("This should be set")
.to_owned(), .to_owned(),
}); });
} else if current_node.children.len() > 0 { } else if !current_node.children.is_empty() {
return Err(error::TrieInsertError::NodeHasChildren(keys.to_owned())); return Err(error::TrieInsertError::NodeHasChildren(keys.to_owned()));
} else { } else {
current_node.value = Some(value); current_node.value = Some(value);
@ -133,8 +142,7 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
let out: Vec<_> = self let out: Vec<_> = self
.children .children
.values() .values()
.map(|node| node.collect_values_all()) .flat_map(|node| node.collect_values_all())
.flatten()
.collect(); .collect();
out out
} }
@ -143,7 +151,7 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::key::{Key, Keys}; use crate::key_repr::{Key, Keys};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use super::Node; use super::Node;
@ -155,7 +163,7 @@ mod test {
str.parse::<Key>().unwrap() str.parse::<Key>().unwrap()
} }
fn collect<V: std::fmt::Display + ToOwned<Owned = V>>( fn collect<V: std::fmt::Display + ToOwned<Owned = V>>(
nodes: Option<(Vec<&Box<Node<V>>>, bool)>, nodes: Option<(Vec<&Node<V>>, bool)>,
) -> Vec<&V> { ) -> Vec<&V> {
let (nodes, _should_call) = nodes.unwrap(); let (nodes, _should_call) = nodes.unwrap();
nodes nodes
@ -208,8 +216,8 @@ mod test {
let mut output: Node<bool> = Node::new(); let mut output: Node<bool> = Node::new();
trie.insert(&i("aa"), true).unwrap(); trie.insert(&i("aa"), true).unwrap();
trie.insert(&i("abd"), true).unwrap();
trie.insert(&i("abe"), true).unwrap(); trie.insert(&i("abe"), true).unwrap();
trie.insert(&i("abd"), true).unwrap();
output.insert(&i("abd"), true).unwrap(); output.insert(&i("abd"), true).unwrap();
output.insert(&i("abe"), true).unwrap(); output.insert(&i("abe"), true).unwrap();
@ -218,7 +226,10 @@ mod test {
output.insert(&i("ace"), true).unwrap(); output.insert(&i("ace"), true).unwrap();
let a_children = &output.children.get(&k("a")).unwrap(); let a_children = &output.children.get(&k("a")).unwrap();
let output_nodes = vec![&a_children.children[&k("b")], &a_children.children[&k("a")]]; let output_nodes = vec![
a_children.children[&k("a")].as_ref(),
a_children.children[&k("b")].as_ref(),
];
let (nodes, _should_call) = trie.get(&i("a")).unwrap(); let (nodes, _should_call) = trie.get(&i("a")).unwrap();
assert_eq!(nodes, output_nodes); assert_eq!(nodes, output_nodes);