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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
let new_groups = group_map.into_values();
for (index, group) in new_groups.into_iter().enumerate() {
merge_result.add_insert_group(index, group);
}
group_map
.into_values()
.enumerate()
.for_each(|(index, group)| merge_result.add_insert_group(index, group));
merge_result
}

View File

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

View File

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

View File

@ -35,7 +35,8 @@ impl Position {
prefix.push(b_at_index);
}
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();
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();
}
@ -74,19 +74,12 @@ impl<'a> TransactionBuilder<'a> {
}
fn get_deleted_nodes(&self, node_id: NodeId) -> Box<NodeSubTree> {
let node = self.document.arena.get(node_id.clone()).unwrap();
let node_data = node.get();
let mut children: Vec<Box<NodeSubTree>> = vec![];
let node_data = self.document.arena.get(node_id).unwrap().get();
let mut children_iterators = node_id.children(&self.document.arena);
loop {
let next_child = children_iterators.next();
if let Some(child_id) = next_child {
children.push(self.get_deleted_nodes(child_id));
} else {
break;
}
}
let mut children: Vec<Box<NodeSubTree>> = vec![];
node_id.children(&self.document.arena).for_each(|child_id| {
children.push(self.get_deleted_nodes(child_id));
});
Box::new(NodeSubTree {
node_type: node_data.node_type.clone(),