group_changeset.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use crate::entities::{GroupPB, InsertedRowPB, RowPB};
  2. use flowy_derive::ProtoBuf;
  3. use flowy_error::ErrorCode;
  4. use flowy_grid_data_model::parser::NotEmptyStr;
  5. use std::fmt::Formatter;
  6. #[derive(Debug, Default, ProtoBuf)]
  7. pub struct GroupRowsChangesetPB {
  8. #[pb(index = 1)]
  9. pub group_id: String,
  10. #[pb(index = 2)]
  11. pub inserted_rows: Vec<InsertedRowPB>,
  12. #[pb(index = 3)]
  13. pub deleted_rows: Vec<String>,
  14. #[pb(index = 4)]
  15. pub updated_rows: Vec<RowPB>,
  16. }
  17. impl std::fmt::Display for GroupRowsChangesetPB {
  18. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  19. for inserted_row in &self.inserted_rows {
  20. let _ = f.write_fmt(format_args!(
  21. "Insert: {} row at {:?}",
  22. inserted_row.row.id, inserted_row.index
  23. ))?;
  24. }
  25. for deleted_row in &self.deleted_rows {
  26. let _ = f.write_fmt(format_args!("Delete: {} row", deleted_row))?;
  27. }
  28. Ok(())
  29. }
  30. }
  31. impl GroupRowsChangesetPB {
  32. pub fn is_empty(&self) -> bool {
  33. self.inserted_rows.is_empty() && self.deleted_rows.is_empty() && self.updated_rows.is_empty()
  34. }
  35. pub fn insert(group_id: String, inserted_rows: Vec<InsertedRowPB>) -> Self {
  36. Self {
  37. group_id,
  38. inserted_rows,
  39. ..Default::default()
  40. }
  41. }
  42. pub fn delete(group_id: String, deleted_rows: Vec<String>) -> Self {
  43. Self {
  44. group_id,
  45. deleted_rows,
  46. ..Default::default()
  47. }
  48. }
  49. pub fn update(group_id: String, updated_rows: Vec<RowPB>) -> Self {
  50. Self {
  51. group_id,
  52. updated_rows,
  53. ..Default::default()
  54. }
  55. }
  56. }
  57. #[derive(Debug, Default, ProtoBuf)]
  58. pub struct MoveGroupPayloadPB {
  59. #[pb(index = 1)]
  60. pub view_id: String,
  61. #[pb(index = 2)]
  62. pub from_group_id: String,
  63. #[pb(index = 3)]
  64. pub to_group_id: String,
  65. }
  66. pub struct MoveGroupParams {
  67. pub view_id: String,
  68. pub from_group_id: String,
  69. pub to_group_id: String,
  70. }
  71. impl TryInto<MoveGroupParams> for MoveGroupPayloadPB {
  72. type Error = ErrorCode;
  73. fn try_into(self) -> Result<MoveGroupParams, Self::Error> {
  74. let view_id = NotEmptyStr::parse(self.view_id)
  75. .map_err(|_| ErrorCode::GridViewIdIsEmpty)?
  76. .0;
  77. let from_group_id = NotEmptyStr::parse(self.from_group_id)
  78. .map_err(|_| ErrorCode::GroupIdIsEmpty)?
  79. .0;
  80. let to_group_id = NotEmptyStr::parse(self.to_group_id)
  81. .map_err(|_| ErrorCode::GroupIdIsEmpty)?
  82. .0;
  83. Ok(MoveGroupParams {
  84. view_id,
  85. from_group_id,
  86. to_group_id,
  87. })
  88. }
  89. }
  90. #[derive(Debug, Default, ProtoBuf)]
  91. pub struct GroupViewChangesetPB {
  92. #[pb(index = 1)]
  93. pub view_id: String,
  94. #[pb(index = 2)]
  95. pub inserted_groups: Vec<GroupPB>,
  96. #[pb(index = 3)]
  97. pub deleted_groups: Vec<String>,
  98. }
  99. impl GroupViewChangesetPB {}