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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
use serde::{Deserialize, Serialize};
use crate::{
prelude::{TypeTag, TypeValidation},
types::Type,
};
use std::{
collections::HashMap,
fs::File,
hash::Hash,
path::PathBuf,
sync::{Arc, Mutex},
};
#[derive(Debug, Default, Clone, Serialize, Deserialize, Hash)]
pub struct Bytes {
pub data: Vec<u8>,
pub coursor: usize,
pub fast_invert: bool,
}
impl From<Vec<u8>> for Bytes {
fn from(value: Vec<u8>) -> Self {
Self {
coursor: value.len(),
data: value,
fast_invert: false,
}
}
}
impl From<&[u8]> for Bytes {
fn from(value: &[u8]) -> Self {
Self {
coursor: value.len(),
data: value.to_vec(),
fast_invert: false,
}
}
}
impl std::io::Write for Bytes {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let len = buf.len();
for b in buf {
if self.data.len() == self.coursor {
self.data.push(*b);
} else {
self.data[self.coursor] = *b;
self.coursor += 1;
}
}
Ok(len)
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl std::io::Read for Bytes {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let mut readed = 0;
for byte in buf.iter_mut() {
if self.coursor == self.data.len() {
break;
}
if self.coursor == 0 {
break;
}
*byte = if self.fast_invert {
self.data[self.data.len() - self.coursor]
} else {
self.data[self.coursor]
};
readed += 1;
self.coursor += 1;
if self.coursor == self.data.len() {
break;
}
if self.coursor == 0 {
break;
}
}
Ok(readed)
}
}
impl std::io::Seek for Bytes {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
match pos {
std::io::SeekFrom::Start(pos) => {
let res = pos;
if res >= self.data.len() as u64 {
Err(std::io::Error::from_raw_os_error(25))
} else {
self.coursor = res as usize;
Ok(res)
}
}
std::io::SeekFrom::End(pos) => {
let res = (self.data.len() as i64) + pos;
if res >= self.data.len() as i64 {
Err(std::io::Error::from_raw_os_error(24))
} else {
self.coursor = res as usize;
Ok(res as u64)
}
}
std::io::SeekFrom::Current(pos) => {
let res = pos + self.coursor as i64;
if res >= self.data.len() as i64 {
Err(std::io::Error::from_raw_os_error(25))
} else {
self.coursor = res as usize;
Ok(res as u64)
}
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
pub struct Value {
pub value: Type,
pub should_be: Vec<TypeTag>,
pub validators: Vec<TypeValidation>,
pub default: Type,
pub desc: String,
pub editabile: bool,
}
impl Value {
pub fn new(
value: Type,
should_be: Vec<TypeTag>,
validators: Vec<TypeValidation>,
editabile: bool,
desc: impl Into<String>,
) -> Self {
Self {
value: value.clone(),
default: value,
should_be,
validators,
desc: desc.into(),
editabile,
}
}
}
impl From<Type> for Value {
fn from(value: Type) -> Self {
Self {
value: value.clone(),
should_be: vec![value.to_tag()],
validators: vec![],
default: value,
desc: String::new(),
editabile: true,
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Data {
pub data: HashMap<String, Value>,
pub locked: bool,
}
impl Hash for Data {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.locked.hash(state);
for (k, v) in self.data.iter() {
k.hash(state);
v.hash(state)
}
}
}
impl Data {
pub fn new() -> Self {
Self {
data: HashMap::new(),
locked: false,
}
}
pub fn set(&mut self, key: &str, value: Type) -> Option<Type> {
let Some(data) = self.data.get_mut(key) else{
return None;
};
let mut value = value;
std::mem::swap(&mut data.value, &mut value);
Some(value)
}
pub fn reset(&mut self, key: &str) -> Option<Type> {
let Some(data) = self.data.get(key)else{return None};
self.set(key, data.default.clone())
}
pub fn get(&self, key: &str) -> Option<&Type> {
let Some(data) = self.data.get(key) else{
return None;
};
Some(&data.value)
}
pub fn get_mut(&mut self, key: &str) -> Option<&mut Type> {
let Some(data) = self.data.get_mut(key) else{
return None;
};
Some(&mut data.value)
}
pub fn validate(&self) -> Option<String> {
let mut errors = String::new();
for (key, value) in self.iter() {
let mut has_correct_type = false;
for should_be in value.should_be.iter() {
let t = value.value.to_tag();
if *should_be == t {
has_correct_type = true;
break;
}
}
if !has_correct_type {
let mut buff = format!("`{}` should be: ", key);
for (i, should_be) in value.should_be.iter().enumerate() {
if i > 0 {
buff.push(',');
}
buff.push_str(&should_be.to_string());
}
errors.push_str(&buff);
}
if !has_correct_type
{
return Some(errors);
}
}
None
}
pub fn get_value(&self, key: &str) -> Option<&Value> {
self.data.get(key)
}
pub fn get_mut_value(&mut self, key: &str) -> Option<&mut Value> {
self.data.get_mut(key)
}
pub fn remove(&mut self, key: &str) -> Option<Value> {
self.data.remove(key)
}
pub fn add(&mut self, key: &str, value: impl Into<Value>) {
if !self.locked {
self.data.insert(key.to_owned(), value.into());
}
}
pub fn lock(&mut self) {
self.locked = true;
}
pub fn unlock(&mut self) {
self.locked = false;
}
pub fn iter(&self) -> std::collections::hash_map::Iter<String, Value> {
self.data.iter()
}
pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<String, Value> {
self.data.iter_mut()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FileOrData {
File(PathBuf, #[serde(skip)] Option<Arc<Mutex<std::fs::File>>>),
Bytes(Bytes),
}
impl Hash for FileOrData {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
FileOrData::File(f, _) => f.hash(state),
FileOrData::Bytes(b) => b.hash(state),
}
}
}
impl std::io::Write for FileOrData {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
FileOrData::File(file_path, file) => {
if let Some(file) = file {
file.lock().unwrap().write(buf)
} else {
let mut f = File::options()
.create(true)
.write(true)
.read(true)
.open(file_path)?;
let res = f.write(buf);
*file = Some(Arc::new(Mutex::new(f)));
res
}
}
FileOrData::Bytes(bytes) => bytes.write(buf),
}
}
fn flush(&mut self) -> std::io::Result<()> {
match self {
FileOrData::File(_, file) => {
if let Some(file) = file {
file.lock().unwrap().flush()
} else {
Ok(())
}
}
FileOrData::Bytes(_) => Ok(()),
}
}
}
impl std::io::Read for FileOrData {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
FileOrData::File(file_path, file) => {
if let Some(file) = file {
file.lock().unwrap().read(buf)
} else {
let mut f = File::options().read(true).write(true).open(file_path)?;
let res = f.read(buf);
*file = Some(Arc::new(Mutex::new(f)));
res
}
}
FileOrData::Bytes(bytes) => bytes.read(buf),
}
}
}
impl std::io::Seek for FileOrData {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
match self {
FileOrData::File(file_path, file) => {
if let Some(file) = file {
file.lock().unwrap().seek(pos)
} else {
let mut f = File::options().read(true).write(true).open(file_path)?;
let res = f.seek(pos);
*file = Some(Arc::new(Mutex::new(f)));
res
}
}
FileOrData::Bytes(bytes) => bytes.seek(pos),
}
}
}