cell_entities.rs 4.5 KB

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