setting_entities.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. use crate::entities::{
  2. CreateGridFilterPayloadPB, CreateGridGroupPayloadPB, CreateGridSortPayloadPB, DeleteFilterPayloadPB,
  3. DeleteGroupPayloadPB, RepeatedGridConfigurationFilterPB, RepeatedGridGroupConfigurationPB, RepeatedGridSortPB,
  4. };
  5. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  6. use flowy_error::ErrorCode;
  7. use flowy_grid_data_model::parser::NotEmptyStr;
  8. use flowy_grid_data_model::revision::LayoutRevision;
  9. use flowy_sync::entities::grid::GridSettingChangesetParams;
  10. use std::collections::HashMap;
  11. use std::convert::TryInto;
  12. use strum::IntoEnumIterator;
  13. use strum_macros::EnumIter;
  14. /// [GridSettingPB] defines the setting options for the grid. Such as the filter, group, and sort.
  15. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  16. pub struct GridSettingPB {
  17. #[pb(index = 1)]
  18. pub layouts: Vec<GridLayoutPB>,
  19. #[pb(index = 2)]
  20. pub current_layout_type: Layout,
  21. #[pb(index = 3)]
  22. pub filter_configuration_by_field_id: HashMap<String, RepeatedGridConfigurationFilterPB>,
  23. #[pb(index = 4)]
  24. pub group_configuration_by_field_id: HashMap<String, RepeatedGridGroupConfigurationPB>,
  25. #[pb(index = 5)]
  26. pub sorts_by_field_id: HashMap<String, RepeatedGridSortPB>,
  27. }
  28. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  29. pub struct GridLayoutPB {
  30. #[pb(index = 1)]
  31. ty: Layout,
  32. }
  33. impl GridLayoutPB {
  34. pub fn all() -> Vec<GridLayoutPB> {
  35. let mut layouts = vec![];
  36. for layout_ty in Layout::iter() {
  37. layouts.push(GridLayoutPB { ty: layout_ty })
  38. }
  39. layouts
  40. }
  41. }
  42. #[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum, EnumIter)]
  43. #[repr(u8)]
  44. pub enum Layout {
  45. Table = 0,
  46. Board = 1,
  47. }
  48. impl std::default::Default for Layout {
  49. fn default() -> Self {
  50. Layout::Table
  51. }
  52. }
  53. impl std::convert::From<LayoutRevision> for Layout {
  54. fn from(rev: LayoutRevision) -> Self {
  55. match rev {
  56. LayoutRevision::Table => Layout::Table,
  57. LayoutRevision::Board => Layout::Board,
  58. }
  59. }
  60. }
  61. impl std::convert::From<Layout> for LayoutRevision {
  62. fn from(layout: Layout) -> Self {
  63. match layout {
  64. Layout::Table => LayoutRevision::Table,
  65. Layout::Board => LayoutRevision::Board,
  66. }
  67. }
  68. }
  69. #[derive(Default, ProtoBuf)]
  70. pub struct GridSettingChangesetPayloadPB {
  71. #[pb(index = 1)]
  72. pub grid_id: String,
  73. #[pb(index = 2)]
  74. pub layout_type: Layout,
  75. #[pb(index = 3, one_of)]
  76. pub insert_filter: Option<CreateGridFilterPayloadPB>,
  77. #[pb(index = 4, one_of)]
  78. pub delete_filter: Option<DeleteFilterPayloadPB>,
  79. #[pb(index = 5, one_of)]
  80. pub insert_group: Option<CreateGridGroupPayloadPB>,
  81. #[pb(index = 6, one_of)]
  82. pub delete_group: Option<DeleteGroupPayloadPB>,
  83. #[pb(index = 7, one_of)]
  84. pub insert_sort: Option<CreateGridSortPayloadPB>,
  85. #[pb(index = 8, one_of)]
  86. pub delete_sort: Option<String>,
  87. }
  88. impl TryInto<GridSettingChangesetParams> for GridSettingChangesetPayloadPB {
  89. type Error = ErrorCode;
  90. fn try_into(self) -> Result<GridSettingChangesetParams, Self::Error> {
  91. let view_id = NotEmptyStr::parse(self.grid_id)
  92. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  93. .0;
  94. let insert_filter = match self.insert_filter {
  95. None => None,
  96. Some(payload) => Some(payload.try_into()?),
  97. };
  98. let delete_filter = match self.delete_filter {
  99. None => None,
  100. Some(payload) => Some(payload.try_into()?),
  101. };
  102. let insert_group = match self.insert_group {
  103. Some(payload) => Some(payload.try_into()?),
  104. None => None,
  105. };
  106. let delete_group = match self.delete_group {
  107. Some(payload) => Some(payload.try_into()?),
  108. None => None,
  109. };
  110. let insert_sort = match self.insert_sort {
  111. None => None,
  112. Some(payload) => Some(payload.try_into()?),
  113. };
  114. let delete_sort = match self.delete_sort {
  115. None => None,
  116. Some(filter_id) => Some(NotEmptyStr::parse(filter_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
  117. };
  118. Ok(GridSettingChangesetParams {
  119. grid_id: view_id,
  120. layout_type: self.layout_type.into(),
  121. insert_filter,
  122. delete_filter,
  123. insert_group,
  124. delete_group,
  125. insert_sort,
  126. delete_sort,
  127. })
  128. }
  129. }