turingdb_helpers/
commands.rs1use serde::{Deserialize, Serialize};
2use tai64::TAI64N;
3#[derive(Debug, Eq, PartialEq)]
5pub enum TuringOp {
6 RepoCreate,
8 RepoDrop,
10 DbCreate,
12 DbList,
14 DbDrop,
16 DocumentCreate,
18 DocumentList,
20 DocumentDrop,
22 FieldInsert,
24 FieldGet,
26 FieldRemove,
28 FieldModify,
30 FieldList,
32 NotSupported,
34}
35pub 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}
54pub 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#[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 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 pub fn update(&mut self, value: &[u8]) -> &FieldData {
105 self.data = value.into();
106 self.modified = TAI64N::now();
107
108 self
109 }
110}