cell_entities.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use crate::entities::parser::NotEmptyStr;
  2. use crate::entities::FieldType;
  3. use database_model::{CellRevision, RowChangeset};
  4. use flowy_derive::ProtoBuf;
  5. use flowy_error::ErrorCode;
  6. use std::collections::HashMap;
  7. #[derive(ProtoBuf, Default)]
  8. pub struct CreateSelectOptionPayloadPB {
  9. #[pb(index = 1)]
  10. pub field_id: String,
  11. #[pb(index = 2)]
  12. pub view_id: String,
  13. #[pb(index = 3)]
  14. pub option_name: String,
  15. }
  16. pub struct CreateSelectOptionParams {
  17. pub field_id: String,
  18. pub view_id: String,
  19. pub option_name: String,
  20. }
  21. impl TryInto<CreateSelectOptionParams> for CreateSelectOptionPayloadPB {
  22. type Error = ErrorCode;
  23. fn try_into(self) -> Result<CreateSelectOptionParams, Self::Error> {
  24. let option_name =
  25. NotEmptyStr::parse(self.option_name).map_err(|_| ErrorCode::SelectOptionNameIsEmpty)?;
  26. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::ViewIdIsInvalid)?;
  27. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  28. Ok(CreateSelectOptionParams {
  29. field_id: field_id.0,
  30. option_name: option_name.0,
  31. view_id: view_id.0,
  32. })
  33. }
  34. }
  35. #[derive(Debug, Clone, Default, ProtoBuf)]
  36. pub struct CellIdPB {
  37. #[pb(index = 1)]
  38. pub view_id: String,
  39. #[pb(index = 2)]
  40. pub field_id: String,
  41. #[pb(index = 3)]
  42. pub row_id: String,
  43. }
  44. /// Represents as the cell identifier. It's used to locate the cell in corresponding
  45. /// view's row with the field id.
  46. pub struct CellIdParams {
  47. pub view_id: String,
  48. pub field_id: String,
  49. pub row_id: String,
  50. }
  51. impl TryInto<CellIdParams> for CellIdPB {
  52. type Error = ErrorCode;
  53. fn try_into(self) -> Result<CellIdParams, Self::Error> {
  54. let view_id = NotEmptyStr::parse(self.view_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?;
  55. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  56. let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  57. Ok(CellIdParams {
  58. view_id: view_id.0,
  59. field_id: field_id.0,
  60. row_id: row_id.0,
  61. })
  62. }
  63. }
  64. /// Represents as the data of the cell.
  65. #[derive(Debug, Default, ProtoBuf)]
  66. pub struct CellPB {
  67. #[pb(index = 1)]
  68. pub field_id: String,
  69. #[pb(index = 2)]
  70. pub row_id: String,
  71. /// Encoded the data using the helper struct `CellProtobufBlob`.
  72. /// Check out the `CellProtobufBlob` for more information.
  73. #[pb(index = 3)]
  74. pub data: Vec<u8>,
  75. /// the field_type will be None if the field with field_id is not found
  76. #[pb(index = 4, one_of)]
  77. pub field_type: Option<FieldType>,
  78. }
  79. impl CellPB {
  80. pub fn new(field_id: &str, row_id: &str, field_type: FieldType, data: Vec<u8>) -> Self {
  81. Self {
  82. field_id: field_id.to_owned(),
  83. row_id: row_id.to_string(),
  84. data,
  85. field_type: Some(field_type),
  86. }
  87. }
  88. pub fn empty(field_id: &str, row_id: &str) -> Self {
  89. Self {
  90. field_id: field_id.to_owned(),
  91. row_id: row_id.to_owned(),
  92. data: vec![],
  93. field_type: None,
  94. }
  95. }
  96. }
  97. #[derive(Debug, Default, ProtoBuf)]
  98. pub struct RepeatedCellPB {
  99. #[pb(index = 1)]
  100. pub items: Vec<CellPB>,
  101. }
  102. impl std::ops::Deref for RepeatedCellPB {
  103. type Target = Vec<CellPB>;
  104. fn deref(&self) -> &Self::Target {
  105. &self.items
  106. }
  107. }
  108. impl std::ops::DerefMut for RepeatedCellPB {
  109. fn deref_mut(&mut self) -> &mut Self::Target {
  110. &mut self.items
  111. }
  112. }
  113. impl std::convert::From<Vec<CellPB>> for RepeatedCellPB {
  114. fn from(items: Vec<CellPB>) -> Self {
  115. Self { items }
  116. }
  117. }
  118. ///
  119. #[derive(Debug, Clone, Default, ProtoBuf)]
  120. pub struct CellChangesetPB {
  121. #[pb(index = 1)]
  122. pub view_id: String,
  123. #[pb(index = 2)]
  124. pub row_id: String,
  125. #[pb(index = 3)]
  126. pub field_id: String,
  127. #[pb(index = 4)]
  128. pub type_cell_data: String,
  129. }
  130. impl std::convert::From<CellChangesetPB> for RowChangeset {
  131. fn from(changeset: CellChangesetPB) -> Self {
  132. let mut cell_by_field_id = HashMap::with_capacity(1);
  133. let field_id = changeset.field_id;
  134. let cell_rev = CellRevision {
  135. type_cell_data: changeset.type_cell_data,
  136. };
  137. cell_by_field_id.insert(field_id, cell_rev);
  138. RowChangeset {
  139. row_id: changeset.row_id,
  140. height: None,
  141. visibility: None,
  142. cell_by_field_id,
  143. }
  144. }
  145. }