1use std::collections::BTreeMap;
2use std::hash::Hash;
3use std::ops::{Index, IndexMut};
4
5use crate::{Entity, IdentifiableEntity, Record, Value, VersionedEntity};
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct SmartList<T> {
9 pub data: Vec<T>,
10 pub total_count: Option<u64>,
11 pub aggregations: Record,
12 pub summary: Record,
13 pub facets: BTreeMap<String, SmartList<Record>>,
14 pub is_loaded: bool,
15}
16
17impl<T> SmartList<T> {
18 pub fn empty() -> Self {
19 let mut list = Self::new(Vec::new());
20 list.is_loaded = false;
21 list
22 }
23
24 pub fn into_value(self) -> Option<Value> where T: Entity {
25 let mut has_changes = false;
26 let mut items = Vec::new();
27 for entity in self.data.into_iter() {
28 if Entity::is_new(&entity)
29 || Entity::dirty_fields(&entity).is_some()
30 || Entity::is_marked_as_delete(&entity)
31 {
32 has_changes = true;
33 }
34 items.push(Value::object(Entity::into_record(entity)));
35 }
36
37 if self.is_loaded || has_changes {
38 Some(Value::List(items))
39 } else {
40 None
41 }
42 }
43
44 pub fn new(data: Vec<T>) -> Self {
45 Self {
46 data,
47 total_count: None,
48 aggregations: Record::new(),
49 summary: Record::new(),
50 facets: BTreeMap::new(),
51 is_loaded: true,
52 }
53 }
54
55 pub fn with_total_count(mut self, total_count: u64) -> Self {
56 self.total_count = Some(total_count);
57 self
58 }
59
60 pub fn with_aggregation(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
61 self.aggregations.insert(key.into(), value.into());
62 self
63 }
64
65 pub fn with_summary(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
66 self.summary.insert(key.into(), value.into());
67 self
68 }
69
70 pub fn with_facet(mut self, key: impl Into<String>, facet: SmartList<Record>) -> Self {
71 self.facets.insert(key.into(), facet);
72 self
73 }
74
75 pub fn add_facet(&mut self, key: impl Into<String>, facet: SmartList<Record>) {
76 self.facets.insert(key.into(), facet);
77 }
78
79 pub fn facets(&self) -> &BTreeMap<String, SmartList<Record>> {
80 &self.facets
81 }
82
83 pub fn facets_mut(&mut self) -> &mut BTreeMap<String, SmartList<Record>> {
84 &mut self.facets
85 }
86
87 pub fn facet(&self, key: &str) -> Option<&SmartList<Record>> {
88 self.facets.get(key)
89 }
90
91 pub fn facet_mut(&mut self, key: &str) -> Option<&mut SmartList<Record>> {
92 self.facets.get_mut(key)
93 }
94
95 pub fn remove_facet(&mut self, key: &str) -> Option<SmartList<Record>> {
96 self.facets.remove(key)
97 }
98
99 pub fn take_facets(&mut self) -> BTreeMap<String, SmartList<Record>> {
100 std::mem::take(&mut self.facets)
101 }
102
103 pub fn push(&mut self, value: T) {
104 self.data.push(value);
105 }
106
107 pub fn extend(&mut self, values: impl IntoIterator<Item = T>) {
108 self.data.extend(values);
109 }
110
111 pub fn set(&mut self, index: usize, value: T) -> Option<T> {
112 if index >= self.data.len() {
113 return None;
114 }
115 Some(std::mem::replace(&mut self.data[index], value))
116 }
117
118 pub fn get(&self, index: usize) -> Option<&T> {
119 self.data.get(index)
120 }
121
122 pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
123 self.data.get_mut(index)
124 }
125
126 pub fn last(&self) -> Option<&T> {
127 self.data.last()
128 }
129
130 pub fn len(&self) -> usize {
131 self.data.len()
132 }
133
134 pub fn is_empty(&self) -> bool {
135 self.data.is_empty()
136 }
137
138 pub fn first(&self) -> Option<&T> {
139 self.data.first()
140 }
141
142 pub fn iter(&self) -> std::slice::Iter<'_, T> {
143 self.data.iter()
144 }
145
146 pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
147 self.data.iter_mut()
148 }
149
150 pub fn as_slice(&self) -> &[T] {
151 &self.data
152 }
153
154 pub fn as_mut_slice(&mut self) -> &mut [T] {
155 &mut self.data
156 }
157
158 pub fn retain(&mut self, filter: impl FnMut(&T) -> bool) {
159 self.data.retain(filter);
160 }
161
162 pub fn total_count_or_len(&self) -> u64 {
163 self.total_count.unwrap_or(self.data.len() as u64)
164 }
165
166 pub fn aggregation(&self, key: &str) -> Option<&Value> {
167 self.aggregations.get(key)
168 }
169
170 pub fn summary(&self, key: &str) -> Option<&Value> {
171 self.summary.get(key)
172 }
173
174 pub fn aggregation_json(&self) -> serde_json::Value {
175 crate::record_to_json_value(&self.aggregations)
176 }
177
178 pub fn summary_json(&self) -> serde_json::Value {
179 crate::record_to_json_value(&self.summary)
180 }
181
182 pub fn into_vec(self) -> Vec<T> {
183 self.data
184 }
185
186 pub fn map<U>(self, mapper: impl FnMut(T) -> U) -> SmartList<U> {
187 SmartList {
188 data: self.data.into_iter().map(mapper).collect(),
189 total_count: self.total_count,
190 aggregations: self.aggregations,
191 summary: self.summary,
192 facets: self.facets,
193 is_loaded: self.is_loaded,
194 }
195 }
196
197 pub fn to_list<U>(&self, mapper: impl FnMut(&T) -> U) -> Vec<U> {
198 self.data.iter().map(mapper).collect()
199 }
200
201 pub fn to_set<U>(&self, mapper: impl FnMut(&T) -> U) -> std::collections::BTreeSet<U>
202 where
203 U: Ord,
204 {
205 self.data.iter().map(mapper).collect()
206 }
207
208 pub fn identity_map<K>(&self, mut key: impl FnMut(&T) -> K) -> BTreeMap<K, T>
209 where
210 K: Ord,
211 T: Clone,
212 {
213 self.data
214 .iter()
215 .map(|item| (key(item), item.clone()))
216 .collect()
217 }
218
219 pub fn group_by<K>(&self, mut key: impl FnMut(&T) -> K) -> BTreeMap<K, Vec<T>>
220 where
221 K: Ord,
222 T: Clone,
223 {
224 let mut groups = BTreeMap::new();
225 for item in &self.data {
226 groups
227 .entry(key(item))
228 .or_insert_with(Vec::new)
229 .push(item.clone());
230 }
231 groups
232 }
233
234 pub fn merge_by<K>(
235 &mut self,
236 incoming: impl IntoIterator<Item = T>,
237 mut key: impl FnMut(&T) -> K,
238 ) where
239 K: Eq + Hash,
240 {
241 let mut positions = self
242 .data
243 .iter()
244 .enumerate()
245 .map(|(index, item)| (key(item), index))
246 .collect::<std::collections::HashMap<_, _>>();
247 for item in incoming {
248 let item_key = key(&item);
249 if let Some(index) = positions.get(&item_key).copied() {
250 self.data[index] = item;
251 } else {
252 positions.insert(item_key, self.data.len());
253 self.data.push(item);
254 }
255 }
256 }
257
258 pub fn into_records(self) -> SmartList<Record>
259 where
260 T: Entity,
261 {
262 SmartList {
263 data: self.data.into_iter().map(Entity::into_record).collect(),
264 total_count: self.total_count,
265 aggregations: self.aggregations,
266 summary: self.summary,
267 facets: self.facets,
268 is_loaded: self.is_loaded,
269 }
270 }
271}
272
273impl<T> SmartList<T>
274where
275 T: IdentifiableEntity,
276{
277 pub fn ids(&self) -> Vec<Value> {
278 self.data.iter().map(IdentifiableEntity::id_value).collect()
279 }
280
281 pub fn map_by_id(&self) -> BTreeMap<String, T>
282 where
283 T: Clone,
284 {
285 self.data
286 .iter()
287 .map(|item| (id_key(&item.id_value()), item.clone()))
288 .collect()
289 }
290}
291
292impl<T> SmartList<T>
293where
294 T: VersionedEntity,
295{
296 pub fn versions(&self) -> Vec<i64> {
297 self.data.iter().map(VersionedEntity::version).collect()
298 }
299}
300
301impl<T> From<Vec<T>> for SmartList<T> {
302 fn from(data: Vec<T>) -> Self {
303 Self::new(data)
304 }
305}
306
307impl<T> Default for SmartList<T> {
308 fn default() -> Self {
309 Self::empty()
310 }
311}
312
313impl<T> IntoIterator for SmartList<T> {
314 type Item = T;
315 type IntoIter = std::vec::IntoIter<T>;
316
317 fn into_iter(self) -> Self::IntoIter {
318 self.data.into_iter()
319 }
320}
321
322impl<'a, T> IntoIterator for &'a SmartList<T> {
323 type Item = &'a T;
324 type IntoIter = std::slice::Iter<'a, T>;
325
326 fn into_iter(self) -> Self::IntoIter {
327 self.data.iter()
328 }
329}
330
331impl<'a, T> IntoIterator for &'a mut SmartList<T> {
332 type Item = &'a mut T;
333 type IntoIter = std::slice::IterMut<'a, T>;
334
335 fn into_iter(self) -> Self::IntoIter {
336 self.data.iter_mut()
337 }
338}
339
340impl<T> FromIterator<T> for SmartList<T> {
341 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
342 Self::new(iter.into_iter().collect())
343 }
344}
345
346impl<T> Extend<T> for SmartList<T> {
347 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
348 self.data.extend(iter);
349 }
350}
351
352impl<T> Index<usize> for SmartList<T> {
353 type Output = T;
354
355 fn index(&self, index: usize) -> &Self::Output {
356 &self.data[index]
357 }
358}
359
360impl<T> IndexMut<usize> for SmartList<T> {
361 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
362 &mut self.data[index]
363 }
364}
365
366impl<T> std::ops::Deref for SmartList<T> {
367 type Target = [T];
368
369 #[inline(always)]
370 fn deref(&self) -> &Self::Target {
371 &self.data
372 }
373}
374
375impl<T> std::ops::DerefMut for SmartList<T> {
376 #[inline(always)]
377 fn deref_mut(&mut self) -> &mut Self::Target {
378 &mut self.data
379 }
380}
381
382impl<T> From<SmartList<T>> for Vec<T> {
383 #[inline(always)]
384 fn from(list: SmartList<T>) -> Self {
385 list.data
386 }
387}
388
389
390fn id_key(value: &Value) -> String {
391 match value {
392 Value::Null => "null".to_owned(),
393 Value::Bool(value) => format!("b:{value}"),
394 Value::I64(value) => format!("i:{value}"),
395 Value::U64(value) => format!("u:{value}"),
396 Value::F64(value) => format!("f:{value}"),
397 Value::Decimal(value) => format!("decimal:{value}"),
398 Value::Text(value) => format!("t:{value}"),
399 Value::Json(value) => format!("j:{value}"),
400 Value::Date(value) => format!("date:{value}"),
401 Value::Timestamp(value) => format!("ts:{}", value.to_rfc3339()),
402 Value::Object(_) => "object".to_owned(),
403 Value::List(_) => "list".to_owned(),
404 }
405}