grid_entities.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. use crate::entities::parser::NotEmptyStr;
  2. use crate::entities::{FieldIdPB, RowPB};
  3. use flowy_derive::ProtoBuf;
  4. use flowy_error::ErrorCode;
  5. /// [GridPB] describes how many fields and blocks the grid has
  6. #[derive(Debug, Clone, Default, ProtoBuf)]
  7. pub struct GridPB {
  8. #[pb(index = 1)]
  9. pub id: String,
  10. #[pb(index = 2)]
  11. pub fields: Vec<FieldIdPB>,
  12. #[pb(index = 3)]
  13. pub rows: Vec<RowPB>,
  14. }
  15. #[derive(ProtoBuf, Default)]
  16. pub struct CreateGridPayloadPB {
  17. #[pb(index = 1)]
  18. pub name: String,
  19. }
  20. #[derive(Clone, ProtoBuf, Default, Debug)]
  21. pub struct GridIdPB {
  22. #[pb(index = 1)]
  23. pub value: String,
  24. }
  25. impl AsRef<str> for GridIdPB {
  26. fn as_ref(&self) -> &str {
  27. &self.value
  28. }
  29. }
  30. #[derive(Clone, ProtoBuf, Default, Debug)]
  31. pub struct GridBlockIdPB {
  32. #[pb(index = 1)]
  33. pub value: String,
  34. }
  35. impl AsRef<str> for GridBlockIdPB {
  36. fn as_ref(&self) -> &str {
  37. &self.value
  38. }
  39. }
  40. impl std::convert::From<&str> for GridBlockIdPB {
  41. fn from(s: &str) -> Self {
  42. GridBlockIdPB { value: s.to_owned() }
  43. }
  44. }
  45. #[derive(Debug, Clone, Default, ProtoBuf)]
  46. pub struct MoveFieldPayloadPB {
  47. #[pb(index = 1)]
  48. pub grid_id: String,
  49. #[pb(index = 2)]
  50. pub field_id: String,
  51. #[pb(index = 3)]
  52. pub from_index: i32,
  53. #[pb(index = 4)]
  54. pub to_index: i32,
  55. }
  56. #[derive(Clone)]
  57. pub struct MoveFieldParams {
  58. pub grid_id: String,
  59. pub field_id: String,
  60. pub from_index: i32,
  61. pub to_index: i32,
  62. }
  63. impl TryInto<MoveFieldParams> for MoveFieldPayloadPB {
  64. type Error = ErrorCode;
  65. fn try_into(self) -> Result<MoveFieldParams, Self::Error> {
  66. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  67. let item_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::InvalidData)?;
  68. Ok(MoveFieldParams {
  69. grid_id: grid_id.0,
  70. field_id: item_id.0,
  71. from_index: self.from_index,
  72. to_index: self.to_index,
  73. })
  74. }
  75. }
  76. #[derive(Debug, Clone, Default, ProtoBuf)]
  77. pub struct MoveRowPayloadPB {
  78. #[pb(index = 1)]
  79. pub view_id: String,
  80. #[pb(index = 2)]
  81. pub from_row_id: String,
  82. #[pb(index = 4)]
  83. pub to_row_id: String,
  84. }
  85. pub struct MoveRowParams {
  86. pub view_id: String,
  87. pub from_row_id: String,
  88. pub to_row_id: String,
  89. }
  90. impl TryInto<MoveRowParams> for MoveRowPayloadPB {
  91. type Error = ErrorCode;
  92. fn try_into(self) -> Result<MoveRowParams, Self::Error> {
  93. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::GridViewIdIsEmpty)?;
  94. let from_row_id = NotEmptyStr::parse(self.from_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  95. let to_row_id = NotEmptyStr::parse(self.to_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  96. Ok(MoveRowParams {
  97. view_id: view_id.0,
  98. from_row_id: from_row_id.0,
  99. to_row_id: to_row_id.0,
  100. })
  101. }
  102. }
  103. #[derive(Debug, Clone, Default, ProtoBuf)]
  104. pub struct MoveGroupRowPayloadPB {
  105. #[pb(index = 1)]
  106. pub view_id: String,
  107. #[pb(index = 2)]
  108. pub from_row_id: String,
  109. #[pb(index = 3)]
  110. pub to_group_id: String,
  111. #[pb(index = 4, one_of)]
  112. pub to_row_id: Option<String>,
  113. }
  114. pub struct MoveGroupRowParams {
  115. pub view_id: String,
  116. pub from_row_id: String,
  117. pub to_group_id: String,
  118. pub to_row_id: Option<String>,
  119. }
  120. impl TryInto<MoveGroupRowParams> for MoveGroupRowPayloadPB {
  121. type Error = ErrorCode;
  122. fn try_into(self) -> Result<MoveGroupRowParams, Self::Error> {
  123. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::GridViewIdIsEmpty)?;
  124. let from_row_id = NotEmptyStr::parse(self.from_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  125. let to_group_id = NotEmptyStr::parse(self.to_group_id).map_err(|_| ErrorCode::GroupIdIsEmpty)?;
  126. let to_row_id = match self.to_row_id {
  127. None => None,
  128. Some(to_row_id) => Some(NotEmptyStr::parse(to_row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?.0),
  129. };
  130. Ok(MoveGroupRowParams {
  131. view_id: view_id.0,
  132. from_row_id: from_row_id.0,
  133. to_group_id: to_group_id.0,
  134. to_row_id,
  135. })
  136. }
  137. }