179 lines
5.3 KiB
Rust
Raw Normal View History

2022-06-21 16:56:50 +08:00
use crate::entities::{
CreateGridFilterParams, CreateGridFilterPayload, CreateGridGroupParams, CreateGridGroupPayload,
2022-06-30 23:00:03 +08:00
CreateGridSortParams, CreateGridSortPayload, DeleteFilterParams, DeleteFilterPayload, RepeatedGridFilter,
RepeatedGridGroup, RepeatedGridSort,
2022-06-21 16:56:50 +08:00
};
use crate::parser::NotEmptyStr;
2022-06-30 23:00:03 +08:00
use crate::revision::GridLayoutRevision;
2022-06-19 21:10:07 +08:00
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
2022-06-15 17:24:46 +08:00
use flowy_error_code::ErrorCode;
2022-06-19 21:10:07 +08:00
use std::collections::HashMap;
2022-06-15 17:24:46 +08:00
use std::convert::TryInto;
#[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-06-21 16:56:50 +08:00
pub filters_by_layout_ty: HashMap<String, RepeatedGridFilter>,
2022-06-15 17:24:46 +08:00
#[pb(index = 2)]
2022-06-21 16:56:50 +08:00
pub groups_by_layout_ty: HashMap<String, RepeatedGridGroup>,
2022-06-15 17:24:46 +08:00
#[pb(index = 3)]
2022-06-21 16:56:50 +08:00
pub sorts_by_layout_ty: HashMap<String, RepeatedGridSort>,
}
2022-06-30 23:00:03 +08:00
//
// impl std::convert::From<&GridSettingRevision> for GridSetting {
// fn from(rev: &GridSettingRevision) -> Self {
// let filters_by_layout_ty: HashMap<String, RepeatedGridFilter> = rev
// .filters
// .iter()
// .map(|(layout_rev, filter_revs)| (layout_rev.to_string(), filter_revs.into()))
// .collect();
//
// let groups_by_layout_ty: HashMap<String, RepeatedGridGroup> = rev
// .groups
// .iter()
// .map(|(layout_rev, group_revs)| (layout_rev.to_string(), group_revs.into()))
// .collect();
//
// let sorts_by_layout_ty: HashMap<String, RepeatedGridSort> = rev
// .sorts
// .iter()
// .map(|(layout_rev, sort_revs)| (layout_rev.to_string(), sort_revs.into()))
// .collect();
//
// GridSetting {
// filters_by_layout_ty,
// groups_by_layout_ty,
// sorts_by_layout_ty,
// }
// }
// }
//
2022-06-19 21:10:07 +08:00
#[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum)]
#[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
pub struct GridSettingChangesetParams {
2022-06-20 09:37:52 +08:00
pub grid_id: String,
2022-06-19 21:10:07 +08:00
pub layout_type: GridLayoutType,
2022-06-21 16:56:50 +08:00
pub insert_filter: Option<CreateGridFilterParams>,
2022-06-30 23:00:03 +08:00
pub delete_filter: Option<DeleteFilterParams>,
2022-06-21 16:56:50 +08:00
pub insert_group: Option<CreateGridGroupParams>,
pub delete_group: Option<String>,
pub insert_sort: Option<CreateGridSortParams>,
pub delete_sort: Option<String>,
}
2022-06-29 16:55:52 +08:00
impl GridSettingChangesetParams {
pub fn is_filter_changed(&self) -> bool {
self.insert_filter.is_some() || self.delete_filter.is_some()
}
}
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,
2022-06-19 21:10:07 +08:00
layout_type: self.layout_type,
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
})
}
}