setting_entities.rs 4.1 KB

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