184 lines
4.6 KiB
Rust
Raw Normal View History

use crate::impl_def_and_def_mut;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
2023-01-30 11:11:19 +08:00
use folder_model::{TrashRevision, TrashTypeRevision};
2022-01-15 23:58:36 +08:00
use serde::{Deserialize, Serialize};
2021-10-28 13:42:39 +08:00
use std::fmt::Formatter;
#[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone)]
2022-07-19 14:11:29 +08:00
pub struct TrashPB {
2022-01-15 23:58:36 +08:00
#[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,
}
2022-07-19 14:11:29 +08:00
impl std::convert::From<TrashRevision> for TrashPB {
fn from(trash_rev: TrashRevision) -> Self {
2022-07-19 14:11:29 +08:00
TrashPB {
id: trash_rev.id,
name: trash_rev.name,
modified_time: trash_rev.modified_time,
create_time: trash_rev.create_time,
ty: trash_rev.ty.into(),
}
}
}
2022-07-19 14:11:29 +08:00
impl std::convert::From<TrashPB> for TrashRevision {
fn from(trash: TrashPB) -> Self {
TrashRevision {
id: trash.id,
name: trash.name,
modified_time: trash.modified_time,
create_time: trash.create_time,
ty: trash.ty.into(),
}
}
}
#[derive(PartialEq, Eq, Debug, Default, ProtoBuf, Clone)]
2022-07-19 14:11:29 +08:00
pub struct RepeatedTrashPB {
2022-01-15 23:58:36 +08:00
#[pb(index = 1)]
2022-07-19 14:11:29 +08:00
pub items: Vec<TrashPB>,
2022-01-15 23:58:36 +08:00
}
2022-07-19 14:11:29 +08:00
impl_def_and_def_mut!(RepeatedTrashPB, TrashPB);
impl std::convert::From<Vec<TrashRevision>> for RepeatedTrashPB {
fn from(trash_revs: Vec<TrashRevision>) -> Self {
2022-07-19 14:11:29 +08:00
let items: Vec<TrashPB> = trash_revs.into_iter().map(|trash_rev| trash_rev.into()).collect();
RepeatedTrashPB { items }
}
}
2022-01-15 23:58:36 +08:00
2022-01-16 13:44:14 +08:00
#[derive(Eq, PartialEq, Debug, ProtoBuf_Enum, Clone, Serialize, Deserialize)]
pub enum TrashType {
TrashUnknown = 0,
TrashView = 1,
TrashApp = 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::TrashUnknown),
1 => Ok(TrashType::TrashView),
2 => Ok(TrashType::TrashApp),
_ => Err(format!("Invalid trash type: {}", value)),
}
}
}
impl std::convert::From<TrashTypeRevision> for TrashType {
fn from(rev: TrashTypeRevision) -> Self {
match rev {
TrashTypeRevision::Unknown => TrashType::TrashUnknown,
TrashTypeRevision::TrashView => TrashType::TrashView,
TrashTypeRevision::TrashApp => TrashType::TrashApp,
}
}
}
impl std::convert::From<TrashType> for TrashTypeRevision {
fn from(rev: TrashType) -> Self {
match rev {
TrashType::TrashUnknown => TrashTypeRevision::Unknown,
TrashType::TrashView => TrashTypeRevision::TrashView,
TrashType::TrashApp => TrashTypeRevision::TrashApp,
}
}
}
impl std::default::Default for TrashType {
2022-01-23 12:14:00 +08:00
fn default() -> Self {
TrashType::TrashUnknown
2022-01-23 12:14:00 +08:00
}
}
2021-10-12 22:31:38 +08:00
#[derive(PartialEq, Eq, ProtoBuf, Default, Debug, Clone)]
2022-07-19 14:11:29 +08:00
pub struct RepeatedTrashIdPB {
#[pb(index = 1)]
2022-07-19 14:11:29 +08:00
pub items: Vec<TrashIdPB>,
#[pb(index = 2)]
pub delete_all: bool,
}
2022-07-19 14:11:29 +08:00
impl std::fmt::Display for RepeatedTrashIdPB {
2021-10-28 13:42:39 +08: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 13:42:39 +08:00
))
}
}
2022-07-19 14:11:29 +08:00
impl RepeatedTrashIdPB {
pub fn all() -> RepeatedTrashIdPB {
RepeatedTrashIdPB {
items: vec![],
delete_all: true,
}
}
}
2022-07-19 14:11:29 +08:00
impl std::convert::From<Vec<TrashIdPB>> for RepeatedTrashIdPB {
fn from(items: Vec<TrashIdPB>) -> Self {
RepeatedTrashIdPB {
items,
delete_all: false,
}
}
}
2022-07-19 14:11:29 +08:00
impl std::convert::From<Vec<TrashRevision>> for RepeatedTrashIdPB {
fn from(trash: Vec<TrashRevision>) -> Self {
let items = trash
.into_iter()
2022-07-19 14:11:29 +08:00
.map(|t| TrashIdPB {
id: t.id,
ty: t.ty.into(),
})
.collect::<Vec<_>>();
2022-07-19 14:11:29 +08:00
RepeatedTrashIdPB {
items,
delete_all: false,
}
}
}
#[derive(PartialEq, Eq, ProtoBuf, Default, Debug, Clone)]
2022-07-19 14:11:29 +08:00
pub struct TrashIdPB {
2021-10-12 22:31:38 +08:00
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub ty: TrashType,
2021-10-12 22:31:38 +08:00
}
2022-07-19 14:11:29 +08:00
impl std::fmt::Display for TrashIdPB {
2022-01-23 12:14:00 +08:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{:?}:{}", self.ty, self.id))
}
}
2022-07-19 14:11:29 +08:00
impl std::convert::From<&TrashRevision> for TrashIdPB {
fn from(trash: &TrashRevision) -> Self {
2022-07-19 14:11:29 +08:00
TrashIdPB {
id: trash.id.clone(),
ty: trash.ty.clone().into(),
}
}
}