setting_rev.rs 4.7 KB

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