158 lines
4.4 KiB
Rust
Raw Normal View History

2022-06-21 16:56:50 +08:00
use crate::entities::{
2022-07-10 17:06:36 +08:00
CreateGridFilterPayload, CreateGridGroupPayload, CreateGridSortPayload, DeleteFilterPayload, GridFilter, GridGroup,
GridSort, RepeatedGridFilter, RepeatedGridGroup, RepeatedGridSort,
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;
2022-07-10 17:06:36 +08:00
use flowy_grid_data_model::revision::{GridLayoutRevision, GridSettingRevision};
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
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
2022-06-19 21:10:07 +08:00
pub struct GridSetting {
2022-06-15 17:24:46 +08:00
#[pb(index = 1)]
2022-07-10 17:06:36 +08:00
pub layouts: Vec<GridLayout>,
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)]
2022-07-10 17:06:36 +08:00
pub filters_by_field_id: HashMap<String, RepeatedGridFilter>,
#[pb(index = 4)]
pub groups_by_field_id: HashMap<String, RepeatedGridGroup>,
#[pb(index = 5)]
pub sorts_by_field_id: HashMap<String, RepeatedGridSort>,
}
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
pub struct GridLayout {
#[pb(index = 1)]
ty: GridLayoutType,
}
impl GridLayout {
pub fn all() -> Vec<GridLayout> {
let mut layouts = vec![];
for layout_ty in GridLayoutType::iter() {
layouts.push(GridLayout { ty: layout_ty })
}
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-06-19 21:10:07 +08:00
pub struct GridSettingChangesetPayload {
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-06-21 16:56:50 +08:00
pub insert_filter: Option<CreateGridFilterPayload>,
2022-06-15 17:24:46 +08:00
#[pb(index = 4, one_of)]
2022-06-30 23:00:03 +08:00
pub delete_filter: Option<DeleteFilterPayload>,
2022-06-19 21:10:07 +08:00
#[pb(index = 5, one_of)]
2022-06-21 16:56:50 +08:00
pub insert_group: Option<CreateGridGroupPayload>,
#[pb(index = 6, one_of)]
pub delete_group: Option<String>,
#[pb(index = 7, one_of)]
pub insert_sort: Option<CreateGridSortPayload>,
#[pb(index = 8, one_of)]
pub delete_sort: Option<String>,
2022-06-15 17:24:46 +08:00
}
2022-06-19 21:10:07 +08:00
impl TryInto<GridSettingChangesetParams> for GridSettingChangesetPayload {
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-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-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
})
}
}