view.rs 9.7 KB

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