group.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. use crate::entities::{FieldType, RowPB};
  2. use flowy_derive::ProtoBuf;
  3. use flowy_error::ErrorCode;
  4. use flowy_grid_data_model::parser::NotEmptyStr;
  5. use flowy_grid_data_model::revision::GroupConfigurationRevision;
  6. use flowy_sync::entities::grid::{CreateGridGroupParams, DeleteGroupParams};
  7. use std::convert::TryInto;
  8. use std::sync::Arc;
  9. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  10. pub struct GridGroupConfigurationPB {
  11. #[pb(index = 1)]
  12. pub id: String,
  13. #[pb(index = 2)]
  14. pub group_field_id: String,
  15. }
  16. impl std::convert::From<&GroupConfigurationRevision> for GridGroupConfigurationPB {
  17. fn from(rev: &GroupConfigurationRevision) -> Self {
  18. GridGroupConfigurationPB {
  19. id: rev.id.clone(),
  20. group_field_id: rev.field_id.clone(),
  21. }
  22. }
  23. }
  24. #[derive(ProtoBuf, Debug, Default, Clone)]
  25. pub struct RepeatedGridGroupPB {
  26. #[pb(index = 1)]
  27. pub items: Vec<GroupPB>,
  28. }
  29. impl std::ops::Deref for RepeatedGridGroupPB {
  30. type Target = Vec<GroupPB>;
  31. fn deref(&self) -> &Self::Target {
  32. &self.items
  33. }
  34. }
  35. impl std::ops::DerefMut for RepeatedGridGroupPB {
  36. fn deref_mut(&mut self) -> &mut Self::Target {
  37. &mut self.items
  38. }
  39. }
  40. #[derive(ProtoBuf, Debug, Default, Clone)]
  41. pub struct GroupPB {
  42. #[pb(index = 1)]
  43. pub group_id: String,
  44. #[pb(index = 2)]
  45. pub desc: String,
  46. #[pb(index = 3)]
  47. pub rows: Vec<RowPB>,
  48. }
  49. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  50. pub struct RepeatedGridGroupConfigurationPB {
  51. #[pb(index = 1)]
  52. pub items: Vec<GridGroupConfigurationPB>,
  53. }
  54. impl std::convert::From<Vec<GridGroupConfigurationPB>> for RepeatedGridGroupConfigurationPB {
  55. fn from(items: Vec<GridGroupConfigurationPB>) -> Self {
  56. Self { items }
  57. }
  58. }
  59. impl std::convert::From<Vec<Arc<GroupConfigurationRevision>>> for RepeatedGridGroupConfigurationPB {
  60. fn from(revs: Vec<Arc<GroupConfigurationRevision>>) -> Self {
  61. RepeatedGridGroupConfigurationPB {
  62. items: revs.iter().map(|rev| rev.as_ref().into()).collect(),
  63. }
  64. }
  65. }
  66. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  67. pub struct CreateGridGroupPayloadPB {
  68. #[pb(index = 1)]
  69. pub field_id: String,
  70. #[pb(index = 2)]
  71. pub field_type: FieldType,
  72. #[pb(index = 3, one_of)]
  73. pub content: Option<Vec<u8>>,
  74. }
  75. impl TryInto<CreateGridGroupParams> for CreateGridGroupPayloadPB {
  76. type Error = ErrorCode;
  77. fn try_into(self) -> Result<CreateGridGroupParams, Self::Error> {
  78. let field_id = NotEmptyStr::parse(self.field_id)
  79. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  80. .0;
  81. Ok(CreateGridGroupParams {
  82. field_id,
  83. field_type_rev: self.field_type.into(),
  84. content: self.content,
  85. })
  86. }
  87. }
  88. #[derive(ProtoBuf, Debug, Default, Clone)]
  89. pub struct DeleteGroupPayloadPB {
  90. #[pb(index = 1)]
  91. pub field_id: String,
  92. #[pb(index = 2)]
  93. pub group_id: String,
  94. #[pb(index = 3)]
  95. pub field_type: FieldType,
  96. }
  97. impl TryInto<DeleteGroupParams> for DeleteGroupPayloadPB {
  98. type Error = ErrorCode;
  99. fn try_into(self) -> Result<DeleteGroupParams, Self::Error> {
  100. let field_id = NotEmptyStr::parse(self.field_id)
  101. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  102. .0;
  103. let group_id = NotEmptyStr::parse(self.group_id)
  104. .map_err(|_| ErrorCode::FieldIdIsEmpty)?
  105. .0;
  106. Ok(DeleteGroupParams {
  107. field_id,
  108. field_type_rev: self.field_type.into(),
  109. group_id,
  110. })
  111. }
  112. }