trash.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(Eq, 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::TrashApp,
  32. }
  33. }
  34. }
  35. #[derive(Eq, PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
  36. pub enum TrashType {
  37. Unknown = 0,
  38. TrashView = 1,
  39. TrashApp = 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::TrashView),
  47. 2 => Ok(TrashType::TrashApp),
  48. _ => Err(format!("Invalid trash type: {}", value)),
  49. }
  50. }
  51. }
  52. impl std::default::Default for TrashType {
  53. fn default() -> Self {
  54. TrashType::Unknown
  55. }
  56. }
  57. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  58. pub struct RepeatedTrashId {
  59. #[pb(index = 1)]
  60. pub items: Vec<TrashId>,
  61. #[pb(index = 2)]
  62. pub delete_all: bool,
  63. }
  64. impl std::fmt::Display for RepeatedTrashId {
  65. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  66. f.write_str(&format!(
  67. "{:?}",
  68. &self.items.iter().map(|item| format!("{}", item)).collect::<Vec<_>>()
  69. ))
  70. }
  71. }
  72. impl RepeatedTrashId {
  73. pub fn all() -> RepeatedTrashId {
  74. RepeatedTrashId {
  75. items: vec![],
  76. delete_all: true,
  77. }
  78. }
  79. }
  80. impl std::convert::From<Vec<TrashId>> for RepeatedTrashId {
  81. fn from(items: Vec<TrashId>) -> Self {
  82. RepeatedTrashId {
  83. items,
  84. delete_all: false,
  85. }
  86. }
  87. }
  88. impl std::convert::From<Vec<Trash>> for RepeatedTrashId {
  89. fn from(trash: Vec<Trash>) -> Self {
  90. let items = trash
  91. .into_iter()
  92. .map(|t| TrashId { id: t.id, ty: t.ty })
  93. .collect::<Vec<_>>();
  94. RepeatedTrashId {
  95. items,
  96. delete_all: false,
  97. }
  98. }
  99. }
  100. #[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
  101. pub struct TrashId {
  102. #[pb(index = 1)]
  103. pub id: String,
  104. #[pb(index = 2)]
  105. pub ty: TrashType,
  106. }
  107. impl std::convert::From<&Trash> for TrashId {
  108. fn from(trash: &Trash) -> Self {
  109. TrashId {
  110. id: trash.id.clone(),
  111. ty: trash.ty.clone(),
  112. }
  113. }
  114. }
  115. impl std::fmt::Display for TrashId {
  116. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  117. f.write_str(&format!("{:?}:{}", self.ty, self.id))
  118. }
  119. }