trash.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. use crate::impl_def_and_def_mut;
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use folder_rev_model::{TrashRevision, TrashTypeRevision};
  4. use serde::{Deserialize, Serialize};
  5. use std::fmt::Formatter;
  6. #[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone)]
  7. pub struct TrashPB {
  8. #[pb(index = 1)]
  9. pub id: String,
  10. #[pb(index = 2)]
  11. pub name: String,
  12. #[pb(index = 3)]
  13. pub modified_time: i64,
  14. #[pb(index = 4)]
  15. pub create_time: i64,
  16. #[pb(index = 5)]
  17. pub ty: TrashType,
  18. }
  19. impl std::convert::From<TrashRevision> for TrashPB {
  20. fn from(trash_rev: TrashRevision) -> Self {
  21. TrashPB {
  22. id: trash_rev.id,
  23. name: trash_rev.name,
  24. modified_time: trash_rev.modified_time,
  25. create_time: trash_rev.create_time,
  26. ty: trash_rev.ty.into(),
  27. }
  28. }
  29. }
  30. impl std::convert::From<TrashPB> for TrashRevision {
  31. fn from(trash: TrashPB) -> Self {
  32. TrashRevision {
  33. id: trash.id,
  34. name: trash.name,
  35. modified_time: trash.modified_time,
  36. create_time: trash.create_time,
  37. ty: trash.ty.into(),
  38. }
  39. }
  40. }
  41. #[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
  42. pub struct RepeatedTrashPB {
  43. #[pb(index = 1)]
  44. pub items: Vec<TrashPB>,
  45. }
  46. impl_def_and_def_mut!(RepeatedTrashPB, TrashPB);
  47. impl std::convert::From<Vec<TrashRevision>> for RepeatedTrashPB {
  48. fn from(trash_revs: Vec<TrashRevision>) -> Self {
  49. let items: Vec<TrashPB> = trash_revs.into_iter().map(|trash_rev| trash_rev.into()).collect();
  50. RepeatedTrashPB { items }
  51. }
  52. }
  53. #[derive(Eq, PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
  54. pub enum TrashType {
  55. TrashUnknown = 0,
  56. TrashView = 1,
  57. TrashApp = 2,
  58. }
  59. impl std::convert::TryFrom<i32> for TrashType {
  60. type Error = String;
  61. fn try_from(value: i32) -> Result<Self, Self::Error> {
  62. match value {
  63. 0 => Ok(TrashType::TrashUnknown),
  64. 1 => Ok(TrashType::TrashView),
  65. 2 => Ok(TrashType::TrashApp),
  66. _ => Err(format!("Invalid trash type: {}", value)),
  67. }
  68. }
  69. }
  70. impl std::convert::From<TrashTypeRevision> for TrashType {
  71. fn from(rev: TrashTypeRevision) -> Self {
  72. match rev {
  73. TrashTypeRevision::Unknown => TrashType::TrashUnknown,
  74. TrashTypeRevision::TrashView => TrashType::TrashView,
  75. TrashTypeRevision::TrashApp => TrashType::TrashApp,
  76. }
  77. }
  78. }
  79. impl std::convert::From<TrashType> for TrashTypeRevision {
  80. fn from(rev: TrashType) -> Self {
  81. match rev {
  82. TrashType::TrashUnknown => TrashTypeRevision::Unknown,
  83. TrashType::TrashView => TrashTypeRevision::TrashView,
  84. TrashType::TrashApp => TrashTypeRevision::TrashApp,
  85. }
  86. }
  87. }
  88. impl std::default::Default for TrashType {
  89. fn default() -> Self {
  90. TrashType::TrashUnknown
  91. }
  92. }
  93. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  94. pub struct RepeatedTrashIdPB {
  95. #[pb(index = 1)]
  96. pub items: Vec<TrashIdPB>,
  97. #[pb(index = 2)]
  98. pub delete_all: bool,
  99. }
  100. impl std::fmt::Display for RepeatedTrashIdPB {
  101. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  102. f.write_str(&format!(
  103. "{:?}",
  104. &self.items.iter().map(|item| format!("{}", item)).collect::<Vec<_>>()
  105. ))
  106. }
  107. }
  108. impl RepeatedTrashIdPB {
  109. pub fn all() -> RepeatedTrashIdPB {
  110. RepeatedTrashIdPB {
  111. items: vec![],
  112. delete_all: true,
  113. }
  114. }
  115. }
  116. impl std::convert::From<Vec<TrashIdPB>> for RepeatedTrashIdPB {
  117. fn from(items: Vec<TrashIdPB>) -> Self {
  118. RepeatedTrashIdPB {
  119. items,
  120. delete_all: false,
  121. }
  122. }
  123. }
  124. impl std::convert::From<Vec<TrashRevision>> for RepeatedTrashIdPB {
  125. fn from(trash: Vec<TrashRevision>) -> Self {
  126. let items = trash
  127. .into_iter()
  128. .map(|t| TrashIdPB {
  129. id: t.id,
  130. ty: t.ty.into(),
  131. })
  132. .collect::<Vec<_>>();
  133. RepeatedTrashIdPB {
  134. items,
  135. delete_all: false,
  136. }
  137. }
  138. }
  139. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  140. pub struct TrashIdPB {
  141. #[pb(index = 1)]
  142. pub id: String,
  143. #[pb(index = 2)]
  144. pub ty: TrashType,
  145. }
  146. impl std::fmt::Display for TrashIdPB {
  147. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  148. f.write_str(&format!("{:?}:{}", self.ty, self.id))
  149. }
  150. }
  151. impl std::convert::From<&TrashRevision> for TrashIdPB {
  152. fn from(trash: &TrashRevision) -> Self {
  153. TrashIdPB {
  154. id: trash.id.clone(),
  155. ty: trash.ty.clone().into(),
  156. }
  157. }
  158. }