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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#![doc = include_str!("../DOCS.md")]
pub use paste;
pub use std;
pub mod prelude;
pub use prelude::*;
mod error;
mod query_build;
mod traits;
pub use query_build::Query;
#[cfg(test)]
mod tests;
#[derive(Clone)]
pub struct SingleChange
{
pub apply_change: Operation,
pub revert_change: Operation
}
#[derive(Clone, PartialEq)]
pub enum Operation
{
Add
{
index: usize,
serialized_object: String
},
Delete
{
index: usize
},
Edit
{
index: usize,
field_name: String,
new_value_str: String
}
}
impl Operation
{
pub fn parse_line(line: &str) -> Result<Operation, AironeError>
{
let mut parts_iter = line.split("\t");
let tipo_op = parts_iter.next().unwrap();
if tipo_op == "A"
{
let index: usize = parts_iter.next().unwrap().parse().unwrap();
let obj_string = parts_iter.collect::<Vec<&str>>().join("\t");
return Ok(Operation::Add {
index,
serialized_object: obj_string
});
}
else if tipo_op == "D"
{
let index: usize = parts_iter.next().unwrap().parse().unwrap();
return Ok(Operation::Delete { index });
}
else if tipo_op == "E"
{
let index: usize = parts_iter.next().unwrap().parse().unwrap();
let field: String = parts_iter.next().unwrap().to_string();
let value_str: String = parts_iter.next().unwrap().to_string();
return Ok(Operation::Edit {
index,
field_name: field,
new_value_str: value_str
});
}
else
{
return Err(AironeError::ParseFailed);
}
}
pub fn to_line(&self) -> String
{
match &self
{
Operation::Add {
index,
serialized_object
} =>
{
format!("A\t{}\t{}", index, serialized_object)
}
Operation::Delete { index } =>
{
format!("D\t{}", index)
}
Operation::Edit {
index,
field_name,
new_value_str
} =>
{
format!("E\t{}\t{}\t{}", index, field_name, new_value_str)
}
}
}
}
#[macro_export]
macro_rules! airone_init {
() => {
pub struct AironeDb<T>
where
T: $crate::CsvLoadable + $crate::CsvSerializable
{
buf_writer: std::io::BufWriter<std::fs::File>,
elements: Vec<T>,
current_transaction_changes: Vec<$crate::SingleChange>,
autocommit: bool
}
};
}
#[macro_export]
macro_rules! airone_db
{
(
$(#[$meta:meta])*
$vis:vis struct $struct_name:ident
{
$(
$field_vis:vis $field_name:ident : $field_type:ty
),*
}
) => {
$(#[$meta])*
$vis struct $struct_name
{
$(
$field_vis $field_name: $field_type,
)*
}
$crate::paste::paste!{
impl $struct_name
{
$(
fn [<get_ $field_name>](&self) -> &$field_type
{
&self.$field_name
}
fn [<set_ $field_name>](&mut self, value: $field_type)
{
self.$field_name = value;
}
)*
}
impl $crate::CsvLoadable for $struct_name
{
fn load_object(s: &String) -> Self
{
let mut parts = s.split("\t");
$(
let next_part = parts.next().unwrap().to_string();
let [<found_ $field_name>]: $field_type = $crate::LoadableValue::from_persistable_string(&next_part);
)*
let obj = Self {
$(
$field_name: [<found_ $field_name>],
)*
};
return obj;
}
}
impl $crate::CsvSerializable for $struct_name
{
fn serialize_object(&self) -> String
{
use $crate::PersistableValue;
let mut output = String::new();
$(
let value: $field_type = self.$field_name.clone();
output.push_str(&format!(
"{}\t",
value.to_persistable_string()
));
)*
output.pop();
return output;
}
}
impl $crate::GetSetObject for $struct_name
{
fn set_str(&mut self, key: &str, value: &str)
{
match key
{
$(
stringify!($field_name) => {
self.$field_name = $crate::LoadableValue::from_persistable_string(value)
},
)*
_ => {panic!("Key not found")}
}
}
}
}
$crate::paste::paste!{
impl $crate::Database<$struct_name> for AironeDb<$struct_name>
{
fn set_auto_commit(&mut self, new_state: bool)
{
self.autocommit = new_state;
}
fn get_auto_commit(&self) -> bool
{
return self.autocommit;
}
fn new() -> Self
{
const FILENAME: &str = stringify!($struct_name);
return Self::new_with_filename(FILENAME);
}
fn new_with_filename(custom_filename: &str) -> Self
{
use $crate::prelude::InternalDatabaseMethods;
if cfg!(target_os="windows")
{
eprintln!("You're running a Windows operating system, but Airone is not supported on, has never been tested on and does not endorse proprietary operating systems.\n\
I encourage you to switch to an actually tested and privacy-and-freedom respecting operating system such as Gnu/Linux.\n\
Head to https://www.fsf.org/windows for more information.");
}
else if cfg!(target_os="macos") || cfg!(target_os="ios")
{
eprintln!("You're running an Apple operating system, but Airone is not supported on, has never been tested on and does not endorse proprietary operating systems.\n\
I encourage you to switch to an actually tested and privacy-and-freedom respecting operating system such as Gnu/Linux.\n\
Learn more at https://www.fsf.org/campaigns/apple");
}
{
let output = std::process::Command::new("uname")
.arg("-a")
.output()
.expect("Failed to execute command");
let st = String::from_utf8(output.stdout).unwrap();
if st.to_ascii_lowercase().contains("microsoft")
{
eprintln!("You're running airone inside of WSL, which is a compatibility layer and may unexpectedly break or introduce bugs. Airone has never been tested on WSL. \n\
Anyway, to get the best from this library I highly encourage you to switch to a traditionally installed operating system such as a full Gnu/Linux installation instead.
");
}
}
let mut starting_elements: Vec<$struct_name> = Vec::with_capacity(
Self::estimate_size(custom_filename)
);
Self::full_load(&mut starting_elements, custom_filename).unwrap();
Self::compact(&mut starting_elements, custom_filename).unwrap();
let mut file = $crate::std::fs::OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open(format!("{}{}", custom_filename, ".changes.csv"))
.unwrap();
return Self {
autocommit: true,
current_transaction_changes: Vec::new(),
elements: starting_elements,
buf_writer: $crate::std::io::BufWriter::new(
file
)
};
}
}
impl $crate::InternalDatabaseMethods<$struct_name> for AironeDb<$struct_name>
{
fn get_elements(&self) -> &Vec<$struct_name>
{
return &self.elements;
}
fn get_elements_mut(&mut self) -> &mut Vec<$struct_name>
{
return &mut self.elements;
}
fn get_buf_writer(&mut self) -> &mut $crate::std::io::BufWriter<$crate::std::fs::File>
{
return &mut self.buf_writer;
}
fn transact_insert_change(&mut self, change: $crate::SingleChange) -> Result<(), $crate::AironeError>
{
self.current_transaction_changes.push(change);
if self.get_auto_commit()
{
self.commit()?;
}
Ok(())
}
fn transact_get_list(&mut self) -> &Vec<$crate::SingleChange>
{
&self.current_transaction_changes
}
fn transact_get_list_mut(&mut self) -> &mut Vec<$crate::SingleChange>
{
&mut self.current_transaction_changes
}
}
impl AironeDb<$struct_name>
{
$(
$field_vis fn [<get_ $field_name>](&self, index: usize) -> Result<&$field_type,AironeError>
{
if let Some(e) = &self.elements.get(index)
{
return Ok(&e.$field_name)
}
else
{
return Err(AironeError::OutOfBound)
}
}
$field_vis fn [<set_ $field_name>](&mut self, index: usize, value: $field_type) -> Result<(),AironeError>
{
use $crate::prelude::InternalDatabaseMethods;
if let Some(el) = self.get_elements_mut().get_mut(index)
{
let old_value = el.$field_name.clone();
el.$field_name = value.clone();
self.transact_insert_change(
self.internal_op_edit(
index,
stringify!($field_name),
&old_value,
&value
)
)?;
return Ok(())
}
else
{
return Err(AironeError::OutOfBound);
}
}
$field_vis fn [<set_bulk_ $field_name>](&mut self, indices: Vec<usize>, value: $field_type) -> Result<(),AironeError>
{
use $crate::prelude::InternalDatabaseMethods;
for index in indices
{
let el: &mut $struct_name = match self.get_elements_mut().get_mut(index)
{
Some(e) => e,
None => return Err(AironeError::OutOfBound)
};
let old_value = el.$field_name.clone();
el.$field_name = value.clone();
self.transact_insert_change(
self.internal_op_edit(
index,
stringify!($field_name),
&old_value,
&value
)
)?;
}
return Ok(())
}
)*
}
}
}
}