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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use super::*;
use super::{CatlibResult, Identity};
use std::collections::HashSet;

pub(crate) mod common;

pub type Signers = HashSet<Identity>;
pub type ContainerPath = String;
pub type ContainerPaths = HashSet<ContainerPath>;

pub trait IForest {
    /// Return UUID object identifier
    fn uuid(&self) -> Uuid;

    /// Return Forest owner
    fn owner(&self) -> Identity;

    /// Return list of manifests Signers
    fn signers(&self) -> Signers;

    /// Return Forest arbitrary data
    fn data(&self) -> Vec<u8>;

    /// Add manifest Signer
    ///
    /// Returns whether the value was newly inserted. That is:
    ///
    /// - If the signer did not previously exist, `true` is returned.
    /// - If the signer already exists, `false` is returned.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    fn add_signer(&mut self, signer: Identity) -> CatlibResult<bool>;

    /// Delete manifest Signer
    ///
    /// Returns whether the value was already present. That is:
    ///
    /// - If the signer did not previously exist, `false` is returned.
    /// - If the signer existed in the set, `true` is returned.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    fn del_signer(&mut self, signer: Identity) -> CatlibResult<bool>;

    /// Return list of Forest Containers
    ///
    /// ## Errors
    ///
    /// Returns [`CatlibError::NoRecordsFound`] if Forest has no [`Container`].
    fn containers(&self) -> CatlibResult<Vec<Container>>;

    /// Set Forest arbitrary data
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    fn update(&mut self, data: Vec<u8>) -> CatlibResult<()>;

    /// Delete Forest from the database
    ///
    /// **WARN: The underlying objects are not removed recursively**
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    fn remove(&mut self) -> CatlibResult<bool>;

    /// Create an empty container, bound to the Forest.
    ///
    /// To set container paths, use [`Container::add_path`]
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use wildland_catlib::CatLib;
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::*;
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  Identity([1; 32]),
    ///                  HashSet::from([Identity([2; 32])]),
    ///                  vec![],
    ///              ).unwrap();
    /// let mut container = forest.create_container().unwrap();
    /// container.add_path("/foo/bar".to_string());
    /// container.add_path("/bar/baz".to_string());
    /// ```
    fn create_container(&self) -> CatlibResult<Container>;

    /// Create a Bridge obect with arbitrary link data to another Forest.
    ///
    /// The aforementioned link data will be defined by the D/FS module.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use wildland_catlib::CatLib;
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::*;
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  Identity([1; 32]),
    ///                  HashSet::from([Identity([2; 32])]),
    ///                  vec![],
    ///              ).unwrap();
    /// forest.create_bridge("/other/forest".to_string(), vec![]);
    /// ```
    fn create_bridge(&self, path: ContainerPath, link_data: Vec<u8>) -> CatlibResult<Bridge>;

