turingdb_helpers/
commands.rs

1use serde::{Deserialize, Serialize};
2use tai64::TAI64N;
3/// Commands to perform on the repo and its contents by the repo owner known as `SuperUser`
4#[derive(Debug, Eq, PartialEq)]
5pub enum TuringOp {
6    /// Initialize the Repository
7    RepoCreate,
8    /// Delete the Repository
9    RepoDrop,
10    /// Create a database
11    DbCreate,
12    /// List all databases in a repo
13    DbList,
14    /// Delete a database
15    DbDrop,
16    /// Create a document
17    DocumentCreate,
18    /// List All Documents
19    DocumentList,
20    /// Delete a document and all its contents
21    DocumentDrop,
22    ///Insert a field into a document
23    FieldInsert,
24    /// Read contents particular document
25    FieldGet,
26    /// Remove a particular document
27    FieldRemove,
28    /// Updata a document
29    FieldModify,
30    /// List all fields in a document
31    FieldList,
32    /// The command is not supported
33    NotSupported,
34}
35/// Converts a database operation to a header using the `TuringOp` enum
36pub fn from_op<'op>(value: &TuringOp) -> &'op [u8] {
37    match *value {
38        TuringOp::RepoCreate => &[0x00],
39        TuringOp::RepoDrop => &[0x01],
40        TuringOp::DbCreate => &[0x02],
41        TuringOp::DbList => &[0x03],
42        TuringOp::DbDrop => &[0x04],
43        TuringOp::DocumentCreate => &[0x05],
44        TuringOp::DocumentList => &[0x06],
45        TuringOp::DocumentDrop => &[0x07],
46        TuringOp::FieldInsert => &[0x08],
47        TuringOp::FieldGet => &[0x09],
48        TuringOp::FieldRemove => &[0x0a],
49        TuringOp::FieldModify => &[0x0b],
50        TuringOp::FieldList => &[0x0c],
51        TuringOp::NotSupported => &[0xf1],
52    }
53}
54/// Converts a database operation from a header to `TuringOp` enum variant
55pub fn to_op(value: &[u8]) -> TuringOp {
56    match *value {
57        [0x00] => TuringOp::RepoCreate,
58        [0x01] => TuringOp::RepoDrop,
59        [0x02] => TuringOp::DbCreate,
60        [0x03] => TuringOp::DbList,
61        [0x04] => TuringOp::DbDrop,
62        [0x05] => TuringOp::DocumentCreate,
63        [0x06] => TuringOp::DocumentList,
64        [0x07] => TuringOp::DocumentDrop,
65        [0x08] => TuringOp::FieldInsert,
66        [0x09] => TuringOp::FieldGet,
67        [0x0a] => TuringOp::FieldRemove,
68        [0x0b] => TuringOp::FieldModify,
69        [0x0c] => TuringOp::FieldList,
70        [0xf1] => TuringOp::NotSupported,
71        _ => TuringOp::NotSupported,
72    }
73}
74/// Contains the structure of a value represented by a key
75///
76/// `Warning:` This is serialized using bincode so deserialization should be done using same version of bincode
77/// ```
78/// #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
79/// pub struct FieldData {
80///     data: Vec<u8>,
81///     created: TAI64N,
82///     modified: TAI64N,
83/// }
84/// ```
85#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
86pub struct FieldData {
87    data: Vec<u8>,
88    created: TAI64N,
89    modified: TAI64N,
90}
91
92impl FieldData {
93    /// Initializes a new `FieldData` struct
94    pub fn new(value: &[u8]) -> FieldData {
95        let current_time = TAI64N::now();
96
97        Self {
98            data: value.into(),
99            created: current_time,
100            modified: current_time,
101        }
102    }
103    /// Updates a `FieldData` by modifying its time with a new `TAI64N` timestamp
104    pub fn update(&mut self, value: &[u8]) -> &FieldData {
105        self.data = value.into();
106        self.modified = TAI64N::now();
107
108        self
109    }
110}