grid_setting_rev.rs 4.7 KB

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