1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::collections::VecDeque;

use crate::db::{entry::Entry, group::Group};

#[derive(Debug, Eq, PartialEq, Clone)]
pub(crate) enum NodePathElement<'a> {
    #[allow(dead_code)]
    UUID(&'a str),
    Title(&'a str),
}

pub(crate) type NodePath<'a> = Vec<NodePathElement<'a>>;

impl<'a> NodePathElement<'_> {
    pub(crate) fn matches(&self, node: &Node) -> bool {
        let uuid = match node {
            Node::Entry(e) => e.uuid,
            Node::Group(g) => g.uuid,
        };
        let title = match node {
            Node::Entry(e) => e.get_title(),
            Node::Group(g) => Some(g.get_name()),
        };
        match self {
            NodePathElement::UUID(u) => uuid.to_string() == *u,
            NodePathElement::Title(t) => {
                if let Some(title) = title {
                    return title == *t;
                }
                return false;
            }
        }
    }

    pub(crate) fn wrap_titles(path: &[&'a str]) -> NodePath<'a> {
        let mut response: NodePath = vec![];
        for path_element in path {
            response.push(NodePathElement::Title(path_element));
        }
        response
    }
}

/// An owned node in the database tree structure which can either be an Entry or Group
#[derive(Debug, Eq, PartialEq, Clone)]
#[cfg_attr(feature = "serialization", derive(serde::Serialize))]
pub enum Node {
    Group(Group),
    Entry(Entry),
}

impl Node {
    pub fn as_ref<'a>(&'a self) -> NodeRef<'a> {
        self.into()
    }

    pub fn as_mut<'a>(&'a mut self) -> NodeRefMut<'a> {
        self.into()
    }
}

impl From<Entry> for Node {
    fn from(entry: Entry) -> Self {
        Node::Entry(entry)
    }
}

impl From<Group> for Node {
    fn from(group: Group) -> Self {
        Node::Group(group)
    }
}

/// A shared reference to a node in the database tree structure which can either point to an Entry or a Group
#[derive(Debug, Eq, PartialEq)]
pub enum NodeRef<'a> {
    Group(&'a Group),
    Entry(&'a Entry),
}

impl<'a> std::convert::From<&'a Node> for NodeRef<'a> {
    fn from(n: &'a Node) -> Self {
        match n {
            Node::Group(g) => NodeRef::Group(g),
            Node::Entry(e) => NodeRef::Entry(e),
        }
    }
}

/// An exclusive mutable reference to a node in the database tree structure which can either point to an Entry or a Group
#[derive(Debug, Eq, PartialEq)]
pub enum NodeRefMut<'a> {
    Group(&'a mut Group),
    Entry(&'a mut Entry),
}

impl<'a> std::convert::From<&'a mut Node> for NodeRefMut<'a> {
    fn from(n: &'a mut Node) -> Self {
        match n {
            Node::Group(g) => NodeRefMut::Group(g),
            Node::Entry(e) => NodeRefMut::Entry(e),
        }
    }
}

/// An iterator over Group and Entry references
pub struct NodeIter<'a> {
    queue: VecDeque<NodeRef<'a>>,
}

impl<'a> NodeIter<'a> {
    pub fn new(queue: VecDeque<NodeRef<'a>>) -> Self {
        Self { queue }
    }
}

impl<'a> Iterator for NodeIter<'a> {
    type Item = NodeRef<'a>;

    fn next(&mut self) -> Option<NodeRef<'a>> {
        let head = self.queue.pop_front()?;

        if let NodeRef::Group(ref g) = head {
            self.queue.extend(g.children.iter().map(|n| n.into()))
        }

        Some(head)
    }
}