From 8d3a421e4c8063db0be6d6f712de2e8e09992476 Mon Sep 17 00:00:00 2001 From: Soispha Date: Tue, 7 Nov 2023 20:00:20 +0100 Subject: [PATCH] fix(keymaps): Apply `clippy` suggestions --- keymaps/src/key_repr/key_value/mod.rs | 6 ++--- keymaps/src/key_repr/keys/mod.rs | 12 ++++------ keymaps/src/trie.rs | 33 ++++++++++++++++++--------- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/keymaps/src/key_repr/key_value/mod.rs b/keymaps/src/key_repr/key_value/mod.rs index cd3b57e..ea6b233 100644 --- a/keymaps/src/key_repr/key_value/mod.rs +++ b/keymaps/src/key_repr/key_value/mod.rs @@ -70,7 +70,7 @@ impl FromStr for KeyValue { f_str // FIXME(@soispha): This should check the full string, as something // 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']) => { Ok(Self::F(f_str.trim_matches('F').parse()?)) @@ -79,14 +79,14 @@ impl FromStr for KeyValue { "DASH" => Ok(Self::Char('-')), "ANGULAR_BRACKET_OPEN" | "ABO" => 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 { 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 { KeyValue::Backspace => w(""), KeyValue::Enter => w(""), diff --git a/keymaps/src/key_repr/keys/mod.rs b/keymaps/src/key_repr/keys/mod.rs index d69039a..fc645b7 100644 --- a/keymaps/src/key_repr/keys/mod.rs +++ b/keymaps/src/key_repr/keys/mod.rs @@ -48,12 +48,11 @@ impl Iterator for KeysIter { type Item = Key; fn next(&mut self) -> Option { - let output; - if self.keys.0.len() <= self.index { - output = None; + let output = if self.keys.0.len() <= self.index { + None } else { - output = Some(self.keys.0[self.index]); - } + Some(self.keys.0[self.index]) + }; self.index += 1; output } @@ -75,8 +74,7 @@ impl<'a> Iterator for KeysIterB<'a> { type Item = &'a Key; fn next(&mut self) -> Option { - let output; - output = self.keys.0.get(self.index); + let output = self.keys.0.get(self.index); self.index += 1; output } diff --git a/keymaps/src/trie.rs b/keymaps/src/trie.rs index 5012422..87b4671 100644 --- a/keymaps/src/trie.rs +++ b/keymaps/src/trie.rs @@ -14,6 +14,12 @@ pub struct Node> { is_child: bool, } +impl> Default for Node { + fn default() -> Self { + Self::new() + } +} + impl> Node { pub fn new() -> Node { Node { @@ -44,7 +50,7 @@ impl> Node { /// empty vector /// The boolean denotes if the returned node is a true end or just a waypoint. It should be /// called, when the bool is true - pub fn get(&self, keys: &Keys) -> Option<(Vec<&Box>>, bool)> { + pub fn get(&self, keys: &Keys) -> Option<(Vec<&Node>, bool)> { let mut current_node = self; let mut old_node = None; for char in keys { @@ -64,7 +70,7 @@ impl> Node { // 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 - if let Some(node) = current_node.children.get(&char) { + if let Some(node) = current_node.children.get(char) { old_node = Some((current_node, char)); current_node = node; } else { @@ -77,12 +83,15 @@ impl> Node { Some(( vec![on .children - .get(&char) + .get(char) .expect("This should be some, as this was checked above")], true, )) } 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> Node { .expect("This should be set") .to_owned(), }); - } else if current_node.children.len() > 0 { + } else if !current_node.children.is_empty() { return Err(error::TrieInsertError::NodeHasChildren(keys.to_owned())); } else { current_node.value = Some(value); @@ -133,8 +142,7 @@ impl> Node { let out: Vec<_> = self .children .values() - .map(|node| node.collect_values_all()) - .flatten() + .flat_map(|node| node.collect_values_all()) .collect(); out } @@ -143,7 +151,7 @@ impl> Node { #[cfg(test)] mod test { - use crate::key::{Key, Keys}; + use crate::key_repr::{Key, Keys}; use pretty_assertions::assert_eq; use super::Node; @@ -155,7 +163,7 @@ mod test { str.parse::().unwrap() } fn collect>( - nodes: Option<(Vec<&Box>>, bool)>, + nodes: Option<(Vec<&Node>, bool)>, ) -> Vec<&V> { let (nodes, _should_call) = nodes.unwrap(); nodes @@ -208,8 +216,8 @@ mod test { let mut output: Node = Node::new(); trie.insert(&i("aa"), true).unwrap(); - trie.insert(&i("abd"), true).unwrap(); trie.insert(&i("abe"), true).unwrap(); + trie.insert(&i("abd"), true).unwrap(); output.insert(&i("abd"), true).unwrap(); output.insert(&i("abe"), true).unwrap(); @@ -218,7 +226,10 @@ mod test { output.insert(&i("ace"), true).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(); assert_eq!(nodes, output_nodes);