143 lines
4.5 KiB
Rust
Raw Normal View History

use crate::revision::{FieldRevision, FieldTypeRevision, FilterConfigurationRevision, GroupConfigurationRevision};
2022-06-19 21:10:07 +08:00
use indexmap::IndexMap;
2022-06-21 16:56:50 +08:00
use nanoid::nanoid;
2022-06-15 17:24:46 +08:00
use serde::{Deserialize, Serialize};
2022-07-10 17:06:36 +08:00
use std::collections::HashMap;
2022-08-11 10:08:42 +08:00
use std::fmt::Debug;
2022-06-30 23:00:03 +08:00
use std::sync::Arc;
2022-06-21 16:56:50 +08:00
pub fn gen_grid_filter_id() -> String {
nanoid!(6)
}
pub fn gen_grid_group_id() -> String {
nanoid!(6)
}
#[allow(dead_code)]
2022-06-21 16:56:50 +08:00
pub fn gen_grid_sort_id() -> String {
nanoid!(6)
}
2022-06-15 17:24:46 +08:00
2022-08-14 21:09:18 +08:00
pub type FilterConfiguration = Configuration<FilterConfigurationRevision>;
2022-08-11 21:18:27 +08:00
pub type FilterConfigurationsByFieldId = HashMap<String, Vec<Arc<FilterConfigurationRevision>>>;
//
2022-08-14 21:09:18 +08:00
pub type GroupConfiguration = Configuration<GroupConfigurationRevision>;
2022-08-11 21:18:27 +08:00
pub type GroupConfigurationsByFieldId = HashMap<String, Vec<Arc<GroupConfigurationRevision>>>;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(transparent)]
2022-08-14 21:09:18 +08:00
pub struct Configuration<T>
where
T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
/// Key: field_id
/// Value: this value contains key/value.
/// Key: FieldType,
/// Value: the corresponding objects.
#[serde(with = "indexmap::serde_seq")]
2022-08-14 21:09:18 +08:00
inner: IndexMap<String, ObjectIndexMap<T>>,
}
2022-08-14 21:09:18 +08:00
impl<T> Configuration<T>
where
T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
2022-08-14 21:09:18 +08:00
pub fn get_mut_objects(&mut self, field_id: &str, field_type: &FieldTypeRevision) -> Option<&mut Vec<Arc<T>>> {
let value = self
.inner
2022-08-14 21:09:18 +08:00
.get_mut(field_id)
.and_then(|object_rev_map| object_rev_map.get_mut(field_type));
if value.is_none() {
tracing::warn!("[Configuration] Can't find the {:?} with", std::any::type_name::<T>());
}
value
2022-06-30 23:00:03 +08:00
}
2022-08-14 21:09:18 +08:00
pub fn get_objects(&self, field_id: &str, field_type_rev: &FieldTypeRevision) -> Option<Vec<Arc<T>>> {
self.inner
2022-08-14 21:09:18 +08:00
.get(field_id)
.and_then(|object_rev_map| object_rev_map.get(field_type_rev))
2022-06-30 23:00:03 +08:00
.cloned()
}
pub fn get_objects_by_field_revs(&self, field_revs: &[Arc<FieldRevision>]) -> Option<HashMap<String, Vec<Arc<T>>>> {
// Get the objects according to the FieldType, so we need iterate the field_revs.
let objects_by_field_id = field_revs
.iter()
.flat_map(|field_rev| {
let field_type = &field_rev.ty;
let field_id = &field_rev.id;
2022-08-14 21:09:18 +08:00
let object_rev_map = self.inner.get(field_id)?;
let objects: Vec<Arc<T>> = object_rev_map.get(field_type)?.clone();
Some((field_rev.id.clone(), objects))
})
.collect::<HashMap<String, Vec<Arc<T>>>>();
Some(objects_by_field_id)
}
pub fn get_all_objects(&self) -> Vec<Arc<T>> {
self.inner.values().map(|map| map.all_objects()).flatten().collect()
}
2022-08-22 16:16:15 +08:00
/// add object to the end of the list
pub fn add_object(&mut self, field_id: &str, field_type: &FieldTypeRevision, object: T) {
2022-08-14 21:09:18 +08:00
let object_rev_map = self
.inner
2022-06-30 23:00:03 +08:00
.entry(field_id.to_string())
2022-08-14 21:09:18 +08:00
.or_insert_with(ObjectIndexMap::<T>::new);
2022-06-30 23:00:03 +08:00
object_rev_map
.entry(field_type.to_owned())
2022-06-30 23:00:03 +08:00
.or_insert_with(Vec::new)
.push(Arc::new(object))
2022-06-30 23:00:03 +08:00
}
2022-08-11 21:18:27 +08:00
2022-08-22 16:16:15 +08:00
pub fn clear(&mut self) {
2022-08-14 21:09:18 +08:00
self.inner.clear()
2022-08-11 21:18:27 +08:00
}
2022-06-30 23:00:03 +08:00
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
2022-06-30 23:00:03 +08:00
#[serde(transparent)]
2022-08-14 21:09:18 +08:00
pub struct ObjectIndexMap<T>
2022-08-11 10:08:42 +08:00
where
T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
2022-08-11 10:08:42 +08:00
{
2022-06-30 23:00:03 +08:00
#[serde(with = "indexmap::serde_seq")]
2022-08-11 10:08:42 +08:00
pub object_by_field_type: IndexMap<FieldTypeRevision, Vec<Arc<T>>>,
2022-06-30 23:00:03 +08:00
}
2022-08-14 21:09:18 +08:00
impl<T> ObjectIndexMap<T>
2022-08-11 10:08:42 +08:00
where
T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
2022-08-11 10:08:42 +08:00
{
2022-06-30 23:00:03 +08:00
pub fn new() -> Self {
2022-08-14 21:09:18 +08:00
ObjectIndexMap::default()
2022-06-30 23:00:03 +08:00
}
pub fn all_objects(&self) -> Vec<Arc<T>> {
self.object_by_field_type.values().cloned().flatten().collect()
}
2022-06-30 23:00:03 +08:00
}
2022-08-14 21:09:18 +08:00
impl<T> std::ops::Deref for ObjectIndexMap<T>
2022-08-11 10:08:42 +08:00
where
T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
2022-08-11 10:08:42 +08:00
{
type Target = IndexMap<FieldTypeRevision, Vec<Arc<T>>>;
2022-06-30 23:00:03 +08:00
fn deref(&self) -> &Self::Target {
2022-08-11 10:08:42 +08:00
&self.object_by_field_type
2022-06-30 23:00:03 +08:00
}
}
2022-08-14 21:09:18 +08:00
impl<T> std::ops::DerefMut for ObjectIndexMap<T>
2022-08-11 10:08:42 +08:00
where
T: Debug + Clone + Default + serde::Serialize + serde::de::DeserializeOwned + 'static,
2022-08-11 10:08:42 +08:00
{
2022-06-30 23:00:03 +08:00
fn deref_mut(&mut self) -> &mut Self::Target {
2022-08-11 10:08:42 +08:00
&mut self.object_by_field_type
2022-06-19 21:10:07 +08:00
}
}