AppFlowy/shared-lib/flowy-folder-data-model/src/entities/trash.rs

143 lines
3.1 KiB
Rust
Raw Normal View History

2022-01-14 01:09:25 +00:00
use crate::{entities::app::App, impl_def_and_def_mut};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
2022-01-15 15:58:36 +00:00
use serde::{Deserialize, Serialize};
2021-10-28 05:42:39 +00:00
use std::fmt::Formatter;
2022-01-16 05:44:14 +00:00
#[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
2022-01-15 15:58:36 +00:00
pub struct Trash {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub name: String,
#[pb(index = 3)]
pub modified_time: i64,
#[pb(index = 4)]
pub create_time: i64,
#[pb(index = 5)]
pub ty: TrashType,
}
#[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
pub struct RepeatedTrash {
#[pb(index = 1)]
pub items: Vec<Trash>,
}
impl_def_and_def_mut!(RepeatedTrash, Trash);
impl std::convert::From<App> for Trash {
fn from(app: App) -> Self {
Trash {
id: app.id,
name: app.name,
modified_time: app.modified_time,
create_time: app.create_time,
ty: TrashType::App,
}
}
}
2022-01-16 05:44:14 +00:00
#[derive(Eq, PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
pub enum TrashType {
Unknown = 0,
2022-01-23 04:14:00 +00:00
View = 1,
App = 2,
}
impl std::convert::TryFrom<i32> for TrashType {
type Error = String;
fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0 => Ok(TrashType::Unknown),
1 => Ok(TrashType::View),
2021-10-30 09:19:50 +00:00
2 => Ok(TrashType::App),
_ => Err(format!("Invalid trash type: {}", value)),
}
}
}
impl std::default::Default for TrashType {
2022-01-23 04:14:00 +00:00
fn default() -> Self {
TrashType::Unknown
}
}
2021-10-12 14:31:38 +00:00
#[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
2021-12-31 02:32:25 +00:00
pub struct RepeatedTrashId {
#[pb(index = 1)]
2021-12-31 02:32:25 +00:00
pub items: Vec<TrashId>,
#[pb(index = 2)]
pub delete_all: bool,
}
2021-12-31 02:32:25 +00:00
impl std::fmt::Display for RepeatedTrashId {
2021-10-28 05:42:39 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
"{:?}",
&self.items.iter().map(|item| format!("{}", item)).collect::<Vec<_>>()
2021-10-28 05:42:39 +00:00
))
}
}
2021-12-31 02:32:25 +00:00
impl RepeatedTrashId {
pub fn all() -> RepeatedTrashId {
RepeatedTrashId {
items: vec![],
delete_all: true,
}
}
}
2021-12-31 02:32:25 +00:00
impl std::convert::From<Vec<TrashId>> for RepeatedTrashId {
fn from(items: Vec<TrashId>) -> Self {
RepeatedTrashId {
items,
delete_all: false,
}
}
}
2021-12-31 02:32:25 +00:00
impl std::convert::From<Vec<Trash>> for RepeatedTrashId {
fn from(trash: Vec<Trash>) -> Self {
let items = trash
.into_iter()
2021-12-31 02:32:25 +00:00
.map(|t| TrashId { id: t.id, ty: t.ty })
.collect::<Vec<_>>();
2021-12-31 02:32:25 +00:00
RepeatedTrashId {
items,
delete_all: false,
}
}
}
#[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
2021-12-31 02:32:25 +00:00
pub struct TrashId {
2021-10-12 14:31:38 +00:00
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub ty: TrashType,
2021-10-12 14:31:38 +00:00
}
2021-12-31 02:32:25 +00:00
impl std::convert::From<&Trash> for TrashId {
fn from(trash: &Trash) -> Self {
2021-12-31 02:32:25 +00:00
TrashId {
id: trash.id.clone(),
ty: trash.ty.clone(),
}
}
}
2021-12-31 02:32:25 +00:00
impl std::fmt::Display for TrashId {
2022-01-23 04:14:00 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{:?}:{}", self.ty, self.id))
}
}