cell_entities.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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::{CellRevision, RowMetaChangeset};
  5. use std::collections::HashMap;
  6. #[derive(ProtoBuf, Default)]
  7. pub struct CreateSelectOptionPayloadPB {
  8. #[pb(index = 1)]
  9. pub field_id: String,
  10. #[pb(index = 2)]
  11. pub grid_id: String,
  12. #[pb(index = 3)]
  13. pub option_name: String,
  14. }
  15. pub struct CreateSelectOptionParams {
  16. pub field_id: String,
  17. pub grid_id: String,
  18. pub option_name: String,
  19. }
  20. impl TryInto<CreateSelectOptionParams> for CreateSelectOptionPayloadPB {
  21. type Error = ErrorCode;
  22. fn try_into(self) -> Result<CreateSelectOptionParams, Self::Error> {
  23. let option_name = NotEmptyStr::parse(self.option_name).map_err(|_| ErrorCode::SelectOptionNameIsEmpty)?;
  24. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  25. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  26. Ok(CreateSelectOptionParams {
  27. field_id: field_id.0,
  28. option_name: option_name.0,
  29. grid_id: grid_id.0,
  30. })
  31. }
  32. }
  33. #[derive(Debug, Clone, Default, ProtoBuf)]
  34. pub struct GridCellIdPB {
  35. #[pb(index = 1)]
  36. pub grid_id: String,
  37. #[pb(index = 2)]
  38. pub field_id: String,
  39. #[pb(index = 3)]
  40. pub row_id: String,
  41. }
  42. pub struct GridCellIdParams {
  43. pub grid_id: String,
  44. pub field_id: String,
  45. pub row_id: String,
  46. }
  47. impl TryInto<GridCellIdParams> for GridCellIdPB {
  48. type Error = ErrorCode;
  49. fn try_into(self) -> Result<GridCellIdParams, Self::Error> {
  50. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  51. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  52. let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  53. Ok(GridCellIdParams {
  54. grid_id: grid_id.0,
  55. field_id: field_id.0,
  56. row_id: row_id.0,
  57. })
  58. }
  59. }
  60. #[derive(Debug, Default, ProtoBuf)]
  61. pub struct GridCellPB {
  62. #[pb(index = 1)]
  63. pub field_id: String,
  64. #[pb(index = 2)]
  65. pub data: Vec<u8>,
  66. }
  67. impl GridCellPB {
  68. pub fn new(field_id: &str, data: Vec<u8>) -> Self {
  69. Self {
  70. field_id: field_id.to_owned(),
  71. data,
  72. }
  73. }
  74. pub fn empty(field_id: &str) -> Self {
  75. Self {
  76. field_id: field_id.to_owned(),
  77. data: vec![],
  78. }
  79. }
  80. }
  81. #[derive(Debug, Default, ProtoBuf)]
  82. pub struct RepeatedCellPB {
  83. #[pb(index = 1)]
  84. pub items: Vec<GridCellPB>,
  85. }
  86. impl std::ops::Deref for RepeatedCellPB {
  87. type Target = Vec<GridCellPB>;
  88. fn deref(&self) -> &Self::Target {
  89. &self.items
  90. }
  91. }
  92. impl std::ops::DerefMut for RepeatedCellPB {
  93. fn deref_mut(&mut self) -> &mut Self::Target {
  94. &mut self.items
  95. }
  96. }
  97. impl std::convert::From<Vec<GridCellPB>> for RepeatedCellPB {
  98. fn from(items: Vec<GridCellPB>) -> Self {
  99. Self { items }
  100. }
  101. }
  102. #[derive(Debug, Clone, Default, ProtoBuf)]
  103. pub struct CellChangesetPB {
  104. #[pb(index = 1)]
  105. pub grid_id: String,
  106. #[pb(index = 2)]
  107. pub row_id: String,
  108. #[pb(index = 3)]
  109. pub field_id: String,
  110. #[pb(index = 4, one_of)]
  111. pub content: Option<String>,
  112. }
  113. impl std::convert::From<CellChangesetPB> for RowMetaChangeset {
  114. fn from(changeset: CellChangesetPB) -> Self {
  115. let mut cell_by_field_id = HashMap::with_capacity(1);
  116. let field_id = changeset.field_id;
  117. let cell_rev = CellRevision {
  118. data: changeset.content.unwrap_or_else(|| "".to_owned()),
  119. };
  120. cell_by_field_id.insert(field_id, cell_rev);
  121. RowMetaChangeset {
  122. row_id: changeset.row_id,
  123. height: None,
  124. visibility: None,
  125. cell_by_field_id,
  126. }
  127. }
  128. }