block_entities.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. use flowy_derive::ProtoBuf;
  2. use flowy_error::ErrorCode;
  3. use flowy_grid_data_model::parser::NotEmptyStr;
  4. use flowy_grid_data_model::revision::RowRevision;
  5. use std::sync::Arc;
  6. /// [GridBlockPB] contains list of row ids. The rows here does not contain any data, just the id
  7. /// of the row. Check out [GridRowPB] for more details.
  8. ///
  9. ///
  10. /// A grid can have many rows. Rows are therefore grouped into Blocks in order to make
  11. /// things more efficient.
  12. /// |
  13. #[derive(Debug, Clone, Default, ProtoBuf)]
  14. pub struct GridBlockPB {
  15. #[pb(index = 1)]
  16. pub id: String,
  17. #[pb(index = 2)]
  18. pub rows: Vec<GridRowPB>,
  19. }
  20. impl GridBlockPB {
  21. pub fn new(block_id: &str, rows: Vec<GridRowPB>) -> Self {
  22. Self {
  23. id: block_id.to_owned(),
  24. rows,
  25. }
  26. }
  27. }
  28. /// [GridRowPB] Describes a row. Has the id of the parent Block. Has the metadata of the row.
  29. #[derive(Debug, Default, Clone, ProtoBuf)]
  30. pub struct GridRowPB {
  31. #[pb(index = 1)]
  32. pub block_id: String,
  33. #[pb(index = 2)]
  34. pub id: String,
  35. #[pb(index = 3)]
  36. pub height: i32,
  37. }
  38. impl GridRowPB {
  39. pub fn row_id(&self) -> &str {
  40. &self.id
  41. }
  42. pub fn block_id(&self) -> &str {
  43. &self.block_id
  44. }
  45. }
  46. impl std::convert::From<&RowRevision> for GridRowPB {
  47. fn from(rev: &RowRevision) -> Self {
  48. Self {
  49. block_id: rev.block_id.clone(),
  50. id: rev.id.clone(),
  51. height: rev.height,
  52. }
  53. }
  54. }
  55. impl std::convert::From<&Arc<RowRevision>> for GridRowPB {
  56. fn from(rev: &Arc<RowRevision>) -> Self {
  57. Self {
  58. block_id: rev.block_id.clone(),
  59. id: rev.id.clone(),
  60. height: rev.height,
  61. }
  62. }
  63. }
  64. #[derive(Debug, Default, ProtoBuf)]
  65. pub struct OptionalRowPB {
  66. #[pb(index = 1, one_of)]
  67. pub row: Option<GridRowPB>,
  68. }
  69. #[derive(Debug, Default, ProtoBuf)]
  70. pub struct RepeatedRowPB {
  71. #[pb(index = 1)]
  72. pub items: Vec<GridRowPB>,
  73. }
  74. impl std::convert::From<Vec<GridRowPB>> for RepeatedRowPB {
  75. fn from(items: Vec<GridRowPB>) -> Self {
  76. Self { items }
  77. }
  78. }
  79. /// [RepeatedGridBlockPB] contains list of [GridBlockPB]
  80. #[derive(Debug, Default, ProtoBuf)]
  81. pub struct RepeatedGridBlockPB {
  82. #[pb(index = 1)]
  83. pub items: Vec<GridBlockPB>,
  84. }
  85. impl std::convert::From<Vec<GridBlockPB>> for RepeatedGridBlockPB {
  86. fn from(items: Vec<GridBlockPB>) -> Self {
  87. Self { items }
  88. }
  89. }
  90. #[derive(Debug, Clone, Default, ProtoBuf)]
  91. pub struct InsertedRowPB {
  92. #[pb(index = 1)]
  93. pub block_id: String,
  94. #[pb(index = 2)]
  95. pub row_id: String,
  96. #[pb(index = 3)]
  97. pub height: i32,
  98. #[pb(index = 4, one_of)]
  99. pub index: Option<i32>,
  100. }
  101. #[derive(Debug, Default, ProtoBuf)]
  102. pub struct UpdatedRowPB {
  103. #[pb(index = 1)]
  104. pub block_id: String,
  105. #[pb(index = 2)]
  106. pub row_id: String,
  107. #[pb(index = 3)]
  108. pub row: GridRowPB,
  109. }
  110. impl UpdatedRowPB {
  111. pub fn new(row_rev: &RowRevision, row: GridRowPB) -> Self {
  112. Self {
  113. row_id: row_rev.id.clone(),
  114. block_id: row_rev.block_id.clone(),
  115. row,
  116. }
  117. }
  118. }
  119. impl std::convert::From<GridRowPB> for InsertedRowPB {
  120. fn from(row_info: GridRowPB) -> Self {
  121. Self {
  122. row_id: row_info.id,
  123. block_id: row_info.block_id,
  124. height: row_info.height,
  125. index: None,
  126. }
  127. }
  128. }
  129. impl std::convert::From<&RowRevision> for InsertedRowPB {
  130. fn from(row: &RowRevision) -> Self {
  131. let row_order = GridRowPB::from(row);
  132. Self::from(row_order)
  133. }
  134. }
  135. #[derive(Debug, Default, ProtoBuf)]
  136. pub struct GridBlockChangesetPB {
  137. #[pb(index = 1)]
  138. pub block_id: String,
  139. #[pb(index = 2)]
  140. pub inserted_rows: Vec<InsertedRowPB>,
  141. #[pb(index = 3)]
  142. pub deleted_rows: Vec<String>,
  143. #[pb(index = 4)]
  144. pub updated_rows: Vec<UpdatedRowPB>,
  145. #[pb(index = 5)]
  146. pub visible_rows: Vec<String>,
  147. #[pb(index = 6)]
  148. pub hide_rows: Vec<String>,
  149. }
  150. impl GridBlockChangesetPB {
  151. pub fn insert(block_id: &str, inserted_rows: Vec<InsertedRowPB>) -> Self {
  152. Self {
  153. block_id: block_id.to_owned(),
  154. inserted_rows,
  155. ..Default::default()
  156. }
  157. }
  158. pub fn delete(block_id: &str, deleted_rows: Vec<String>) -> Self {
  159. Self {
  160. block_id: block_id.to_owned(),
  161. deleted_rows,
  162. ..Default::default()
  163. }
  164. }
  165. pub fn update(block_id: &str, updated_rows: Vec<UpdatedRowPB>) -> Self {
  166. Self {
  167. block_id: block_id.to_owned(),
  168. updated_rows,
  169. ..Default::default()
  170. }
  171. }
  172. }
  173. /// [QueryGridBlocksPayloadPB] is used to query the data of the block that belongs to the grid whose
  174. /// id is grid_id.
  175. #[derive(ProtoBuf, Default)]
  176. pub struct QueryGridBlocksPayloadPB {
  177. #[pb(index = 1)]
  178. pub grid_id: String,
  179. #[pb(index = 2)]
  180. pub block_ids: Vec<String>,
  181. }
  182. pub struct QueryGridBlocksParams {
  183. pub grid_id: String,
  184. pub block_ids: Vec<String>,
  185. }
  186. impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayloadPB {
  187. type Error = ErrorCode;
  188. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  189. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  190. Ok(QueryGridBlocksParams {
  191. grid_id: grid_id.0,
  192. block_ids: self.block_ids,
  193. })
  194. }
  195. }