    /// Return bridge that matches the given [`ContainerPath`].
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Bridge`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Bridge`] was found.
    fn find_bridge(&self, path: ContainerPath) -> CatlibResult<Bridge>;

    /// Retrieve Containers that match given [`ContainerPath`]s.
    ///
    /// If `include_subdirs` is `true`, then the [`ContainerPath`]s are treated as Path prefixes
    /// and not absolute paths.
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Container`] was found.
    ///
    /// ## Example
    /// # use wildland_catlib::CatLib;
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::*;
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  b"owner".to_vec(),
    ///                  HashSet::from([b"signer".to_vec()]),
    ///                  vec![],
    ///              ).unwrap();
    /// let mut container = forest.create_container().unwrap();
    /// container.add_path("/foo/bar".to_string());
    ///
    /// let containers = forest.find_containers(vec!["/foo/bar".to_string()], false).unwrap();
    fn find_containers(
        &self,
        paths: Vec<ContainerPath>,
        include_subdirs: bool,
    ) -> CatlibResult<Vec<Container>>;
}

pub trait IContainer {
    /// Return UUID object identifier
    fn uuid(&self) -> Uuid;

    /// Return [`Forest`] that contains the [`Container`].
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Forest`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Forest`] was found.
    fn forest(&self) -> CatlibResult<Forest>;

    /// Return [`Container`]'s paths
    fn paths(&self) -> ContainerPaths;

    /// Add a path to the Container.
    ///
    /// Returns self to allow chain method exection.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use wildland_catlib::CatLib;
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::*;
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  Identity([1; 32]),
    ///                  HashSet::from([Identity([2; 32])]),
    ///                  vec![],
    ///              ).unwrap();
    /// let mut container = forest.create_container().unwrap();
    /// container.add_path("/bar/baz2".to_string()).unwrap()
    ///     .add_path("/baz/qux1".to_string()).unwrap()
    ///     .add_path("/baz/qux2".to_string()).unwrap();
    /// ```
    fn add_path(&mut self, path: ContainerPath) -> CatlibResult<Container>;

    /// Delete a path from the Container.
    ///
    /// Returns self to allow chain method exection.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use wildland_catlib::CatLib;
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::*;
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  Identity([1; 32]),
    ///                  HashSet::from([Identity([2; 32])]),
    ///                  vec![],
    ///              ).unwrap();
    /// let mut container = forest.create_container().unwrap();
    /// container.add_path("/bar/baz2".to_string()).unwrap()
    ///     .del_path("/baz/qux1".to_string()).unwrap()
    ///     .del_path("/baz/qux2".to_string()).unwrap();
    /// ```
    fn del_path(&mut self, path: ContainerPath) -> CatlibResult<Container>;

    /// Return list of Forest [`Storage`]s.
    ///
    /// ## Errors
    ///
    /// Returns [`CatlibError::NoRecordsFound`] if Forest has no [`Storage`].
    fn storages(&self) -> CatlibResult<Vec<Storage>>;

    /// Create a [`Storage`], bound to the [`Container`].
    ///
    /// `template_uuid` is an arbitrary, optional, [`String`] that is later used to find
    /// [`Container`]s and [`Storage`]s using [`CatLib::find_storages_with_template`] and
    /// [`CatLib::find_containers_with_template`].
    ///
    /// `data` represents arbitrary data that is defined and used by the DF/S module.
    ///
    /// ## Errors
    ///
    /// Returns `RustbreakError` cast on [`CatlibResult`] upon failure to save to the database.
    ///
    /// ## Example
    ///
    /// ```rust
    /// # use wildland_catlib::CatLib;
    /// # use std::collections::HashSet;
    /// # use crate::wildland_catlib::*;
    /// # use uuid::Uuid;
    /// let catlib = CatLib::default();
    /// let forest = catlib.create_forest(
    ///                  Identity([1; 32]),
    ///                  HashSet::from([Identity([2; 32])]),
    ///                  vec![],
    ///              ).unwrap();
    /// let mut container = forest.create_container().unwrap();
    /// container.add_path("/foo/bar".to_string());
    /// container.create_storage(Some(Uuid::from_u128(1)), vec![]).unwrap();
    /// ```
    fn create_storage(&self, template_uuid: Option<Uuid>, data: Vec<u8>) -> CatlibResult<Storage>;
}

pub trait IStorage {
    /// Return UUID object identifier
    fn uuid(&self) -> Uuid;

    /// Return Template UUID
    fn template_uuid(&self) -> Option<Uuid>;

    /// Return [`Container`] that contains the [`Storage`].
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Container`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Container`] was found.
    fn container(&self) -> CatlibResult<Container>;

    /// Return Storage data
    fn data(&self) -> Vec<u8>;

    /// Update Storage data
    fn update(&mut self, data: Vec<u8>) -> CatlibResult<Storage>;
}

pub trait IBridge {
    /// Return UUID object identifier
    fn uuid(&self) -> Uuid;

    // Returns [`Bridge`]'s path
    fn path(&self) -> ContainerPath;

    /// Return [`Forest`] that contains the [`Bridge`].
    ///
    /// ## Errors
    ///
    /// - Returns [`CatlibError::NoRecordsFound`] if no [`Bridge`] was found.
    /// - Returns [`CatlibError::MalformedDatabaseEntry`] if more than one [`Bridge`] was found.
    fn forest(&self) -> CatlibResult<Forest>;

    /// Return Bridge link data
    fn link(&self) -> Vec<u8>;

    /// Update Bridge link data
    fn update(&mut self, data: Vec<u8>) -> CatlibResult<Bridge>;
}