trash.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. use crate::{entities::app::App, impl_def_and_def_mut};
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use serde::{Deserialize, Serialize};
  4. use std::fmt::Formatter;
  5. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
  6. pub struct Trash {
  7. #[pb(index = 1)]
  8. pub id: String,
  9. #[pb(index = 2)]
  10. pub name: String,
  11. #[pb(index = 3)]
  12. pub modified_time: i64,
  13. #[pb(index = 4)]
  14. pub create_time: i64,
  15. #[pb(index = 5)]
  16. pub ty: TrashType,
  17. }
  18. #[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
  19. pub struct RepeatedTrash {
  20. #[pb(index = 1)]
  21. pub items: Vec<Trash>,
  22. }
  23. impl_def_and_def_mut!(RepeatedTrash, Trash);
  24. impl std::convert::From<App> for Trash {
  25. fn from(app: App) -> Self {
  26. Trash {
  27. id: app.id,
  28. name: app.name,
  29. modified_time: app.modified_time,
  30. create_time: app.create_time,
  31. ty: TrashType::App,
  32. }
  33. }
  34. }
  35. #[derive(PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
  36. pub enum TrashType {
  37. Unknown = 0,
  38. View = 1,
  39. App = 2,
  40. }
  41. impl std::convert::TryFrom<i32> for TrashType {
  42. type Error = String;
  43. fn try_from(value: i32) -> Result<Self, Self::Error> {
  44. match value {
  45. 0 => Ok(TrashType::Unknown),
  46. 1 => Ok(TrashType::View),
  47. 2 => Ok(TrashType::App),
  48. _ => Err(format!("Invalid trash type: {}", value)),
  49. }
  50. }
  51. }
  52. impl std::default::Default for TrashType {
  53. fn default() -> Self { TrashType::Unknown }
  54. }
  55. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  56. pub struct RepeatedTrashId {
  57. #[pb(index = 1)]
  58. pub items: Vec<TrashId>,
  59. #[pb(index = 2)]
  60. pub delete_all: bool,
  61. }
  62. impl std::fmt::Display for RepeatedTrashId {
  63. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  64. f.write_str(&format!(
  65. "{:?}",
  66. &self.items.iter().map(|item| format!("{}", item)).collect::<Vec<_>>()
  67. ))
  68. }
  69. }
  70. impl RepeatedTrashId {
  71. pub fn all() -> RepeatedTrashId {
  72. RepeatedTrashId {
  73. items: vec![],
  74. delete_all: true,
  75. }
  76. }
  77. }
  78. impl std::convert::From<Vec<TrashId>> for RepeatedTrashId {
  79. fn from(items: Vec<TrashId>) -> Self {
  80. RepeatedTrashId {
  81. items,
  82. delete_all: false,
  83. }
  84. }
  85. }
  86. impl std::convert::From<Vec<Trash>> for RepeatedTrashId {
  87. fn from(trash: Vec<Trash>) -> Self {
  88. let items = trash
  89. .into_iter()
  90. .map(|t| TrashId { id: t.id, ty: t.ty })
  91. .collect::<Vec<_>>();
  92. RepeatedTrashId {
  93. items,
  94. delete_all: false,
  95. }
  96. }
  97. }
  98. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  99. pub struct TrashId {
  100. #[pb(index = 1)]
  101. pub id: String,
  102. #[pb(index = 2)]
  103. pub ty: TrashType,
  104. }
  105. impl std::convert::From<&Trash> for TrashId {
  106. fn from(trash: &Trash) -> Self {
  107. TrashId {
  108. id: trash.id.clone(),
  109. ty: trash.ty.clone(),
  110. }
  111. }
  112. }
  113. impl std::fmt::Display for TrashId {
  114. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&format!("{:?}:{}", self.ty, self.id)) }
  115. }