block_entities.rs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. use crate::entities::parser::NotEmptyStr;
  2. use flowy_derive::ProtoBuf;
  3. use flowy_error::ErrorCode;
  4. use grid_rev_model::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, Eq, PartialEq)]
  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<&mut RowRevision> for RowPB {
  56. fn from(rev: &mut RowRevision) -> Self {
  57. Self {
  58. block_id: rev.block_id.clone(),
  59. id: rev.id.clone(),
  60. height: rev.height,
  61. }
  62. }
  63. }
  64. impl std::convert::From<&Arc<RowRevision>> for RowPB {
  65. fn from(rev: &Arc<RowRevision>) -> Self {
  66. Self {
  67. block_id: rev.block_id.clone(),
  68. id: rev.id.clone(),
  69. height: rev.height,
  70. }
  71. }
  72. }
  73. #[derive(Debug, Default, ProtoBuf)]
  74. pub struct OptionalRowPB {
  75. #[pb(index = 1, one_of)]
  76. pub row: Option<RowPB>,
  77. }
  78. #[derive(Debug, Default, ProtoBuf)]
  79. pub struct RepeatedRowPB {
  80. #[pb(index = 1)]
  81. pub items: Vec<RowPB>,
  82. }
  83. impl std::convert::From<Vec<RowPB>> for RepeatedRowPB {
  84. fn from(items: Vec<RowPB>) -> Self {
  85. Self { items }
  86. }
  87. }
  88. /// [RepeatedBlockPB] contains list of [BlockPB]
  89. #[derive(Debug, Default, ProtoBuf)]
  90. pub struct RepeatedBlockPB {
  91. #[pb(index = 1)]
  92. pub items: Vec<BlockPB>,
  93. }
  94. impl std::convert::From<Vec<BlockPB>> for RepeatedBlockPB {
  95. fn from(items: Vec<BlockPB>) -> Self {
  96. Self { items }
  97. }
  98. }
  99. #[derive(Debug, Clone, Default, ProtoBuf)]
  100. pub struct InsertedRowPB {
  101. #[pb(index = 1)]
  102. pub row: RowPB,
  103. #[pb(index = 2, one_of)]
  104. pub index: Option<i32>,
  105. #[pb(index = 3)]
  106. pub is_new: bool,
  107. }
  108. impl InsertedRowPB {
  109. pub fn new(row: RowPB) -> Self {
  110. Self {
  111. row,
  112. index: None,
  113. is_new: false,
  114. }
  115. }
  116. pub fn with_index(row: RowPB, index: i32) -> Self {
  117. Self {
  118. row,
  119. index: Some(index),
  120. is_new: false,
  121. }
  122. }
  123. }
  124. impl std::convert::From<RowPB> for InsertedRowPB {
  125. fn from(row: RowPB) -> Self {
  126. Self {
  127. row,
  128. index: None,
  129. is_new: false,
  130. }
  131. }
  132. }
  133. impl std::convert::From<&RowRevision> for InsertedRowPB {
  134. fn from(row: &RowRevision) -> Self {
  135. let row_order = RowPB::from(row);
  136. Self::from(row_order)
  137. }
  138. }
  139. #[derive(Debug, Clone, Default, ProtoBuf)]
  140. pub struct UpdatedRowPB {
  141. #[pb(index = 1)]
  142. pub row: RowPB,
  143. // represents as the cells that were updated in this row.
  144. #[pb(index = 2)]
  145. pub field_ids: Vec<String>,
  146. }
  147. #[derive(Debug, Default, Clone, ProtoBuf)]
  148. pub struct GridBlockChangesetPB {
  149. #[pb(index = 1)]
  150. pub view_id: String,
  151. #[pb(index = 2)]
  152. pub inserted_rows: Vec<InsertedRowPB>,
  153. #[pb(index = 3)]
  154. pub deleted_rows: Vec<String>,
  155. #[pb(index = 4)]
  156. pub updated_rows: Vec<UpdatedRowPB>,
  157. #[pb(index = 5)]
  158. pub visible_rows: Vec<InsertedRowPB>,
  159. #[pb(index = 6)]
  160. pub invisible_rows: Vec<String>,
  161. }
  162. impl GridBlockChangesetPB {
  163. pub fn insert(view_id: String, inserted_rows: Vec<InsertedRowPB>) -> Self {
  164. Self {
  165. view_id,
  166. inserted_rows,
  167. ..Default::default()
  168. }
  169. }
  170. pub fn delete(block_id: &str, deleted_rows: Vec<String>) -> Self {
  171. Self {
  172. view_id: block_id.to_owned(),
  173. deleted_rows,
  174. ..Default::default()
  175. }
  176. }
  177. pub fn update(block_id: &str, updated_rows: Vec<UpdatedRowPB>) -> Self {
  178. Self {
  179. view_id: block_id.to_owned(),
  180. updated_rows,
  181. ..Default::default()
  182. }
  183. }
  184. }
  185. /// [QueryBlocksPayloadPB] is used to query the data of the block that belongs to the grid whose
  186. /// id is grid_id.
  187. #[derive(ProtoBuf, Default)]
  188. pub struct QueryBlocksPayloadPB {
  189. #[pb(index = 1)]
  190. pub grid_id: String,
  191. #[pb(index = 2)]
  192. pub block_ids: Vec<String>,
  193. }
  194. pub struct QueryGridBlocksParams {
  195. pub grid_id: String,
  196. pub block_ids: Vec<String>,
  197. }
  198. impl TryInto<QueryGridBlocksParams> for QueryBlocksPayloadPB {
  199. type Error = ErrorCode;
  200. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  201. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  202. Ok(QueryGridBlocksParams {
  203. grid_id: grid_id.0,
  204. block_ids: self.block_ids,
  205. })
  206. }
  207. }