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
use super::*;
use serde::{Deserialize, Serialize};
use std::rc::Rc;
impl TryFrom<String> for Storage {
type Error = ron::error::SpannedError;
fn try_from(value: String) -> Result<Self, Self::Error> {
ron::from_str(value.as_str())
}
}
#[derive(Clone, Serialize, Deserialize)]
pub struct Storage {
uuid: Uuid,
container_uuid: Uuid,
template_uuid: Option<Uuid>,
data: Vec<u8>,
#[serde(skip, default = "use_default_database")]
db: Rc<StoreDb>,
}
impl Storage {
pub fn new(
container_uuid: Uuid,
template_uuid: Option<Uuid>,
data: Vec<u8>,
db: Rc<StoreDb>,
) -> Self {
Storage {
uuid: Uuid::new_v4(),
container_uuid,
template_uuid,
data,
db,
}
}
}
impl IStorage for Storage {
fn uuid(&self) -> Uuid {
self.uuid
}
fn template_uuid(&self) -> Option<Uuid> {
self.template_uuid
}
fn container(&self) -> CatlibResult<crate::container::Container> {
fetch_container_by_uuid(self.db.clone(), self.container_uuid)
}
fn data(&self) -> Vec<u8> {
self.data.clone()
}
fn update(&mut self, data: Vec<u8>) -> CatlibResult<crate::storage::Storage> {
self.data = data;
self.save()?;
Ok(self.clone())
}
}
impl Model for Storage {
fn save(&mut self) -> CatlibResult<()> {
save_model(
self.db.clone(),
format!("storage-{}", self.uuid()),
ron::to_string(self).unwrap(),
)
}
fn delete(&mut self) -> CatlibResult<()> {
delete_model(self.db.clone(), format!("storage-{}", self.uuid()))
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use crate::container::Container;
use crate::contracts::IStorage;
use crate::storage::Storage;
use crate::*;
use rstest::*;
use uuid::Bytes;
fn _catlib() -> CatLib {
let catlib = db::init_catlib(rand::random::<Bytes>());
catlib
.create_forest(
Identity([1; 32]),
HashSet::from([Identity([2; 32])]),
vec![],
)
.unwrap();
catlib
}
fn _container(catlib: &CatLib) -> Container {
let forest = catlib.find_forest(Identity([1; 32])).unwrap();
forest.create_container().unwrap()
}
#[fixture]
fn catlib() -> CatLib {
_catlib()
}
#[fixture]
fn container() -> Container {
let catlib = _catlib();
_container(&catlib)
}
fn make_storage(container: &Container) -> Storage {
container.create_storage(None, vec![]).unwrap()
}
fn make_storage_with_template(container: &Container, template_id: Uuid) -> Storage {
container.create_storage(Some(template_id), vec![]).unwrap()
}
#[rstest]
fn create_empty_storage(container: Container) {
make_storage(&container);
assert_eq!(container.storages().unwrap().len(), 1);
make_storage(&container);
assert_eq!(container.storages().unwrap().len(), 2);
}
#[rstest]
fn delete_a_storage(container: Container) {
make_storage(&container);
let mut storage = make_storage(&container);
storage.delete().unwrap();
assert_eq!(container.storages().unwrap().len(), 1);
}
#[rstest]
fn create_storage_with_template_id(catlib: CatLib) {
let container = _container(&catlib);
make_storage(&container); make_storage_with_template(&container, Uuid::from_u128(1));
make_storage_with_template(&container, Uuid::from_u128(1));
make_storage_with_template(&container, Uuid::from_u128(2));
let storages = catlib
.find_storages_with_template(Uuid::from_u128(1))
.unwrap();
assert_eq!(storages.len(), 2);
let storages = catlib
.find_storages_with_template(Uuid::from_u128(2))
.unwrap();
assert_eq!(storages.len(), 1);
}
#[rstest]
fn create_storage_with_data(container: Container) {
container
.create_storage(None, b"storage data".to_vec())
.unwrap();
assert_eq!(container.storages().unwrap()[0].data(), b"storage data")
}
}