258 lines
7.6 KiB
Rust
Raw Normal View History

2022-07-10 17:06:36 +08:00
use crate::revision::{FieldRevision, FieldTypeRevision};
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};
use serde_repr::*;
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)
}
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>>>;
//
2022-08-14 23:01:53 +08:00
pub type SortConfiguration = Configuration<SortConfigurationRevision>;
2022-08-11 21:18:27 +08:00
pub type SortConfigurationsByFieldId = HashMap<String, Vec<Arc<SortConfigurationRevision>>>;
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
2022-08-14 21:09:18 +08:00
pub struct SettingRevision {
pub layout: LayoutRevision,
2022-07-10 17:06:36 +08:00
2022-08-14 21:09:18 +08:00
pub filters: FilterConfiguration,
2022-06-15 17:24:46 +08:00
#[serde(default)]
2022-08-14 21:09:18 +08:00
pub groups: GroupConfiguration,
2022-06-19 21:10:07 +08:00
#[serde(skip)]
2022-08-14 23:01:53 +08:00
pub sorts: SortConfiguration,
2022-06-15 17:24:46 +08:00
}
2022-08-11 10:08:42 +08:00
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
2022-08-14 21:09:18 +08:00
pub enum LayoutRevision {
2022-08-11 10:08:42 +08:00
Table = 0,
Board = 1,
}
2022-08-14 21:09:18 +08:00
impl ToString for LayoutRevision {
2022-08-11 10:08:42 +08:00
fn to_string(&self) -> String {
let layout_rev = self.clone() as u8;
layout_rev.to_string()
}
}
2022-08-14 21:09:18 +08:00
impl std::default::Default for LayoutRevision {
2022-08-11 10:08:42 +08:00
fn default() -> Self {
2022-08-14 21:09:18 +08:00
LayoutRevision::Table
2022-08-11 10:08:42 +08:00
}
}
2022-08-14 21:09:18 +08:00
impl SettingRevision {
2022-08-11 21:18:27 +08:00
pub fn get_all_groups(&self, field_revs: &[Arc<FieldRevision>]) -> Option<GroupConfigurationsByFieldId> {
2022-08-14 21:09:18 +08:00
self.groups.get_all_objects(field_revs)
}
pub fn get_groups(
&self,
field_id: &str,
field_type_rev: &FieldTypeRevision,
2022-08-11 21:18:27 +08:00
) -> Option<Vec<Arc<GroupConfigurationRevision>>> {
2022-08-14 21:09:18 +08:00
self.groups.get_objects(field_id, field_type_rev)
2022-07-10 17:06:36 +08:00
}
2022-08-11 10:08:42 +08:00
pub fn get_mut_groups(
&mut self,
field_id: &str,
field_type: &FieldTypeRevision,
2022-08-11 21:18:27 +08:00
) -> Option<&mut Vec<Arc<GroupConfigurationRevision>>> {
2022-08-14 21:09:18 +08:00
self.groups.get_mut_objects(field_id, field_type)
2022-08-11 10:08:42 +08:00
}
pub fn insert_group(
&mut self,
field_id: &str,
field_type: &FieldTypeRevision,
2022-08-11 21:18:27 +08:00
group_rev: GroupConfigurationRevision,
2022-08-11 10:08:42 +08:00
) {
2022-08-14 21:09:18 +08:00
// only one group can be set
self.groups.remove_all();
self.groups.insert_object(field_id, field_type, group_rev);
}
2022-08-11 10:08:42 +08:00
2022-08-11 21:18:27 +08:00
pub fn get_all_filters(&self, field_revs: &[Arc<FieldRevision>]) -> Option<FilterConfigurationsByFieldId> {
2022-08-14 21:09:18 +08:00
self.filters.get_all_objects(field_revs)
2022-08-11 10:08:42 +08:00
}
pub fn get_filters(
&self,
field_id: &str,
field_type_rev: &FieldTypeRevision,
2022-08-11 21:18:27 +08:00
) -> Option<Vec<Arc<FilterConfigurationRevision>>> {
2022-08-14 21:09:18 +08:00
self.filters.get_objects(field_id, field_type_rev)
2022-07-10 17:06:36 +08:00
}
pub fn get_mut_filters(
&mut self,
field_id: &str,
field_type: &FieldTypeRevision,
2022-08-11 21:18:27 +08:00
) -> Option<&mut Vec<Arc<FilterConfigurationRevision>>> {
2022-08-14 21:09:18 +08:00
self.filters.get_mut_objects(field_id, field_type)
}
2022-07-10 17:06:36 +08:00
pub fn insert_filter(
&mut self,
field_id: &str,
field_type: &FieldTypeRevision,
2022-08-11 21:18:27 +08:00
filter_rev: FilterConfigurationRevision,
) {
2022-08-14 21:09:18 +08:00
self.filters.insert_object(field_id, field_type, filter_rev);
2022-07-10 17:06:36 +08:00
}
2022-08-11 21:18:27 +08:00
pub fn get_all_sort(&self) -> Option<SortConfigurationsByFieldId> {
None
2022-07-10 17:06:36 +08:00
}
}
2022-07-10 17:06:36 +08:00
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
2022-08-11 21:18:27 +08:00
pub struct SortConfigurationRevision {
pub id: String,
pub field_id: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
#[serde(transparent)]
2022-08-14 21:09:18 +08:00
pub struct Configuration<T>
where
T: Debug + Clone + Default + Eq + PartialEq + 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 + Eq + PartialEq + 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!("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()
}
2022-08-14 21:09:18 +08:00
pub fn get_all_objects(&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.field_type_rev;
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)
}
2022-08-14 21:09:18 +08:00
pub fn insert_object(&mut self, field_id: &str, field_type: &FieldTypeRevision, object: T) {
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-14 21:09:18 +08:00
pub fn remove_all(&mut self) {
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, Eq, PartialEq)]
#[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 + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
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 + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
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
}
}
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 + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
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 + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
{
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
}
}
2022-08-14 21:09:18 +08:00
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct GroupConfigurationRevision {
pub id: String,
pub field_id: String,
pub field_type_rev: FieldTypeRevision,
pub content: Option<Vec<u8>>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
pub struct FilterConfigurationRevision {
pub id: String,
pub field_id: String,
pub condition: u8,
pub content: Option<String>,
}