row_entities.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. use crate::entities::parser::NotEmptyStr;
  2. use crate::entities::LayoutTypePB;
  3. use database_model::RowRevision;
  4. use flowy_derive::ProtoBuf;
  5. use flowy_error::ErrorCode;
  6. use std::sync::Arc;
  7. /// [RowPB] Describes a row. Has the id of the parent Block. Has the metadata of the row.
  8. #[derive(Debug, Default, Clone, ProtoBuf, Eq, PartialEq)]
  9. pub struct RowPB {
  10. #[pb(index = 1)]
  11. pub block_id: String,
  12. #[pb(index = 2)]
  13. pub id: String,
  14. #[pb(index = 3)]
  15. pub height: i32,
  16. }
  17. impl RowPB {
  18. pub fn row_id(&self) -> &str {
  19. &self.id
  20. }
  21. pub fn block_id(&self) -> &str {
  22. &self.block_id
  23. }
  24. }
  25. impl std::convert::From<&RowRevision> for RowPB {
  26. fn from(rev: &RowRevision) -> Self {
  27. Self {
  28. block_id: rev.block_id.clone(),
  29. id: rev.id.clone(),
  30. height: rev.height,
  31. }
  32. }
  33. }
  34. impl std::convert::From<&mut RowRevision> for RowPB {
  35. fn from(rev: &mut RowRevision) -> Self {
  36. Self {
  37. block_id: rev.block_id.clone(),
  38. id: rev.id.clone(),
  39. height: rev.height,
  40. }
  41. }
  42. }
  43. impl std::convert::From<&Arc<RowRevision>> for RowPB {
  44. fn from(rev: &Arc<RowRevision>) -> Self {
  45. Self {
  46. block_id: rev.block_id.clone(),
  47. id: rev.id.clone(),
  48. height: rev.height,
  49. }
  50. }
  51. }
  52. #[derive(Debug, Default, ProtoBuf)]
  53. pub struct OptionalRowPB {
  54. #[pb(index = 1, one_of)]
  55. pub row: Option<RowPB>,
  56. }
  57. #[derive(Debug, Default, ProtoBuf)]
  58. pub struct RepeatedRowPB {
  59. #[pb(index = 1)]
  60. pub items: Vec<RowPB>,
  61. }
  62. impl std::convert::From<Vec<RowPB>> for RepeatedRowPB {
  63. fn from(items: Vec<RowPB>) -> Self {
  64. Self { items }
  65. }
  66. }
  67. #[derive(Debug, Clone, Default, ProtoBuf)]
  68. pub struct InsertedRowPB {
  69. #[pb(index = 1)]
  70. pub row: RowPB,
  71. #[pb(index = 2, one_of)]
  72. pub index: Option<i32>,
  73. #[pb(index = 3)]
  74. pub is_new: bool,
  75. }
  76. impl InsertedRowPB {
  77. pub fn new(row: RowPB) -> Self {
  78. Self {
  79. row,
  80. index: None,
  81. is_new: false,
  82. }
  83. }
  84. pub fn with_index(row: RowPB, index: i32) -> Self {
  85. Self {
  86. row,
  87. index: Some(index),
  88. is_new: false,
  89. }
  90. }
  91. }
  92. impl std::convert::From<RowPB> for InsertedRowPB {
  93. fn from(row: RowPB) -> Self {
  94. Self {
  95. row,
  96. index: None,
  97. is_new: false,
  98. }
  99. }
  100. }
  101. impl std::convert::From<&RowRevision> for InsertedRowPB {
  102. fn from(row: &RowRevision) -> Self {
  103. let row_order = RowPB::from(row);
  104. Self::from(row_order)
  105. }
  106. }
  107. #[derive(Debug, Clone, Default, ProtoBuf)]
  108. pub struct UpdatedRowPB {
  109. #[pb(index = 1)]
  110. pub row: RowPB,
  111. // represents as the cells that were updated in this row.
  112. #[pb(index = 2)]
  113. pub field_ids: Vec<String>,
  114. }
  115. #[derive(Debug, Default, Clone, ProtoBuf)]
  116. pub struct RowIdPB {
  117. #[pb(index = 1)]
  118. pub view_id: String,
  119. #[pb(index = 2)]
  120. pub row_id: String,
  121. }
  122. pub struct RowIdParams {
  123. pub view_id: String,
  124. pub row_id: String,
  125. }
  126. impl TryInto<RowIdParams> for RowIdPB {
  127. type Error = ErrorCode;
  128. fn try_into(self) -> Result<RowIdParams, Self::Error> {
  129. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?;
  130. let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  131. Ok(RowIdParams {
  132. view_id: view_id.0,
  133. row_id: row_id.0,
  134. })
  135. }
  136. }
  137. #[derive(Debug, Default, Clone, ProtoBuf)]
  138. pub struct BlockRowIdPB {
  139. #[pb(index = 1)]
  140. pub block_id: String,
  141. #[pb(index = 2)]
  142. pub row_id: String,
  143. }
  144. #[derive(ProtoBuf, Default)]
  145. pub struct CreateRowPayloadPB {
  146. #[pb(index = 1)]
  147. pub view_id: String,
  148. #[pb(index = 2, one_of)]
  149. pub start_row_id: Option<String>,
  150. }
  151. #[derive(Default)]
  152. pub struct CreateRowParams {
  153. pub view_id: String,
  154. pub start_row_id: Option<String>,
  155. pub group_id: Option<String>,
  156. pub layout: LayoutTypePB,
  157. }
  158. impl TryInto<CreateRowParams> for CreateRowPayloadPB {
  159. type Error = ErrorCode;
  160. fn try_into(self) -> Result<CreateRowParams, Self::Error> {
  161. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::ViewIdIsInvalid)?;
  162. Ok(CreateRowParams {
  163. view_id: view_id.0,
  164. start_row_id: self.start_row_id,
  165. group_id: None,
  166. layout: LayoutTypePB::Grid,
  167. })
  168. }
  169. }