159 lines
4.5 KiB
Rust
Raw Normal View History

2022-06-21 16:56:50 +08:00
use crate::entities::{
2022-07-17 13:38:53 +08:00
CreateGridFilterPayloadPB, CreateGridGroupPayloadPB, CreateGridSortPayloadPB, DeleteFilterPayloadPB,
DeleteGroupPayloadPB, RepeatedGridConfigurationFilterPB, RepeatedGridGroupConfigurationPB, RepeatedGridSortPB,
2022-06-21 16:56:50 +08:00
};
2022-06-19 21:10:07 +08:00
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::ErrorCode;
use flowy_grid_data_model::parser::NotEmptyStr;
use flowy_grid_data_model::revision::GridLayoutRevision;
2022-08-11 15:00:36 +08:00
use flowy_sync::entities::grid::GridSettingChangesetParams;
2022-06-19 21:10:07 +08:00
use std::collections::HashMap;
2022-06-15 17:24:46 +08:00
use std::convert::TryInto;
2022-07-10 17:06:36 +08:00
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
2022-06-15 17:24:46 +08:00
/// [GridSettingPB] defines the setting options for the grid. Such as the filter, group, and sort.
2022-06-15 17:24:46 +08:00
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
2022-07-17 13:38:53 +08:00
pub struct GridSettingPB {
2022-06-15 17:24:46 +08:00
#[pb(index = 1)]
2022-07-17 13:38:53 +08:00
pub layouts: Vec<GridLayoutPB>,
2022-06-15 17:24:46 +08:00
#[pb(index = 2)]
2022-07-10 17:06:36 +08:00
pub current_layout_type: GridLayoutType,
2022-06-15 17:24:46 +08:00
#[pb(index = 3)]
pub filter_configuration_by_field_id: HashMap<String, RepeatedGridConfigurationFilterPB>,
2022-07-10 17:06:36 +08:00
#[pb(index = 4)]
pub group_configuration_by_field_id: HashMap<String, RepeatedGridGroupConfigurationPB>,
2022-07-10 17:06:36 +08:00
#[pb(index = 5)]
2022-07-17 13:38:53 +08:00
pub sorts_by_field_id: HashMap<String, RepeatedGridSortPB>,
2022-07-10 17:06:36 +08:00
}
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
2022-07-17 13:38:53 +08:00
pub struct GridLayoutPB {
2022-07-10 17:06:36 +08:00
#[pb(index = 1)]
ty: GridLayoutType,
}
2022-07-17 13:38:53 +08:00
impl GridLayoutPB {
pub fn all() -> Vec<GridLayoutPB> {
2022-07-10 17:06:36 +08:00
let mut layouts = vec![];
for layout_ty in GridLayoutType::iter() {
2022-07-17 13:38:53 +08:00
layouts.push(GridLayoutPB { ty: layout_ty })
2022-07-10 17:06:36 +08:00
}
layouts
}
2022-06-21 16:56:50 +08:00
}
2022-07-10 17:06:36 +08:00
#[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum, EnumIter)]
2022-06-19 21:10:07 +08:00
#[repr(u8)]
pub enum GridLayoutType {
Table = 0,
Board = 1,
}
impl std::default::Default for GridLayoutType {
fn default() -> Self {
GridLayoutType::Table
}
2022-06-15 17:24:46 +08:00
}
2022-06-21 16:56:50 +08:00
impl std::convert::From<GridLayoutRevision> for GridLayoutType {
fn from(rev: GridLayoutRevision) -> Self {
match rev {
GridLayoutRevision::Table => GridLayoutType::Table,
GridLayoutRevision::Board => GridLayoutType::Board,
}
}
2022-06-15 17:24:46 +08:00
}
2022-06-21 16:56:50 +08:00
impl std::convert::From<GridLayoutType> for GridLayoutRevision {
fn from(layout: GridLayoutType) -> Self {
match layout {
GridLayoutType::Table => GridLayoutRevision::Table,
GridLayoutType::Board => GridLayoutRevision::Board,
}
}
2022-06-15 17:24:46 +08:00
}
#[derive(Default, ProtoBuf)]
2022-07-17 13:38:53 +08:00
pub struct GridSettingChangesetPayloadPB {
2022-06-15 17:24:46 +08:00
#[pb(index = 1)]
pub grid_id: String,
2022-06-19 21:10:07 +08:00
#[pb(index = 2)]
pub layout_type: GridLayoutType,
2022-06-15 17:24:46 +08:00
#[pb(index = 3, one_of)]
2022-07-17 13:38:53 +08:00
pub insert_filter: Option<CreateGridFilterPayloadPB>,
2022-06-15 17:24:46 +08:00
#[pb(index = 4, one_of)]
2022-07-17 13:38:53 +08:00
pub delete_filter: Option<DeleteFilterPayloadPB>,
2022-06-19 21:10:07 +08:00
#[pb(index = 5, one_of)]
2022-07-17 13:38:53 +08:00
pub insert_group: Option<CreateGridGroupPayloadPB>,
2022-06-21 16:56:50 +08:00
#[pb(index = 6, one_of)]
2022-08-11 10:08:42 +08:00
pub delete_group: Option<DeleteGroupPayloadPB>,
2022-06-21 16:56:50 +08:00
#[pb(index = 7, one_of)]
2022-07-17 13:38:53 +08:00
pub insert_sort: Option<CreateGridSortPayloadPB>,
2022-06-21 16:56:50 +08:00
#[pb(index = 8, one_of)]
pub delete_sort: Option<String>,
2022-06-15 17:24:46 +08:00
}
2022-07-17 13:38:53 +08:00
impl TryInto<GridSettingChangesetParams> for GridSettingChangesetPayloadPB {
2022-06-15 17:24:46 +08:00
type Error = ErrorCode;
2022-06-19 21:10:07 +08:00
fn try_into(self) -> Result<GridSettingChangesetParams, Self::Error> {
2022-06-15 17:24:46 +08:00
let view_id = NotEmptyStr::parse(self.grid_id)
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
.0;
2022-06-21 16:56:50 +08:00
let insert_filter = match self.insert_filter {
None => None,
Some(payload) => Some(payload.try_into()?),
};
let delete_filter = match self.delete_filter {
None => None,
2022-06-30 23:00:03 +08:00
Some(payload) => Some(payload.try_into()?),
2022-06-21 16:56:50 +08:00
};
let insert_group = match self.insert_group {
Some(payload) => Some(payload.try_into()?),
None => None,
};
let delete_group = match self.delete_group {
2022-08-11 10:08:42 +08:00
Some(payload) => Some(payload.try_into()?),
2022-06-15 17:24:46 +08:00
None => None,
};
2022-06-21 16:56:50 +08:00
let insert_sort = match self.insert_sort {
2022-06-15 17:24:46 +08:00
None => None,
2022-06-21 16:56:50 +08:00
Some(payload) => Some(payload.try_into()?),
2022-06-15 17:24:46 +08:00
};
2022-06-21 16:56:50 +08:00
let delete_sort = match self.delete_sort {
2022-06-15 17:24:46 +08:00
None => None,
2022-06-21 16:56:50 +08:00
Some(filter_id) => Some(NotEmptyStr::parse(filter_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
2022-06-15 17:24:46 +08:00
};
2022-06-19 21:10:07 +08:00
Ok(GridSettingChangesetParams {
2022-06-20 09:37:52 +08:00
grid_id: view_id,
layout_type: self.layout_type.into(),
2022-06-21 16:56:50 +08:00
insert_filter,
delete_filter,
insert_group,
delete_group,
insert_sort,
delete_sort,
2022-06-15 17:24:46 +08:00
})
}
}