Resolve rust clippy warnings (#946)

* refactor: avoid using `collect()` when not needed
and cascade notation

* refactor:  The user might expect to be able
to use Default as the type can be
constructed without arguments.

* refactor: using `clone` on type `indextree::NodeId`
which implements the `Copy` trait

* refactor: remove intermediary
variables and use cascade notation

* refactor: using `clone` on type `indextree::NodeId`
which implements the `Copy` trait

* refactor: unneeded `return` statement

* refactor: ok_or_else avoids executing a
function when it's not needed

* refactor: dereferenced by the compiler

* refactor: user enumeration for index

* refactor: using `clone` on type `usize`
 which implements the `Copy` trait

* refactor: useless conversion to the
same type: `&str`

* refactor:  The user might expect to be able use Default
as type can be constructed without arguments

* refactor:  The user might expect to be able use Default
as type can be constructed without arguments

* fix: rust formating with fmt

* fix: conflict default implementation

Co-authored-by: appflowy <annie@appflowy.io>
This commit is contained in:
gabrielztk
2022-09-01 01:26:51 -03:00
committed by GitHub
parent fd7b0af10e
commit 70f9a289a4
5 changed files with 34 additions and 30 deletions

View File

@ -285,10 +285,11 @@ fn merge_groups(old_groups: &[GroupRevision], groups: Vec<Group>) -> MergeGroupR
} }
// Find out the new groups // Find out the new groups
let new_groups = group_map.into_values(); group_map
for (index, group) in new_groups.into_iter().enumerate() { .into_values()
merge_result.add_insert_group(index, group); .enumerate()
} .for_each(|(index, group)| merge_result.add_insert_group(index, group));
merge_result merge_result
} }

View File

