datex_core/value_updates/update_data/
mod.rs1mod serde_dif;
2
3use crate::{
4 prelude::*,
5 shared_values::base_shared_value_container::observers::TransceiverId,
6 values::value_container::ValueContainer,
7};
8use strum::{AsRefStr, EnumString};
9mod append_entry;
10mod decrement;
11mod delete_entry;
12mod increment;
13mod list_splice;
14mod replace;
15mod set_entry;
16
17pub use append_entry::*;
18pub use decrement::*;
19pub use delete_entry::*;
20pub use increment::*;
21pub use list_splice::*;
22pub use replace::*;
23pub use set_entry::*;
24
25mod to_instructions;
26
27use strum::VariantNames;
28
29#[derive(Clone, Debug, PartialEq, AsRefStr, EnumString, VariantNames)]
30#[strum(serialize_all = "snake_case")]
31pub enum UpdateOperationKind {
32 Replace,
33 SetEntry,
34 DeleteEntry,
35 Clear,
36 AppendEntry,
37 ListSplice,
38 Increment,
39 Decrement,
40}
41
42#[derive(Clone, Debug, PartialEq, AsRefStr, Hash)]
43#[strum(serialize_all = "snake_case")]
44pub enum UpdateOperation {
45 Replace(Box<ReplaceUpdateData>),
47
48 SetEntry(Box<SetEntryUpdateData>),
51
52 DeleteEntry(Box<DeleteEntryUpdateData>),
54
55 Clear,
57
58 AppendEntry(Box<AppendEntryUpdateData>),
60
61 ListSplice(Box<ListSpliceUpdateData>),
63
64 Increment(Box<IncrementUpdateData>),
66
67 Decrement(Box<DecrementUpdateData>),
69}
70impl UpdateOperation {
71 pub fn replace(value: ValueContainer) -> Self {
72 UpdateOperation::Replace(Box::new(ReplaceUpdateData::new(value)))
73 }
74 pub fn set_entry(key: ValueContainer, value: ValueContainer) -> Self {
75 UpdateOperation::SetEntry(Box::new(SetEntryUpdateData::new(key, value)))
76 }
77 pub fn delete_entry(key: ValueContainer) -> Self {
78 UpdateOperation::DeleteEntry(Box::new(DeleteEntryUpdateData::new(key)))
79 }
80 pub fn clear() -> Self {
81 UpdateOperation::Clear
82 }
83 pub fn append_entry(value: ValueContainer) -> Self {
84 UpdateOperation::AppendEntry(Box::new(AppendEntryUpdateData::new(
85 value,
86 )))
87 }
88 pub fn list_splice(
89 start_index: u32,
90 delete_count: u32,
91 items_to_insert: Vec<ValueContainer>,
92 ) -> Self {
93 UpdateOperation::ListSplice(Box::new(ListSpliceUpdateData::new(
94 start_index,
95 delete_count,
96 items_to_insert,
97 )))
98 }
99 pub fn increment(amount: ValueContainer) -> Self {
100 UpdateOperation::Increment(Box::new(IncrementUpdateData::new(amount)))
101 }
102 pub fn decrement(amount: ValueContainer) -> Self {
103 UpdateOperation::Decrement(Box::new(DecrementUpdateData::new(amount)))
104 }
105}
106
107#[derive(Clone, Debug, PartialEq, AsRefStr, Hash)]
108pub enum UpdateOperator {
109 AppendEntry, DeleteEntry, Increment, Decrement, }
114
115impl UpdateOperation {
116 pub fn with_source(
118 self,
119 source_id: TransceiverId,
120 path: Vec<ValueContainer>,
121 ) -> Update {
122 Update {
123 source_id,
124 data: UpdateData::new_with_path(self, path),
125 }
126 }
127
128 pub fn with_source_root(self, source_id: TransceiverId) -> Update {
130 Self::with_source(self, source_id, vec![])
131 }
132}
133#[derive(Clone, Debug, PartialEq, Hash)]
135pub struct Update {
136 source_id: TransceiverId,
137 data: UpdateData,
138}
139impl Update {
140 pub fn new(source_id: TransceiverId, data: UpdateData) -> Self {
142 Update { source_id, data }
143 }
144 pub fn source_id(&self) -> &TransceiverId {
145 &self.source_id
146 }
147 pub fn data(&self) -> &UpdateData {
148 &self.data
149 }
150 pub fn path(&self) -> &[ValueContainer] {
151 self.data.path()
152 }
153 pub fn operation(&self) -> &UpdateOperation {
154 self.data.operation()
155 }
156 pub fn into_operation(self) -> UpdateOperation {
157 self.data.into_operation()
158 }
159 pub fn into_parts(
160 self,
161 ) -> (TransceiverId, UpdateOperation, Vec<ValueContainer>) {
162 let (operation, path) = self.data.into_parts();
163 (self.source_id, operation, path)
164 }
165}
166
167#[derive(Clone, Debug, PartialEq, Hash)]
168pub struct UpdateData {
169 operation: UpdateOperation,
171 path: Vec<ValueContainer>,
174}
175
176impl UpdateData {
177 pub fn new(operation: UpdateOperation) -> Self {
179 UpdateData {
180 operation,
181 path: vec![],
182 }
183 }
184 pub fn new_with_path(
185 operation: UpdateOperation,
186 path: Vec<ValueContainer>,
187 ) -> Self {
188 UpdateData { operation, path }
189 }
190
191 pub fn path(&self) -> &[ValueContainer] {
193 &self.path
194 }
195
196 pub fn into_parts(self) -> (UpdateOperation, Vec<ValueContainer>) {
197 (self.operation, self.path)
198 }
199
200 pub fn operation(&self) -> &UpdateOperation {
201 &self.operation
202 }
203 pub fn into_operation(self) -> UpdateOperation {
204 self.operation
205 }
206}