fluxpro_engine/models/context_map/
context_map.rs1use crate::models::context_map::context_patcher::{ContextPatcher, ContextPatcherOp};
4use crate::models::id_field::IdField;
5use chrono::{DateTime, NaiveDate, Utc};
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::collections::HashMap;
9
10#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq)]
33pub struct ContextMap(pub HashMap<IdField, ContextValue>);
34
35impl ContextMap {
36 pub fn get(&self, key: &IdField) -> Option<&ContextValue> {
38 self.0.get(key)
39 }
40}
41
42#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
47#[serde(untagged)]
48#[serde(rename_all = "lowercase")]
49pub enum ContextValue {
50 String {
52 string: String,
54 },
55 IdField {
57 #[serde(alias = "id_field")]
59 id_field: IdField,
60 },
61 Number {
63 number: i64,
65 },
66 Float {
68 float: f64,
70 },
71 Date {
73 date: NaiveDate,
75 },
76 Array {
78 array: Vec<ContextValue>,
80 },
81 DateTime {
83 #[serde(alias = "date_time")]
85 datetime: DateTime<Utc>,
86 },
87 Boolean {
89 boolean: bool,
91 },
92 Object {
94 object: Value,
96 },
97}
98
99impl ContextValue {
100 pub fn string(value: String) -> Self {
102 Self::String { string: value }
103 }
104
105 pub fn id_field(value: IdField) -> Self {
107 Self::IdField { id_field: value }
108 }
109
110 pub fn number(value: i64) -> Self {
112 Self::Number { number: value }
113 }
114
115 pub fn float(value: f64) -> Self {
117 Self::Float { float: value }
118 }
119
120 pub fn date(value: NaiveDate) -> Self {
122 Self::Date { date: value }
123 }
124
125 pub fn array(value: Vec<ContextValue>) -> Self {
127 Self::Array { array: value }
128 }
129
130 pub fn date_time(value: DateTime<Utc>) -> Self {
132 Self::DateTime { datetime: value }
133 }
134
135 pub fn boolean(value: bool) -> Self {
137 Self::Boolean { boolean: value }
138 }
139
140 pub fn object(value: Value) -> Self {
142 Self::Object { object: value }
143 }
144}
145
146impl ContextMap {
147 pub fn apply_patcher(&mut self, patcher: &ContextPatcher) {
149 patcher.ops().iter().for_each(|op| match op {
150 ContextPatcherOp::Set { key, value } => {
151 if self.0.contains_key(&key) {
152 self.0.remove(&key);
153 }
154 self.0.insert(key.clone(), value.clone());
155 }
156 ContextPatcherOp::Remove { key } => {
157 self.0.remove(&key);
158 }
159 })
160 }
161
162 pub fn as_string(&self, key: &IdField) -> Option<String> {
164 self.0.get(key).and_then(|v| match v {
165 ContextValue::String { string } => Some(string.clone()),
166 _ => None,
167 })
168 }
169
170 pub fn as_number(&self, key: &IdField) -> Option<i64> {
172 self.0.get(key).and_then(|v| match v {
173 ContextValue::Number { number } => Some(number.clone()),
174 _ => None,
175 })
176 }
177
178 pub fn as_id_field(&self, key: &IdField) -> Option<IdField> {
180 self.0.get(key).and_then(|v| match v {
181 ContextValue::IdField { id_field } => Some(id_field.clone()),
182 _ => None,
183 })
184 }
185
186 pub fn as_float(&self, key: &IdField) -> Option<f64> {
188 self.0.get(key).and_then(|v| match v {
189 ContextValue::Float { float } => Some(float.clone()),
190 _ => None,
191 })
192 }
193
194 pub fn as_date(&self, key: &IdField) -> Option<NaiveDate> {
196 self.0.get(key).and_then(|v| match v {
197 ContextValue::Date { date } => Some(date.clone()),
198 _ => None,
199 })
200 }
201
202 pub fn as_date_time(&self, key: &IdField) -> Option<DateTime<Utc>> {
204 self.0.get(key).and_then(|v| match v {
205 ContextValue::DateTime { datetime } => Some(datetime.clone()),
206 _ => None,
207 })
208 }
209
210 pub fn as_bool(&self, key: &IdField) -> Option<bool> {
212 self.0.get(key).and_then(|v| match v {
213 ContextValue::Boolean { boolean } => Some(boolean.clone()),
214 _ => None,
215 })
216 }
217
218 pub fn as_object(&self, key: &IdField) -> Option<Value> {
220 self.0.get(key).and_then(|v| match v {
221 ContextValue::Object { object } => Some(object.clone()),
222 _ => None,
223 })
224 }
225
226 pub fn as_array(&self, key: &IdField) -> Option<Vec<ContextValue>> {
228 self.0.get(key).and_then(|v| match v {
229 ContextValue::Array { array } => Some(array.clone()),
230 _ => None,
231 })
232 }
233}
234
235pub struct ContextMapBuilder {
237 context_map: ContextMap,
238}
239
240impl ContextMapBuilder {
241 pub fn new() -> Self {
243 Self {
244 context_map: ContextMap::default(),
245 }
246 }
247
248 pub fn from(context_map: ContextMap) -> Self {
250 Self { context_map }
251 }
252
253 pub fn set(mut self, key: IdField, value: ContextValue) -> Self {
255 self.context_map.0.insert(key, value);
256 self
257 }
258
259 pub fn build(self) -> ContextMap {
261 self.context_map
262 }
263}