453 lines
10 KiB
Rust
Raw Normal View History

2021-07-19 22:44:37 +08:00
use crate::{
entities::trash::{Trash, TrashType},
errors::ErrorCode,
2021-07-20 15:51:49 +08:00
impl_def_and_def_mut,
parser::{
2021-12-31 10:32:25 +08:00
app::AppIdentify,
2022-03-16 10:02:37 +08:00
view::{ViewDesc, ViewIdentify, ViewName, ViewThumbnail},
},
2021-07-19 22:44:37 +08:00
};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
2022-04-11 15:27:03 +08:00
use nanoid::nanoid;
2022-01-15 23:58:36 +08:00
use serde::{Deserialize, Serialize};
2022-03-01 20:51:49 +08:00
use serde_repr::*;
2021-07-19 22:44:37 +08:00
use std::convert::TryInto;
2022-04-11 15:27:03 +08:00
pub fn gen_view_id() -> String {
nanoid!(10)
}
#[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone)]
2022-01-13 11:16:26 +08:00
pub struct View {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub belong_to_id: String,
#[pb(index = 3)]
pub name: String,
#[pb(index = 4)]
pub desc: String,
#[pb(index = 5)]
2022-02-28 22:38:53 +08:00
pub data_type: ViewDataType,
2022-01-13 11:16:26 +08:00
#[pb(index = 6)]
pub version: i64,
#[pb(index = 7)]
pub belongings: RepeatedView,
#[pb(index = 8)]
pub modified_time: i64,
#[pb(index = 9)]
pub create_time: i64,
2022-02-28 22:38:53 +08:00
#[pb(index = 10)]
pub ext_data: String,
#[pb(index = 11)]
pub thumbnail: String,
2022-03-01 10:25:21 +08:00
#[pb(index = 12)]
pub plugin_type: i32,
}
#[derive(Serialize, Deserialize)]
pub struct ViewSerde {
pub id: String,
pub belong_to_id: String,
pub name: String,
pub desc: String,
#[serde(default)]
pub data_type: ViewDataType,
pub version: i64,
pub belongings: Vec<ViewSerde>,
pub modified_time: i64,
pub create_time: i64,
#[serde(default)]
pub ext_data: String,
#[serde(default)]
pub thumbnail: String,
2022-03-01 10:25:21 +08:00
#[serde(default = "default_plugin_type")]
pub plugin_type: i32,
}
fn default_plugin_type() -> i32 {
0
2022-01-13 11:16:26 +08:00
}
impl std::convert::From<ViewSerde> for View {
fn from(view_serde: ViewSerde) -> Self {
View {
id: view_serde.id,
belong_to_id: view_serde.belong_to_id,
name: view_serde.name,
desc: view_serde.desc,
data_type: view_serde.data_type,
version: view_serde.version,
belongings: view_serde.belongings.into(),
modified_time: view_serde.modified_time,
create_time: view_serde.create_time,
ext_data: view_serde.ext_data,
thumbnail: view_serde.thumbnail,
plugin_type: view_serde.plugin_type,
}
}
}
#[derive(Eq, PartialEq, Debug, Default, ProtoBuf, Clone)]
// #[serde(transparent)]
2022-01-13 11:16:26 +08:00
pub struct RepeatedView {
#[pb(index = 1)]
pub items: Vec<View>,
}
impl_def_and_def_mut!(RepeatedView, View);
impl std::convert::From<Vec<ViewSerde>> for RepeatedView {
fn from(values: Vec<ViewSerde>) -> Self {
let items = values.into_iter().map(|value| value.into()).collect::<Vec<View>>();
RepeatedView { items }
}
}
2022-01-13 11:16:26 +08:00
impl std::convert::From<View> for Trash {
fn from(view: View) -> Self {
Trash {
id: view.id,
name: view.name,
modified_time: view.modified_time,
create_time: view.create_time,
ty: TrashType::TrashView,
2022-01-13 11:16:26 +08:00
}
}
}
2022-03-05 22:30:42 +08:00
#[derive(Eq, PartialEq, Hash, Debug, ProtoBuf_Enum, Clone, Serialize_repr, Deserialize_repr)]
2022-03-01 20:51:49 +08:00
#[repr(u8)]
2022-02-28 22:38:53 +08:00
pub enum ViewDataType {
2022-03-12 09:30:13 +08:00
TextBlock = 0,
2022-03-06 21:22:42 +08:00
Grid = 1,
2021-07-19 22:44:37 +08:00
}
2022-02-28 22:38:53 +08:00
impl std::default::Default for ViewDataType {
2022-01-24 17:35:58 +08:00
fn default() -> Self {
2022-03-12 09:30:13 +08:00
ViewDataType::TextBlock
2022-01-24 17:35:58 +08:00
}
2021-07-19 22:44:37 +08:00
}
2022-02-28 22:38:53 +08:00
impl std::convert::From<i32> for ViewDataType {
2021-08-25 21:33:29 +08:00
fn from(val: i32) -> Self {
match val {
2022-03-12 09:30:13 +08:00
0 => ViewDataType::TextBlock,
2022-03-06 21:22:42 +08:00
1 => ViewDataType::Grid,
2021-08-25 21:33:29 +08:00
_ => {
log::error!("Invalid view type: {}", val);
2022-03-12 09:30:13 +08:00
ViewDataType::TextBlock
2022-01-24 17:35:58 +08:00
}
2021-08-25 21:33:29 +08:00
}
}
}
2021-07-19 22:44:37 +08:00
#[derive(Default, ProtoBuf)]
2022-02-24 21:49:18 +08:00
pub struct CreateViewPayload {
2021-07-19 22:44:37 +08:00
#[pb(index = 1)]
2021-07-28 13:41:39 +08:00
pub belong_to_id: String,
2021-07-19 22:44:37 +08:00
#[pb(index = 2)]
pub name: String,
#[pb(index = 3)]
pub desc: String,
#[pb(index = 4, one_of)]
pub thumbnail: Option<String>,
#[pb(index = 5)]
2022-02-28 22:38:53 +08:00
pub data_type: ViewDataType,
2022-02-28 16:00:43 +08:00
#[pb(index = 6)]
2022-03-15 11:07:18 +08:00
pub plugin_type: i32,
2022-03-01 10:25:21 +08:00
#[pb(index = 7)]
2022-03-15 19:00:28 +08:00
pub data: Vec<u8>,
2021-07-19 22:44:37 +08:00
}
2021-09-11 14:26:30 +08:00
#[derive(Default, ProtoBuf, Debug, Clone)]
2021-07-19 22:44:37 +08:00
pub struct CreateViewParams {
2021-08-25 21:33:29 +08:00
#[pb(index = 1)]
2021-07-28 13:41:39 +08:00
pub belong_to_id: String,
2021-08-25 21:33:29 +08:00
#[pb(index = 2)]
2021-07-19 22:44:37 +08:00
pub name: String,
2021-08-25 21:33:29 +08:00
#[pb(index = 3)]
2021-07-19 22:44:37 +08:00
pub desc: String,
2021-08-25 21:33:29 +08:00
#[pb(index = 4)]
2021-07-19 22:44:37 +08:00
pub thumbnail: String,
2021-08-25 21:33:29 +08:00
#[pb(index = 5)]
2022-02-28 22:38:53 +08:00
pub data_type: ViewDataType,
2021-12-29 00:34:00 +08:00
#[pb(index = 6)]
pub view_id: String,
2021-09-14 16:22:44 +08:00
2022-03-15 11:07:18 +08:00
#[pb(index = 7)]
2022-03-15 19:00:28 +08:00
pub data: Vec<u8>,
2022-03-01 10:25:21 +08:00
2022-03-15 11:07:18 +08:00
#[pb(index = 8)]
2022-03-01 10:25:21 +08:00
pub plugin_type: i32,
2021-07-19 22:44:37 +08:00
}
2022-02-24 21:49:18 +08:00
impl TryInto<CreateViewParams> for CreateViewPayload {
type Error = ErrorCode;
2021-07-19 22:44:37 +08:00
fn try_into(self) -> Result<CreateViewParams, Self::Error> {
let name = ViewName::parse(self.name)?.0;
2021-12-31 10:32:25 +08:00
let belong_to_id = AppIdentify::parse(self.belong_to_id)?.0;
2022-04-11 15:27:03 +08:00
let view_id = gen_view_id();
2021-07-19 22:44:37 +08:00
let thumbnail = match self.thumbnail {
None => "".to_string(),
Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
2021-07-19 22:44:37 +08:00
};
2022-02-28 22:38:53 +08:00
Ok(CreateViewParams {
2021-09-26 16:39:57 +08:00
belong_to_id,
name,
2022-02-28 22:38:53 +08:00
desc: self.desc,
data_type: self.data_type,
2021-09-26 16:39:57 +08:00
thumbnail,
2021-12-29 00:34:00 +08:00
view_id,
2022-03-15 11:07:18 +08:00
data: self.data,
2022-03-01 10:25:21 +08:00
plugin_type: self.plugin_type,
2022-02-28 22:38:53 +08:00
})
2021-07-19 22:44:37 +08:00
}
}
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct ViewId {
#[pb(index = 1)]
2022-02-24 21:49:18 +08:00
pub value: String,
2022-01-13 11:16:26 +08:00
}
2021-07-19 22:44:37 +08:00
2022-02-24 21:49:18 +08:00
impl std::convert::From<&str> for ViewId {
fn from(value: &str) -> Self {
ViewId {
value: value.to_string(),
}
}
}
2021-09-02 19:57:19 +08:00
2022-05-05 10:45:53 +08:00
impl std::ops::Deref for ViewId {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.value
}
}
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf)]
pub struct RepeatedViewId {
#[pb(index = 1)]
pub items: Vec<String>,
}
2021-09-02 19:57:19 +08:00
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf)]
2022-02-24 21:49:18 +08:00
pub struct UpdateViewPayload {
2021-07-20 15:51:49 +08:00
#[pb(index = 1)]
2022-01-13 11:16:26 +08:00
pub view_id: String,
#[pb(index = 2, one_of)]
pub name: Option<String>,
#[pb(index = 3, one_of)]
pub desc: Option<String>,
#[pb(index = 4, one_of)]
pub thumbnail: Option<String>,
2021-07-20 15:51:49 +08:00
}
2022-01-13 11:16:26 +08:00
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct UpdateViewParams {
#[pb(index = 1)]
pub view_id: String,
2021-10-13 11:10:29 +08:00
2022-01-13 11:16:26 +08:00
#[pb(index = 2, one_of)]
pub name: Option<String>,
#[pb(index = 3, one_of)]
pub desc: Option<String>,
#[pb(index = 4, one_of)]
pub thumbnail: Option<String>,
}
impl UpdateViewParams {
pub fn new(view_id: &str) -> Self {
Self {
view_id: view_id.to_owned(),
..Default::default()
2021-10-13 11:10:29 +08:00
}
}
2022-01-13 11:16:26 +08:00
pub fn name(mut self, name: &str) -> Self {
self.name = Some(name.to_owned());
self
}
pub fn desc(mut self, desc: &str) -> Self {
self.desc = Some(desc.to_owned());
self
}
}
2022-02-24 21:49:18 +08:00
impl TryInto<UpdateViewParams> for UpdateViewPayload {
2022-01-13 11:16:26 +08:00
type Error = ErrorCode;
fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
let view_id = ViewIdentify::parse(self.view_id)?.0;
let name = match self.name {
None => None,
Some(name) => Some(ViewName::parse(name)?.0),
};
let desc = match self.desc {
None => None,
Some(desc) => Some(ViewDesc::parse(desc)?.0),
};
let thumbnail = match self.thumbnail {
None => None,
Some(thumbnail) => Some(ViewThumbnail::parse(thumbnail)?.0),
};
Ok(UpdateViewParams {
view_id,
name,
desc,
thumbnail,
})
}
2021-10-13 11:10:29 +08:00
}
2022-04-26 21:20:02 +08:00
#[derive(ProtoBuf_Enum)]
pub enum MoveFolderItemType {
MoveApp = 0,
MoveView = 1,
}
impl std::default::Default for MoveFolderItemType {
fn default() -> Self {
MoveFolderItemType::MoveApp
}
}
#[derive(Default, ProtoBuf)]
pub struct MoveFolderItemPayload {
#[pb(index = 1)]
pub item_id: String,
#[pb(index = 2)]
pub from: i32,
#[pb(index = 3)]
pub to: i32,
#[pb(index = 4)]
pub ty: MoveFolderItemType,
}
pub struct MoveFolderItemParams {
pub item_id: String,
pub from: usize,
pub to: usize,
pub ty: MoveFolderItemType,
}
impl TryInto<MoveFolderItemParams> for MoveFolderItemPayload {
type Error = ErrorCode;
fn try_into(self) -> Result<MoveFolderItemParams, Self::Error> {
let view_id = ViewIdentify::parse(self.item_id)?.0;
Ok(MoveFolderItemParams {
item_id: view_id,
from: self.from as usize,
to: self.to as usize,
ty: self.ty,
})
}
}
2022-03-01 20:51:49 +08:00
// impl<'de> Deserialize<'de> for ViewDataType {
// fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
// where
// D: Deserializer<'de>,
// {
// struct ViewTypeVisitor();
//
// impl<'de> Visitor<'de> for ViewTypeVisitor {
// type Value = ViewDataType;
// fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
// formatter.write_str("RichText, PlainText")
// }
//
// fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
// where
// E: de::Error,
// {
// let data_type;
// match v {
// 0 => {
// data_type = ViewDataType::RichText;
// }
// 1 => {
// data_type = ViewDataType::PlainText;
// }
// _ => {
// return Err(de::Error::invalid_value(Unexpected::Unsigned(v as u64), &self));
// }
// }
// Ok(data_type)
// }
//
// fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
// where
// E: de::Error,
// {
// let data_type;
// match s {
// "Doc" | "RichText" => {
// // Rename ViewDataType::Doc to ViewDataType::RichText, So we need to migrate the ViewType manually.
// data_type = ViewDataType::RichText;
// }
// "PlainText" => {
// data_type = ViewDataType::PlainText;
// }
// unknown => {
// return Err(de::Error::invalid_value(Unexpected::Str(unknown), &self));
// }
// }
// Ok(data_type)
// }
// }
// deserializer.deserialize_any(ViewTypeVisitor())
// }
// }