grid_setting_rev.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. use crate::{FieldRevision, FieldTypeRevision, FilterRevision, GroupConfigurationRevision};
  2. use indexmap::IndexMap;
  3. use nanoid::nanoid;
  4. use serde::{Deserialize, Serialize};
  5. use std::collections::HashMap;
  6. use std::fmt::Debug;
  7. use std::sync::Arc;
  8. pub fn gen_grid_filter_id() -> String {
  9. nanoid!(6)
  10. }
  11. pub fn gen_grid_group_id() -> String {
  12. nanoid!(6)
  13. }
  14. #[allow(dead_code)]
  15. pub fn gen_grid_sort_id() -> String {
  16. nanoid!(6)
  17. }
  18. pub type FilterConfiguration = Configuration<FilterRevision>;
  19. pub type FilterConfigurationsByFieldId = HashMap<String, Vec<Arc<FilterRevision>>>;
  20. //
  21. pub type GroupConfiguration = Configuration<GroupConfigurationRevision>;
  22. pub type GroupConfigurationsByFieldId = HashMap<String, Vec<Arc<GroupConfigurationRevision>>>;
  23. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  24. #[serde(transparent)]
  25. pub struct Configuration<T>
  26. where
  27. T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
  28. {
  29. /// Key: field_id
  30. /// Value: this value contains key/value.
  31. /// Key: FieldType,
  32. /// Value: the corresponding objects.
  33. #[serde(with = "indexmap::serde_seq")]
  34. inner: IndexMap<String, ObjectIndexMap<T>>,
  35. }
  36. impl<T> Configuration<T>
  37. where
  38. T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
  39. {
  40. pub fn get_mut_objects(&mut self, field_id: &str, field_type: &FieldTypeRevision) -> Option<&mut Vec<Arc<T>>> {
  41. let value = self
  42. .inner
  43. .get_mut(field_id)
  44. .and_then(|object_map| object_map.get_mut(field_type));
  45. if value.is_none() {
  46. eprintln!("[Configuration] Can't find the {:?} with", std::any::type_name::<T>());
  47. }
  48. value
  49. }
  50. pub fn get_object(
  51. &self,
  52. field_id: &str,
  53. field_type: &FieldTypeRevision,
  54. predicate: impl Fn(&Arc<T>) -> bool,
  55. ) -> Option<Arc<T>> {
  56. let objects = self.get_objects(field_id, field_type)?;
  57. let index = objects.iter().position(predicate)?;
  58. objects.get(index).cloned()
  59. }
  60. pub fn get_mut_object(
  61. &mut self,
  62. field_id: &str,
  63. field_type: &FieldTypeRevision,
  64. predicate: impl Fn(&Arc<T>) -> bool,
  65. ) -> Option<&mut Arc<T>> {
  66. let objects = self.get_mut_objects(field_id, field_type)?;
  67. let index = objects.iter().position(predicate)?;
  68. objects.get_mut(index)
  69. }
  70. pub fn get_objects(&self, field_id: &str, field_type_rev: &FieldTypeRevision) -> Option<Vec<Arc<T>>> {
  71. self.inner
  72. .get(field_id)
  73. .and_then(|object_map| object_map.get(field_type_rev))
  74. .cloned()
  75. }
  76. pub fn get_objects_by_field_revs(&self, field_revs: &[Arc<FieldRevision>]) -> Vec<Arc<T>> {
  77. // Get the objects according to the FieldType, so we need iterate the field_revs.
  78. let objects = field_revs
  79. .iter()
  80. .flat_map(|field_rev| {
  81. let field_type = &field_rev.ty;
  82. let field_id = &field_rev.id;
  83. let object_rev_map = self.inner.get(field_id)?;
  84. let objects: Vec<Arc<T>> = object_rev_map.get(field_type)?.clone();
  85. Some(objects)
  86. })
  87. .flatten()
  88. .collect::<Vec<Arc<T>>>();
  89. objects
  90. }
  91. pub fn get_all_objects(&self) -> Vec<Arc<T>> {
  92. self.inner.values().flat_map(|map| map.all_objects()).collect()
  93. }
  94. /// add object to the end of the list
  95. pub fn add_object(&mut self, field_id: &str, field_type: &FieldTypeRevision, object: T) {
  96. let object_rev_map = self
  97. .inner
  98. .entry(field_id.to_string())
  99. .or_insert_with(ObjectIndexMap::<T>::new);
  100. object_rev_map
  101. .entry(field_type.to_owned())
  102. .or_insert_with(Vec::new)
  103. .push(Arc::new(object))
  104. }
  105. pub fn clear(&mut self) {
  106. self.inner.clear()
  107. }
  108. }
  109. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  110. #[serde(transparent)]
  111. pub struct ObjectIndexMap<T>
  112. where
  113. T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
  114. {
  115. #[serde(with = "indexmap::serde_seq")]
  116. pub object_by_field_type: IndexMap<FieldTypeRevision, Vec<Arc<T>>>,
  117. }
  118. impl<T> ObjectIndexMap<T>
  119. where
  120. T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
  121. {
  122. pub fn new() -> Self {
  123. ObjectIndexMap::default()
  124. }
  125. pub fn all_objects(&self) -> Vec<Arc<T>> {
  126. self.object_by_field_type.values().flatten().cloned().collect()
  127. }
  128. }
  129. impl<T> std::ops::Deref for ObjectIndexMap<T>
  130. where
  131. T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
  132. {
  133. type Target = IndexMap<FieldTypeRevision, Vec<Arc<T>>>;
  134. fn deref(&self) -> &Self::Target {
  135. &self.object_by_field_type
  136. }
  137. }
  138. impl<T> std::ops::DerefMut for ObjectIndexMap<T>
  139. where
  140. T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
  141. {
  142. fn deref_mut(&mut self) -> &mut Self::Target {
  143. &mut self.object_by_field_type
  144. }
  145. }