block_entities.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /// [BlockPB] contains list of row ids. The rows here does not contain any data, just the id
  7. /// of the row. Check out [RowPB] 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 BlockPB {
  15. #[pb(index = 1)]
  16. pub id: String,
  17. #[pb(index = 2)]
  18. pub rows: Vec<RowPB>,
  19. }
  20. impl BlockPB {
  21. pub fn new(block_id: &str, rows: Vec<RowPB>) -> Self {
  22. Self {
  23. id: block_id.to_owned(),
  24. rows,
  25. }
  26. }
  27. }
  28. /// [RowPB] 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 RowPB {
  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 RowPB {
  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 RowPB {
  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 RowPB {
  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<RowPB>,
  68. }
  69. #[derive(Debug, Default, ProtoBuf)]
  70. pub struct RepeatedRowPB {
  71. #[pb(index = 1)]
  72. pub items: Vec<RowPB>,
  73. }
  74. impl std::convert::From<Vec<RowPB>> for RepeatedRowPB {
  75. fn from(items: Vec<RowPB>) -> Self {
  76. Self { items }
  77. }
  78. }
  79. /// [RepeatedBlockPB] contains list of [BlockPB]
  80. #[derive(Debug, Default, ProtoBuf)]
  81. pub struct RepeatedBlockPB {
  82. #[pb(index = 1)]
  83. pub items: Vec<BlockPB>,
  84. }
  85. impl std::convert::From<Vec<BlockPB>> for RepeatedBlockPB {
  86. fn from(items: Vec<BlockPB>) -> Self {
  87. Self { items }
  88. }
  89. }
  90. #[derive(Debug, Clone, Default, ProtoBuf)]
  91. pub struct InsertedRowPB {
  92. #[pb(index = 1)]
  93. pub row: RowPB,
  94. #[pb(index = 2, one_of)]
  95. pub index: Option<i32>,
  96. }
  97. impl InsertedRowPB {
  98. pub fn new(row: RowPB) -> Self {
  99. Self { row, index: None }
  100. }
  101. }
  102. impl std::convert::From<RowPB> for InsertedRowPB {
  103. fn from(row: RowPB) -> Self {
  104. Self { row, index: None }
  105. }
  106. }
  107. impl std::convert::From<&RowRevision> for InsertedRowPB {
  108. fn from(row: &RowRevision) -> Self {
  109. let row_order = RowPB::from(row);
  110. Self::from(row_order)
  111. }
  112. }
  113. #[derive(Debug, Default, ProtoBuf)]
  114. pub struct GridBlockChangesetPB {
  115. #[pb(index = 1)]
  116. pub block_id: String,
  117. #[pb(index = 2)]
  118. pub inserted_rows: Vec<InsertedRowPB>,
  119. #[pb(index = 3)]
  120. pub deleted_rows: Vec<String>,
  121. #[pb(index = 4)]
  122. pub updated_rows: Vec<RowPB>,
  123. #[pb(index = 5)]
  124. pub visible_rows: Vec<String>,
  125. #[pb(index = 6)]
  126. pub hide_rows: Vec<String>,
  127. }
  128. impl GridBlockChangesetPB {
  129. pub fn insert(block_id: String, inserted_rows: Vec<InsertedRowPB>) -> Self {
  130. Self {
  131. block_id,
  132. inserted_rows,
  133. ..Default::default()
  134. }
  135. }
  136. pub fn delete(block_id: &str, deleted_rows: Vec<String>) -> Self {
  137. Self {
  138. block_id: block_id.to_owned(),
  139. deleted_rows,
  140. ..Default::default()
  141. }
  142. }
  143. pub fn update(block_id: &str, updated_rows: Vec<RowPB>) -> Self {
  144. Self {
  145. block_id: block_id.to_owned(),
  146. updated_rows,
  147. ..Default::default()
  148. }
  149. }
  150. }
  151. /// [QueryBlocksPayloadPB] is used to query the data of the block that belongs to the grid whose
  152. /// id is grid_id.
  153. #[derive(ProtoBuf, Default)]
  154. pub struct QueryBlocksPayloadPB {
  155. #[pb(index = 1)]
  156. pub grid_id: String,
  157. #[pb(index = 2)]
  158. pub block_ids: Vec<String>,
  159. }
  160. pub struct QueryGridBlocksParams {
  161. pub grid_id: String,
  162. pub block_ids: Vec<String>,
  163. }
  164. impl TryInto<QueryGridBlocksParams> for QueryBlocksPayloadPB {
  165. type Error = ErrorCode;
  166. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  167. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  168. Ok(QueryGridBlocksParams {
  169. grid_id: grid_id.0,
  170. block_ids: self.block_ids,
  171. })
  172. }
  173. }