lockbook_shared/
tree_like.rs1use crate::file_like::FileLike;
2use crate::lazy::LazyTree;
3use crate::staged::StagedTree;
4use crate::{SharedErrorKind, SharedResult};
5use std::collections::HashSet;
6use std::fmt::Debug;
7use uuid::Uuid;
8
9pub trait TreeLike: Sized {
10 type F: FileLike + Debug;
11
12 fn ids(&self) -> HashSet<&Uuid>;
14 fn maybe_find(&self, id: &Uuid) -> Option<&Self::F>;
15
16 fn find(&self, id: &Uuid) -> SharedResult<&Self::F> {
17 self.maybe_find(id)
18 .ok_or_else(|| SharedErrorKind::FileNonexistent.into())
19 }
20
21 fn maybe_find_parent<F2: FileLike>(&self, file: &F2) -> Option<&Self::F> {
22 self.maybe_find(file.parent())
23 }
24
25 fn find_parent<F2: FileLike>(&self, file: &F2) -> SharedResult<&Self::F> {
26 self.maybe_find_parent(file)
27 .ok_or_else(|| SharedErrorKind::FileParentNonexistent.into())
28 }
29
30 fn owned_ids(&self) -> HashSet<Uuid> {
31 self.ids().iter().map(|id| **id).collect()
32 }
33
34 fn all_files(&self) -> SharedResult<Vec<&Self::F>> {
35 let mut all = vec![];
36 for id in self.ids() {
37 let meta = self.find(id)?;
38 all.push(meta);
39 }
40
41 Ok(all)
42 }
43
44 fn stage<Staged>(&self, staged: Staged) -> StagedTree<&Self, Staged>
45 where
46 Staged: TreeLike<F = Self::F>,
47 Self: Sized,
48 {
49 StagedTree::new(self, staged)
50 }
51
52 fn to_staged<Staged>(self, staged: Staged) -> StagedTree<Self, Staged>
53 where
54 Staged: TreeLike<F = Self::F>,
55 Self: Sized,
56 {
57 StagedTree::new(self, staged)
58 }
59
60 fn as_lazy(&self) -> LazyTree<&Self> {
61 LazyTree::new(self)
62 }
63
64 fn to_lazy(self) -> LazyTree<Self> {
65 LazyTree::new(self)
66 }
67}
68
69pub trait TreeLikeMut: TreeLike {
70 fn insert(&mut self, f: Self::F) -> SharedResult<Option<Self::F>>;
71 fn remove(&mut self, id: Uuid) -> SharedResult<Option<Self::F>>;
72 fn clear(&mut self) -> SharedResult<()>;
73}
74
75impl<T> TreeLike for &T
76where
77 T: TreeLike,
78{
79 type F = T::F;
80
81 fn ids(&self) -> HashSet<&Uuid> {
82 T::ids(self)
83 }
84
85 fn maybe_find(&self, id: &Uuid) -> Option<&Self::F> {
86 T::maybe_find(self, id)
87 }
88}
89
90impl<T> TreeLike for &mut T
91where
92 T: TreeLike,
93{
94 type F = T::F;
95
96 fn ids(&self) -> HashSet<&Uuid> {
97 T::ids(self)
98 }
99
100 fn maybe_find(&self, id: &Uuid) -> Option<&Self::F> {
101 T::maybe_find(self, id)
102 }
103}
104
105impl<T> TreeLikeMut for &mut T
106where
107 T: TreeLikeMut,
108{
109 fn insert(&mut self, f: Self::F) -> SharedResult<Option<Self::F>> {
110 T::insert(self, f)
111 }
112
113 fn remove(&mut self, id: Uuid) -> SharedResult<Option<Self::F>> {
114 T::remove(self, id)
115 }
116
117 fn clear(&mut self) -> SharedResult<()> {
118 T::clear(self)
119 }
120}
121
122impl<F> TreeLike for Vec<F>
123where
124 F: FileLike,
125{
126 type F = F;
127
128 fn ids(&self) -> HashSet<&Uuid> {
129 self.iter().map(|f| f.id()).collect()
130 }
131
132 fn maybe_find(&self, id: &Uuid) -> Option<&F> {
133 self.iter().find(|f| f.id() == id)
134 }
135}
136
137impl<F> TreeLikeMut for Vec<F>
138where
139 F: FileLike,
140{
141 fn insert(&mut self, f: F) -> SharedResult<Option<F>> {
142 for (i, value) in self.iter().enumerate() {
143 if value.id() == f.id() {
144 let old = std::mem::replace(&mut self[i], f);
145 return Ok(Some(old));
146 }
147 }
148
149 self.push(f);
150
151 Ok(None)
152 }
153
154 fn remove(&mut self, id: Uuid) -> SharedResult<Option<F>> {
155 for (i, value) in self.iter().enumerate() {
156 if *value.id() == id {
157 return Ok(Some(self.remove(i)));
158 }
159 }
160
161 Ok(None)
162 }
163
164 fn clear(&mut self) -> SharedResult<()> {
165 self.clear();
166 Ok(())
167 }
168}