group_changeset.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. use std::fmt::Formatter;
  2. use flowy_derive::ProtoBuf;
  3. use flowy_error::ErrorCode;
  4. use crate::entities::parser::NotEmptyStr;
  5. use crate::entities::{GroupPB, InsertedRowPB, RowMetaPB};
  6. #[derive(Debug, Default, ProtoBuf)]
  7. pub struct GroupRowsNotificationPB {
  8. #[pb(index = 1)]
  9. pub group_id: String,
  10. #[pb(index = 2, one_of)]
  11. pub group_name: Option<String>,
  12. #[pb(index = 3)]
  13. pub inserted_rows: Vec<InsertedRowPB>,
  14. #[pb(index = 4)]
  15. pub deleted_rows: Vec<String>,
  16. #[pb(index = 5)]
  17. pub updated_rows: Vec<RowMetaPB>,
  18. }
  19. impl std::fmt::Display for GroupRowsNotificationPB {
  20. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  21. for inserted_row in &self.inserted_rows {
  22. f.write_fmt(format_args!(
  23. "Insert: {} row at {:?}",
  24. inserted_row.row_meta.id, inserted_row.index
  25. ))?;
  26. }
  27. for deleted_row in &self.deleted_rows {
  28. f.write_fmt(format_args!("Delete: {} row", deleted_row))?;
  29. }
  30. Ok(())
  31. }
  32. }
  33. impl GroupRowsNotificationPB {
  34. pub fn is_empty(&self) -> bool {
  35. self.group_name.is_none()
  36. && self.inserted_rows.is_empty()
  37. && self.deleted_rows.is_empty()
  38. && self.updated_rows.is_empty()
  39. }
  40. pub fn new(group_id: String) -> Self {
  41. Self {
  42. group_id,
  43. ..Default::default()
  44. }
  45. }
  46. pub fn name(group_id: String, name: &str) -> Self {
  47. Self {
  48. group_id,
  49. group_name: Some(name.to_owned()),
  50. ..Default::default()
  51. }
  52. }
  53. pub fn insert(group_id: String, inserted_rows: Vec<InsertedRowPB>) -> Self {
  54. Self {
  55. group_id,
  56. inserted_rows,
  57. ..Default::default()
  58. }
  59. }
  60. pub fn delete(group_id: String, deleted_rows: Vec<String>) -> Self {
  61. Self {
  62. group_id,
  63. deleted_rows,
  64. ..Default::default()
  65. }
  66. }
  67. pub fn update(group_id: String, updated_rows: Vec<RowMetaPB>) -> Self {
  68. Self {
  69. group_id,
  70. updated_rows,
  71. ..Default::default()
  72. }
  73. }
  74. }
  75. #[derive(Debug, Default, ProtoBuf)]
  76. pub struct MoveGroupPayloadPB {
  77. #[pb(index = 1)]
  78. pub view_id: String,
  79. #[pb(index = 2)]
  80. pub from_group_id: String,
  81. #[pb(index = 3)]
  82. pub to_group_id: String,
  83. }
  84. #[derive(Debug)]
  85. pub struct MoveGroupParams {
  86. pub view_id: String,
  87. pub from_group_id: String,
  88. pub to_group_id: String,
  89. }
  90. impl TryInto<MoveGroupParams> for MoveGroupPayloadPB {
  91. type Error = ErrorCode;
  92. fn try_into(self) -> Result<MoveGroupParams, Self::Error> {
  93. let view_id = NotEmptyStr::parse(self.view_id)
  94. .map_err(|_| ErrorCode::DatabaseViewIdIsEmpty)?
  95. .0;
  96. let from_group_id = NotEmptyStr::parse(self.from_group_id)
  97. .map_err(|_| ErrorCode::GroupIdIsEmpty)?
  98. .0;
  99. let to_group_id = NotEmptyStr::parse(self.to_group_id)
  100. .map_err(|_| ErrorCode::GroupIdIsEmpty)?
  101. .0;
  102. Ok(MoveGroupParams {
  103. view_id,
  104. from_group_id,
  105. to_group_id,
  106. })
  107. }
  108. }
  109. #[derive(Debug, Default, ProtoBuf)]
  110. pub struct GroupChangesPB {
  111. #[pb(index = 1)]
  112. pub view_id: String,
  113. #[pb(index = 2)]
  114. pub inserted_groups: Vec<InsertedGroupPB>,
  115. #[pb(index = 3)]
  116. pub initial_groups: Vec<GroupPB>,
  117. #[pb(index = 4)]
  118. pub deleted_groups: Vec<String>,
  119. #[pb(index = 5)]
  120. pub update_groups: Vec<GroupPB>,
  121. }
  122. impl GroupChangesPB {
  123. pub fn is_empty(&self) -> bool {
  124. self.initial_groups.is_empty()
  125. && self.inserted_groups.is_empty()
  126. && self.deleted_groups.is_empty()
  127. && self.update_groups.is_empty()
  128. }
  129. }
  130. #[derive(Debug, Default, ProtoBuf)]
  131. pub struct InsertedGroupPB {
  132. #[pb(index = 1)]
  133. pub group: GroupPB,
  134. #[pb(index = 2)]
  135. pub index: i32,
  136. }