view.rs 10 KB

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