@ -3,6 +3,12 @@ use std::collections::HashMap;
#[derive(Clone, serde::Serialize, serde::Deserialize)] #[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct NodeAttributes(pub HashMap<String, Option<String>>); pub struct NodeAttributes(pub HashMap<String, Option<String>>);
impl Default for NodeAttributes {
fn default() -> Self {
Self::new()
}
}
impl NodeAttributes { impl NodeAttributes {
pub fn new() -> NodeAttributes { pub fn new() -> NodeAttributes {
NodeAttributes(HashMap::new()) NodeAttributes(HashMap::new())

View File

@ -10,10 +10,16 @@ pub struct DocumentTree {
pub root: NodeId, pub root: NodeId,
} }
impl Default for DocumentTree {
fn default() -> Self {
Self::new()
}
}
impl DocumentTree { impl DocumentTree {
pub fn new() -> DocumentTree { pub fn new() -> DocumentTree {
let mut arena = Arena::new(); let mut arena = Arena::new();
let root = arena.new_node(NodeData::new("root".into())); let root = arena.new_node(NodeData::new("root"));
DocumentTree { arena, root } DocumentTree { arena, root }
} }
@ -25,7 +31,7 @@ impl DocumentTree {
let mut iterate_node = self.root; let mut iterate_node = self.root;
for id in &position.0 { for id in &position.0 {
let child = self.child_at_index_of_path(iterate_node, id.clone()); let child = self.child_at_index_of_path(iterate_node, *id);
iterate_node = match child { iterate_node = match child {
Some(node) => node, Some(node) => node,
None => return None, None => return None,
@ -74,13 +80,10 @@ impl DocumentTree {
fn child_at_index_of_path(&self, at_node: NodeId, index: usize) -> Option<NodeId> { fn child_at_index_of_path(&self, at_node: NodeId, index: usize) -> Option<NodeId> {
let children = at_node.children(&self.arena); let children = at_node.children(&self.arena);
let mut counter = 0; for (counter, child) in children.enumerate() {
for child in children {
if counter == index { if counter == index {
return Some(child); return Some(child);
} }
counter += 1;
} }
None None
@ -107,7 +110,7 @@ impl DocumentTree {
let last_index = path.0[path.0.len() - 1]; let last_index = path.0[path.0.len() - 1];
let parent_node = self let parent_node = self
.node_at_path(&Position(parent_path.to_vec())) .node_at_path(&Position(parent_path.to_vec()))
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?; .ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
self.insert_child_at_index(parent_node, last_index, nodes.as_ref()) self.insert_child_at_index(parent_node, last_index, nodes.as_ref())
} }
@ -132,7 +135,7 @@ impl DocumentTree {
let node_to_insert = self let node_to_insert = self
.child_at_index_of_path(parent, index) .child_at_index_of_path(parent, index)
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?; .ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
self.insert_subtree_before(&node_to_insert, insert_children); self.insert_subtree_before(&node_to_insert, insert_children);
Ok(()) Ok(())
@ -160,11 +163,11 @@ impl DocumentTree {
fn apply_update(&mut self, path: &Position, attributes: &NodeAttributes) -> Result<(), OTError> { fn apply_update(&mut self, path: &Position, attributes: &NodeAttributes) -> Result<(), OTError> {
let update_node = self let update_node = self
.node_at_path(path) .node_at_path(path)
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?; .ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
let node_data = self.arena.get_mut(update_node).unwrap(); let node_data = self.arena.get_mut(update_node).unwrap();
let new_node = { let new_node = {
let old_attributes = &node_data.get().attributes; let old_attributes = &node_data.get().attributes;
let new_attributes = NodeAttributes::compose(&old_attributes, attributes); let new_attributes = NodeAttributes::compose(old_attributes, attributes);
NodeData { NodeData {
attributes: new_attributes, attributes: new_attributes,
..node_data.get().clone() ..node_data.get().clone()
@ -177,7 +180,7 @@ impl DocumentTree {
fn apply_delete(&mut self, path: &Position, len: usize) -> Result<(), OTError> { fn apply_delete(&mut self, path: &Position, len: usize) -> Result<(), OTError> {
let mut update_node = self let mut update_node = self
.node_at_path(path) .node_at_path(path)
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?; .ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
for _ in 0..len { for _ in 0..len {
let next = update_node.following_siblings(&self.arena).next(); let next = update_node.following_siblings(&self.arena).next();
update_node.remove_subtree(&mut self.arena); update_node.remove_subtree(&mut self.arena);
@ -193,7 +196,7 @@ impl DocumentTree {
fn apply_text_edit(&mut self, path: &Position, delta: &TextDelta) -> Result<(), OTError> { fn apply_text_edit(&mut self, path: &Position, delta: &TextDelta) -> Result<(), OTError> {
let edit_node = self let edit_node = self
.node_at_path(path) .node_at_path(path)
.ok_or(ErrorBuilder::new(OTErrorCode::PathNotFound).build())?; .ok_or_else(|| ErrorBuilder::new(OTErrorCode::PathNotFound).build())?;
let node_data = self.arena.get_mut(edit_node).unwrap(); let node_data = self.arena.get_mut(edit_node).unwrap();
let new_delta = if let Some(old_delta) = &node_data.get().delta { let new_delta = if let Some(old_delta) = &node_data.get().delta {
Some(old_delta.compose(delta)?) Some(old_delta.compose(delta)?)

View File

@ -35,7 +35,8 @@ impl Position {
prefix.push(b_at_index); prefix.push(b_at_index);
} }
prefix.append(&mut suffix); prefix.append(&mut suffix);
return Position(prefix);
Position(prefix)
} }
} }

View File

@ -63,7 +63,7 @@ impl<'a> TransactionBuilder<'a> {
let mut deleted_nodes: Vec<Box<NodeSubTree>> = Vec::new(); let mut deleted_nodes: Vec<Box<NodeSubTree>> = Vec::new();
for _ in 0..length { for _ in 0..length {
deleted_nodes.push(self.get_deleted_nodes(node.clone())); deleted_nodes.push(self.get_deleted_nodes(node));
node = node.following_siblings(&self.document.arena).next().unwrap(); node = node.following_siblings(&self.document.arena).next().unwrap();
} }
@ -74,19 +74,12 @@ impl<'a> TransactionBuilder<'a> {
} }
fn get_deleted_nodes(&self, node_id: NodeId) -> Box<NodeSubTree> { fn get_deleted_nodes(&self, node_id: NodeId) -> Box<NodeSubTree> {
let node = self.document.arena.get(node_id.clone()).unwrap(); let node_data = self.document.arena.get(node_id).unwrap().get();
let node_data = node.get();
let mut children: Vec<Box<NodeSubTree>> = vec![];
let mut children_iterators = node_id.children(&self.document.arena); let mut children: Vec<Box<NodeSubTree>> = vec![];
loop { node_id.children(&self.document.arena).for_each(|child_id| {
let next_child = children_iterators.next(); children.push(self.get_deleted_nodes(child_id));
if let Some(child_id) = next_child { });
children.push(self.get_deleted_nodes(child_id));
} else {
break;
}
}
Box::new(NodeSubTree { Box::new(NodeSubTree {
node_type: node_data.node_type.clone(), node_type: node_data.node_type.clone(),