167 lines
3.6 KiB
Rust
Raw Normal View History

2021-07-19 22:44:37 +08:00
use crate::{
2021-10-13 11:10:29 +08:00
entities::{app::parser::AppId, trash::Trash, view::parser::*},
2021-09-16 12:35:55 +08:00
errors::WorkspaceError,
2021-07-20 15:51:49 +08:00
impl_def_and_def_mut,
2021-07-19 22:44:37 +08:00
};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use std::convert::TryInto;
2021-09-02 19:57:19 +08:00
#[derive(PartialEq, Debug, ProtoBuf_Enum, Clone)]
2021-07-21 22:41:44 +08:00
pub enum ViewType {
2021-07-22 17:39:44 +08:00
Blank = 0,
Doc = 1,
2021-07-19 22:44:37 +08:00
}
2021-07-21 22:41:44 +08:00
impl std::default::Default for ViewType {
2021-07-22 17:39:44 +08:00
fn default() -> Self { ViewType::Blank }
2021-07-19 22:44:37 +08:00
}
2021-08-25 21:33:29 +08:00
impl std::convert::From<i32> for ViewType {
fn from(val: i32) -> Self {
match val {
1 => ViewType::Doc,
0 => ViewType::Blank,
_ => {
log::error!("Invalid view type: {}", val);
ViewType::Blank
},
}
}
}
2021-07-19 22:44:37 +08:00
#[derive(Default, ProtoBuf)]
pub struct CreateViewRequest {
#[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)]
2021-07-21 22:41:44 +08:00
pub view_type: ViewType,
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)]
pub view_type: ViewType,
2021-09-11 14:26:30 +08:00
#[pb(index = 6)]
2021-09-26 16:39:57 +08:00
pub data: String,
2021-09-14 16:22:44 +08:00
}
2021-09-27 23:23:23 +08:00
pub const DOC_DEFAULT_DATA: &str = "[{\"insert\":\"\\n\"}]";
2021-09-26 16:39:57 +08:00
#[allow(dead_code)]
2021-09-27 23:23:23 +08:00
pub fn default_delta() -> Vec<u8> { DOC_DEFAULT_DATA.as_bytes().to_vec() }
2021-09-14 16:22:44 +08:00
impl CreateViewParams {
pub fn new(belong_to_id: String, name: String, desc: String, view_type: ViewType, thumbnail: String) -> Self {
Self {
belong_to_id,
name,
desc,
thumbnail,
view_type,
2021-09-27 23:23:23 +08:00
data: DOC_DEFAULT_DATA.to_string(),
2021-09-14 16:22:44 +08:00
}
}
2021-07-19 22:44:37 +08:00
}
impl TryInto<CreateViewParams> for CreateViewRequest {
type Error = WorkspaceError;
fn try_into(self) -> Result<CreateViewParams, Self::Error> {
2021-09-26 16:39:57 +08:00
let name = ViewName::parse(self.name)
.map_err(|e| WorkspaceError::view_name().context(e))?
.0;
2021-07-19 22:44:37 +08:00
2021-09-26 16:39:57 +08:00
let belong_to_id = AppId::parse(self.belong_to_id)
.map_err(|e| WorkspaceError::app_id().context(e))?
.0;
2021-07-19 22:44:37 +08:00
let thumbnail = match self.thumbnail {
None => "".to_string(),
Some(thumbnail) => {
ViewThumbnail::parse(thumbnail)
2021-09-16 12:35:55 +08:00
.map_err(|e| WorkspaceError::view_thumbnail().context(e))?
2021-07-19 22:44:37 +08:00
.0
},
};
2021-09-26 16:39:57 +08:00
Ok(CreateViewParams::new(
belong_to_id,
name,
self.desc,
self.view_type,
thumbnail,
))
2021-07-19 22:44:37 +08:00
}
}
2021-09-02 19:57:19 +08:00
#[derive(PartialEq, ProtoBuf, Default, Debug, Clone)]
2021-07-19 22:44:37 +08:00
pub struct View {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
2021-07-28 13:41:39 +08:00
pub belong_to_id: String,
2021-07-19 22:44:37 +08:00
#[pb(index = 3)]
pub name: String,
#[pb(index = 4)]
pub desc: String,
#[pb(index = 5)]
2021-07-21 22:41:44 +08:00
pub view_type: ViewType,
2021-07-28 15:13:48 +08:00
#[pb(index = 6)]
pub version: i64,
#[pb(index = 7)]
pub belongings: RepeatedView,
2021-09-02 19:57:19 +08:00
#[pb(index = 8)]
pub modified_time: i64,
#[pb(index = 9)]
pub create_time: i64,
2021-07-19 22:44:37 +08:00
}
2021-07-20 15:51:49 +08:00
2021-09-02 19:57:19 +08:00
#[derive(PartialEq, Debug, Default, ProtoBuf, Clone)]
2021-07-20 15:51:49 +08:00
pub struct RepeatedView {
#[pb(index = 1)]
pub items: Vec<View>,
}
impl_def_and_def_mut!(RepeatedView, View);
2021-10-13 11:10:29 +08:00
impl std::convert::Into(Trash) for View {
fn into(self) -> (Trash) {
Trash {
id: self.id,
name: self.name,
modified_time: self.modified_time,
create_time: self.create_time,
}
}
}