view.rs 8.5 KB

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