view.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. use crate::{
  2. entities::trash::{Trash, TrashType},
  3. errors::ErrorCode,
  4. impl_def_and_def_mut,
  5. parser::{
  6. app::AppIdentify,
  7. view::{ViewDesc, ViewIdentify, ViewName, ViewThumbnail},
  8. },
  9. };
  10. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  11. use nanoid::nanoid;
  12. use serde::{Deserialize, Serialize};
  13. use serde_repr::*;
  14. use std::convert::TryInto;
  15. pub fn gen_view_id() -> String {
  16. nanoid!(10)
  17. }
  18. #[derive(Eq, PartialEq, ProtoBuf, Default, Debug, Clone, Serialize, Deserialize)]
  19. pub struct View {
  20. #[pb(index = 1)]
  21. pub id: String,
  22. #[pb(index = 2)]
  23. pub belong_to_id: String,
  24. #[pb(index = 3)]
  25. pub name: String,
  26. #[pb(index = 4)]
  27. pub desc: String,
  28. #[pb(index = 5)]
  29. #[serde(default)]
  30. pub data_type: ViewDataType,
  31. #[pb(index = 6)]
  32. pub version: i64,
  33. #[pb(index = 7)]
  34. pub belongings: RepeatedView,
  35. #[pb(index = 8)]
  36. pub modified_time: i64,
  37. #[pb(index = 9)]
  38. pub create_time: i64,
  39. #[pb(index = 10)]
  40. #[serde(default)]
  41. pub ext_data: String,
  42. #[pb(index = 11)]
  43. #[serde(default)]
  44. pub thumbnail: String,
  45. #[pb(index = 12)]
  46. #[serde(default = "default_plugin_type")]
  47. pub plugin_type: i32,
  48. }
  49. fn default_plugin_type() -> i32 {
  50. 0
  51. }
  52. #[derive(Eq, PartialEq, Debug, Default, ProtoBuf, Clone, Serialize, Deserialize)]
  53. #[serde(transparent)]
  54. pub struct RepeatedView {
  55. #[pb(index = 1)]
  56. pub items: Vec<View>,
  57. }
  58. impl_def_and_def_mut!(RepeatedView, View);
  59. impl std::convert::From<View> for Trash {
  60. fn from(view: View) -> Self {
  61. Trash {
  62. id: view.id,
  63. name: view.name,
  64. modified_time: view.modified_time,
  65. create_time: view.create_time,
  66. ty: TrashType::TrashView,
  67. }
  68. }
  69. }
  70. #[derive(Eq, PartialEq, Hash, Debug, ProtoBuf_Enum, Clone, Serialize_repr, Deserialize_repr)]
  71. #[repr(u8)]
  72. pub enum ViewDataType {
  73. TextBlock = 0,
  74. Grid = 1,
  75. }
  76. impl std::default::Default for ViewDataType {
  77. fn default() -> Self {
  78. ViewDataType::TextBlock
  79. }
  80. }
  81. impl std::convert::From<i32> for ViewDataType {
  82. fn from(val: i32) -> Self {
  83. match val {
  84. 0 => ViewDataType::TextBlock,
  85. 1 => ViewDataType::Grid,
  86. _ => {
  87. log::error!("Invalid view type: {}", val);
  88. ViewDataType::TextBlock
  89. }
  90. }
  91. }
  92. }
  93. #[derive(Default, ProtoBuf)]
  94. pub struct CreateViewPayload {
  95. #[pb(index = 1)]
  96. pub belong_to_id: String,
  97. #[pb(index = 2)]
  98. pub name: String,
  99. #[pb(index = 3)]
  100. pub desc: String,
  101. #[pb(index = 4, one_of)]
  102. pub thumbnail: Option<String>,
  103. #[pb(index = 5)]
  104. pub data_type: ViewDataType,
  105. #[pb(index = 6)]
  106. pub plugin_type: i32,
  107. #[pb(index = 7)]
  108. pub data: Vec<u8>,
  109. }
  110. #[derive(Default, ProtoBuf, Debug, Clone)]
  111. pub struct CreateViewParams {
  112. #[pb(index = 1)]
  113. pub belong_to_id: String,
  114. #[pb(index = 2)]
  115. pub name: String,
  116. #[pb(index = 3)]
  117. pub desc: String,
  118. #[pb(index = 4)]
  119. pub thumbnail: String,
  120. #[pb(index = 5)]
  121. pub data_type: ViewDataType,
  122. #[pb(index = 6)]
  123. pub view_id: String,
  124. #[pb(index = 7)]
  125. pub data: Vec<u8>,
  126. #[pb(index = 8)]
  127. pub plugin_type: i32,
  128. }
  129. impl TryInto<CreateViewParams> for CreateViewPayload {
  130. type Error = ErrorCode;
  131. fn try_into(self) -> Result<CreateViewParams, Self::Error> {
  132. let name = ViewName::parse(self.name)?.0;
  133. let belong_to_id = AppIdentify::parse(self.belong_to_id)?.0;
  134. let view_id = gen_view_id();
  135. let thumbnail = match self.thumbnail {
  136. None => "".to_string(),
  137. Some(thumbnail) => ViewThumbnail::parse(thumbnail)?.0,
  138. };
  139. Ok(CreateViewParams {
  140. belong_to_id,
  141. name,
  142. desc: self.desc,
  143. data_type: self.data_type,
  144. thumbnail,
  145. view_id,
  146. data: self.data,
  147. plugin_type: self.plugin_type,
  148. })
  149. }
  150. }
  151. #[derive(Default, ProtoBuf, Clone, Debug)]
  152. pub struct ViewId {
  153. #[pb(index = 1)]
  154. pub value: String,
  155. }
  156. impl std::convert::From<&str> for ViewId {
  157. fn from(value: &str) -> Self {
  158. ViewId {
  159. value: value.to_string(),
  160. }
  161. }
  162. }
  163. impl std::ops::Deref for ViewId {
  164. type Target = str;
  165. fn deref(&self) -> &Self::Target {
  166. &self.value
  167. }
  168. }
  169. #[derive(Default, ProtoBuf)]
  170. pub struct RepeatedViewId {
  171. #[pb(index = 1)]
  172. pub items: Vec<String>,
  173. }
  174. #[derive(Default, ProtoBuf)]
  175. pub struct UpdateViewPayload {
  176. #[pb(index = 1)]
  177. pub view_id: String,
  178. #[pb(index = 2, one_of)]
  179. pub name: Option<String>,
  180. #[pb(index = 3, one_of)]
  181. pub desc: Option<String>,
  182. #[pb(index = 4, one_of)]
  183. pub thumbnail: Option<String>,
  184. }
  185. #[derive(Default, ProtoBuf, Clone, Debug)]
  186. pub struct UpdateViewParams {
  187. #[pb(index = 1)]
  188. pub view_id: String,
  189. #[pb(index = 2, one_of)]
  190. pub name: Option<String>,
  191. #[pb(index = 3, one_of)]
  192. pub desc: Option<String>,
  193. #[pb(index = 4, one_of)]
  194. pub thumbnail: Option<String>,
  195. }
  196. impl UpdateViewParams {
  197. pub fn new(view_id: &str) -> Self {
  198. Self {
  199. view_id: view_id.to_owned(),
  200. ..Default::default()
  201. }
  202. }
  203. pub fn name(mut self, name: &str) -> Self {
  204. self.name = Some(name.to_owned());
  205. self
  206. }
  207. pub fn desc(mut self, desc: &str) -> Self {
  208. self.desc = Some(desc.to_owned());
  209. self
  210. }
  211. }
  212. impl TryInto<UpdateViewParams> for UpdateViewPayload {
  213. type Error = ErrorCode;
  214. fn try_into(self) -> Result<UpdateViewParams, Self::Error> {
  215. let view_id = ViewIdentify::parse(self.view_id)?.0;
  216. let name = match self.name {
  217. None => None,
  218. Some(name) => Some(ViewName::parse(name)?.0),
  219. };
  220. let desc = match self.desc {
  221. None => None,
  222. Some(desc) => Some(ViewDesc::parse(desc)?.0),
  223. };
  224. let thumbnail = match self.thumbnail {
  225. None => None,
  226. Some(thumbnail) => Some(ViewThumbnail::parse(thumbnail)?.0),
  227. };
  228. Ok(UpdateViewParams {
  229. view_id,
  230. name,
  231. desc,
  232. thumbnail,
  233. })
  234. }
  235. }
  236. #[derive(ProtoBuf_Enum)]
  237. pub enum MoveFolderItemType {
  238. MoveApp = 0,
  239. MoveView = 1,
  240. }
  241. impl std::default::Default for MoveFolderItemType {
  242. fn default() -> Self {
  243. MoveFolderItemType::MoveApp
  244. }
  245. }
  246. #[derive(Default, ProtoBuf)]
  247. pub struct MoveFolderItemPayload {
  248. #[pb(index = 1)]
  249. pub item_id: String,
  250. #[pb(index = 2)]
  251. pub from: i32,
  252. #[pb(index = 3)]
  253. pub to: i32,
  254. #[pb(index = 4)]
  255. pub ty: MoveFolderItemType,
  256. }
  257. pub struct MoveFolderItemParams {
  258. pub item_id: String,
  259. pub from: usize,
  260. pub to: usize,
  261. pub ty: MoveFolderItemType,
  262. }
  263. impl TryInto<MoveFolderItemParams> for MoveFolderItemPayload {
  264. type Error = ErrorCode;
  265. fn try_into(self) -> Result<MoveFolderItemParams, Self::Error> {
  266. let view_id = ViewIdentify::parse(self.item_id)?.0;
  267. Ok(MoveFolderItemParams {
  268. item_id: view_id,
  269. from: self.from as usize,
  270. to: self.to as usize,
  271. ty: self.ty,
  272. })
  273. }
  274. }
  275. // impl<'de> Deserialize<'de> for ViewDataType {
  276. // fn deserialize<D>(deserializer: D) -> Result<Self, <D as Deserializer<'de>>::Error>
  277. // where
  278. // D: Deserializer<'de>,
  279. // {
  280. // struct ViewTypeVisitor();
  281. //
  282. // impl<'de> Visitor<'de> for ViewTypeVisitor {
  283. // type Value = ViewDataType;
  284. // fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
  285. // formatter.write_str("RichText, PlainText")
  286. // }
  287. //
  288. // fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
  289. // where
  290. // E: de::Error,
  291. // {
  292. // let data_type;
  293. // match v {
  294. // 0 => {
  295. // data_type = ViewDataType::RichText;
  296. // }
  297. // 1 => {
  298. // data_type = ViewDataType::PlainText;
  299. // }
  300. // _ => {
  301. // return Err(de::Error::invalid_value(Unexpected::Unsigned(v as u64), &self));
  302. // }
  303. // }
  304. // Ok(data_type)
  305. // }
  306. //
  307. // fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
  308. // where
  309. // E: de::Error,
  310. // {
  311. // let data_type;
  312. // match s {
  313. // "Doc" | "RichText" => {
  314. // // Rename ViewDataType::Doc to ViewDataType::RichText, So we need to migrate the ViewType manually.
  315. // data_type = ViewDataType::RichText;
  316. // }
  317. // "PlainText" => {
  318. // data_type = ViewDataType::PlainText;
  319. // }
  320. // unknown => {
  321. // return Err(de::Error::invalid_value(Unexpected::Str(unknown), &self));
  322. // }
  323. // }
  324. // Ok(data_type)
  325. // }
  326. // }
  327. // deserializer.deserialize_any(ViewTypeVisitor())
  328. // }
  329. // }