Skip to content

Commit 4b5a889

Browse files
committed
feat: implement add_node fn
1 parent 79bf794 commit 4b5a889

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

src/db/group.rs

+24-28
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ impl Group {
267267
return Err("The group was not found.".to_string());
268268
}
269269

270+
pub fn add_node<T>(&mut self, n: T)
271+
where
272+
T: Into<Node>,
273+
{
274+
self.children.push(n.into())
275+
}
276+
270277
pub(crate) fn insert_entry(
271278
&mut self,
272279
entry: Entry,
@@ -550,7 +557,7 @@ mod group_tests {
550557
let mut entry = Entry::new();
551558
let entry_uuid = entry.uuid.clone();
552559
entry.set_field_and_commit("Title", "entry1");
553-
destination_group.children.push(Node::Entry(entry));
560+
destination_group.add_node(entry);
554561

555562
let mut source_group = destination_group.clone();
556563

@@ -579,7 +586,7 @@ mod group_tests {
579586
let mut entry = Entry::new();
580587
let entry_uuid = entry.uuid.clone();
581588
entry.set_field_and_commit("Title", "entry1");
582-
source_group.children.push(Node::Entry(entry));
589+
source_group.add_node(entry);
583590

584591
destination_group.merge(&source_group);
585592
assert_eq!(destination_group.children.len(), 1);
@@ -599,16 +606,15 @@ mod group_tests {
599606
fn test_merge_add_new_non_root_entry() {
600607
let mut destination_group = Group::new("group1");
601608
let mut destination_sub_group = Group::new("subgroup1");
602-
destination_group
603-
.children
604-
.push(Node::Group(destination_sub_group));
609+
destination_group.add_node(destination_sub_group);
610+
605611
let mut source_group = destination_group.clone();
606612
let mut source_sub_group = &mut source_group.groups_mut()[0];
607613

608614
let mut entry = Entry::new();
609615
let entry_uuid = entry.uuid.clone();
610616
entry.set_field_and_commit("Title", "entry1");
611-
source_sub_group.children.push(Node::Entry(entry));
617+
source_sub_group.add_node(entry);
612618

613619
destination_group.merge(&source_group);
614620
let destination_entries = destination_group.get_all_entries(&vec![]);
@@ -628,8 +634,8 @@ mod group_tests {
628634
let mut entry = Entry::new();
629635
let entry_uuid = entry.uuid.clone();
630636
entry.set_field_and_commit("Title", "entry1");
631-
source_sub_group.children.push(Node::Entry(entry));
632-
source_group.children.push(Node::Group(source_sub_group));
637+
source_sub_group.add_node(entry);
638+
source_group.add_node(source_sub_group);
633639

634640
destination_group.merge(&source_group);
635641
let destination_entries = destination_group.get_all_entries(&vec![]);
@@ -646,15 +652,9 @@ mod group_tests {
646652
let mut destination_group = Group::new("group1");
647653
let mut destination_sub_group1 = Group::new("subgroup1");
648654
let mut destination_sub_group2 = Group::new("subgroup2");
649-
destination_sub_group1
650-
.children
651-
.push(Node::Entry(entry.clone()));
652-
destination_group
653-
.children
654-
.push(Node::Group(destination_sub_group1.clone()));
655-
destination_group
656-
.children
657-
.push(Node::Group(destination_sub_group2.clone()));
655+
destination_sub_group1.add_node(entry.clone());
656+
destination_group.add_node(destination_sub_group1.clone());
657+
destination_group.add_node(destination_sub_group2.clone());
658658

659659
let mut source_group = destination_group.clone();
660660
assert!(source_group.get_all_entries(&vec![]).len() == 1);
@@ -713,12 +713,8 @@ mod group_tests {
713713
entry.set_field_and_commit("Title", "entry1");
714714
let mut destination_group = Group::new("group1");
715715
let mut destination_sub_group = Group::new("subgroup1");
716-
destination_sub_group
717-
.children
718-
.push(Node::Entry(entry.clone()));
719-
destination_group
720-
.children
721-
.push(Node::Group(destination_sub_group));
716+
destination_sub_group.add_node(entry.clone());
717+
destination_group.add_node(destination_sub_group);
722718

723719
let mut source_group = destination_group.clone();
724720
let mut source_sub_group = Group::new("subgroup2");
@@ -727,9 +723,9 @@ mod group_tests {
727723
// FIXME we should not have to update the history here. We should
728724
// have a better compare function in the merge function instead.
729725
entry.update_history();
730-
source_sub_group.children.push(Node::Entry(entry.clone()));
726+
source_sub_group.add_node(entry.clone());
731727
source_group.children = vec![];
732-
source_group.children.push(Node::Group(source_sub_group));
728+
source_group.add_node(source_sub_group);
733729

734730
destination_group.merge(&source_group);
735731
let destination_entries = destination_group.get_all_entries(&vec![]);
@@ -748,7 +744,7 @@ mod group_tests {
748744
let entry_uuid = entry.uuid.clone();
749745
entry.set_field_and_commit("Title", "entry1");
750746

751-
destination_group.children.push(Node::Entry(entry));
747+
destination_group.add_node(entry);
752748

753749
let mut source_group = destination_group.clone();
754750

@@ -768,7 +764,7 @@ mod group_tests {
768764
let mut entry = Entry::new();
769765
let entry_uuid = entry.uuid.clone();
770766
entry.set_field_and_commit("Title", "entry1");
771-
destination_group.children.push(Node::Entry(entry));
767+
destination_group.add_node(entry);
772768

773769
let mut source_group = destination_group.clone();
774770

@@ -788,7 +784,7 @@ mod group_tests {
788784
let mut entry = Entry::new();
789785
let entry_uuid = entry.uuid.clone();
790786
entry.set_field_and_commit("Title", "entry1");
791-
destination_group.children.push(Node::Entry(entry));
787+
destination_group.add_node(entry);
792788

793789
let mut source_group = destination_group.clone();
794790

src/db/node.rs

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ impl<'a> std::convert::From<&'a Node> for NodeRef<'a> {
3636
}
3737
}
3838

39+
impl From<Entry> for Node {
40+
fn from(entry: Entry) -> Self {
41+
Node::Entry(entry)
42+
}
43+
}
44+
45+
impl From<Group> for Node {
46+
fn from(group: Group) -> Self {
47+
Node::Group(group)
48+
}
49+
}
50+
3951
/// An exclusive mutable reference to a node in the database tree structure which can either point to an Entry or a Group
4052
#[derive(Debug, Eq, PartialEq)]
4153
pub enum NodeRefMut<'a> {

0 commit comments

Comments
 (0)