forked from trinitrix/core
fix(keymaps): Apply `clippy` suggestions
This commit is contained in:
parent
831831cd1c
commit
8d3a421e4c
|
@ -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("<BACKSPACE>"),
|
||||
KeyValue::Enter => w("<ENTER>"),
|
||||
|
|
|
@ -48,12 +48,11 @@ impl Iterator for KeysIter {
|
|||
type Item = Key;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
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<Self::Item> {
|
||||
let output;
|
||||
output = self.keys.0.get(self.index);
|
||||
let output = self.keys.0.get(self.index);
|
||||
self.index += 1;
|
||||
output
|
||||
}
|
||||
|
|
|
@ -14,6 +14,12 @@ pub struct Node<V: std::fmt::Display + ToOwned<Owned = V>> {
|
|||
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> {
|
||||
pub fn new() -> Node<V> {
|
||||
Node {
|
||||
|
@ -44,7 +50,7 @@ impl<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
|
|||
/// 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<Node<V>>>, bool)> {
|
||||
pub fn get(&self, keys: &Keys) -> Option<(Vec<&Node<V>>, bool)> {
|
||||
let mut current_node = self;
|
||||
let mut old_node = None;
|
||||
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: 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<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
|
|||
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<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
|
|||
.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<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
|
|||
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<V: std::fmt::Display + ToOwned<Owned = V>> Node<V> {
|
|||
|
||||
#[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::<Key>().unwrap()
|
||||
}
|
||||
fn collect<V: std::fmt::Display + ToOwned<Owned = V>>(
|
||||
nodes: Option<(Vec<&Box<Node<V>>>, bool)>,
|
||||
nodes: Option<(Vec<&Node<V>>, bool)>,
|
||||
) -> Vec<&V> {
|
||||
let (nodes, _should_call) = nodes.unwrap();
|
||||
nodes
|
||||
|
@ -208,8 +216,8 @@ mod test {
|
|||
let mut output: Node<bool> = 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);
|
||||
|
|
Loading…
Reference in New Issue