field_entities.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  2. use flowy_error::ErrorCode;
  3. use flowy_grid_data_model::parser::NotEmptyStr;
  4. use flowy_grid_data_model::revision::{FieldRevision, FieldTypeRevision};
  5. use flowy_sync::entities::grid::FieldChangesetParams;
  6. use serde_repr::*;
  7. use std::sync::Arc;
  8. use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString};
  9. #[derive(Debug, Clone, Default, ProtoBuf)]
  10. pub struct GridFieldPB {
  11. #[pb(index = 1)]
  12. pub id: String,
  13. #[pb(index = 2)]
  14. pub name: String,
  15. #[pb(index = 3)]
  16. pub desc: String,
  17. #[pb(index = 4)]
  18. pub field_type: FieldType,
  19. #[pb(index = 5)]
  20. pub frozen: bool,
  21. #[pb(index = 6)]
  22. pub visibility: bool,
  23. #[pb(index = 7)]
  24. pub width: i32,
  25. #[pb(index = 8)]
  26. pub is_primary: bool,
  27. }
  28. impl std::convert::From<FieldRevision> for GridFieldPB {
  29. fn from(field_rev: FieldRevision) -> Self {
  30. Self {
  31. id: field_rev.id,
  32. name: field_rev.name,
  33. desc: field_rev.desc,
  34. field_type: field_rev.field_type_rev.into(),
  35. frozen: field_rev.frozen,
  36. visibility: field_rev.visibility,
  37. width: field_rev.width,
  38. is_primary: field_rev.is_primary,
  39. }
  40. }
  41. }
  42. impl std::convert::From<Arc<FieldRevision>> for GridFieldPB {
  43. fn from(field_rev: Arc<FieldRevision>) -> Self {
  44. let field_rev = field_rev.as_ref().clone();
  45. GridFieldPB::from(field_rev)
  46. }
  47. }
  48. #[derive(Debug, Clone, Default, ProtoBuf)]
  49. pub struct GridFieldIdPB {
  50. #[pb(index = 1)]
  51. pub field_id: String,
  52. }
  53. impl std::convert::From<&str> for GridFieldIdPB {
  54. fn from(s: &str) -> Self {
  55. GridFieldIdPB { field_id: s.to_owned() }
  56. }
  57. }
  58. impl std::convert::From<String> for GridFieldIdPB {
  59. fn from(s: String) -> Self {
  60. GridFieldIdPB { field_id: s }
  61. }
  62. }
  63. impl std::convert::From<&Arc<FieldRevision>> for GridFieldIdPB {
  64. fn from(field_rev: &Arc<FieldRevision>) -> Self {
  65. Self {
  66. field_id: field_rev.id.clone(),
  67. }
  68. }
  69. }
  70. #[derive(Debug, Clone, Default, ProtoBuf)]
  71. pub struct GridFieldChangesetPB {
  72. #[pb(index = 1)]
  73. pub grid_id: String,
  74. #[pb(index = 2)]
  75. pub inserted_fields: Vec<IndexFieldPB>,
  76. #[pb(index = 3)]
  77. pub deleted_fields: Vec<GridFieldIdPB>,
  78. #[pb(index = 4)]
  79. pub updated_fields: Vec<GridFieldPB>,
  80. }
  81. impl GridFieldChangesetPB {
  82. pub fn insert(grid_id: &str, inserted_fields: Vec<IndexFieldPB>) -> Self {
  83. Self {
  84. grid_id: grid_id.to_owned(),
  85. inserted_fields,
  86. deleted_fields: vec![],
  87. updated_fields: vec![],
  88. }
  89. }
  90. pub fn delete(grid_id: &str, deleted_fields: Vec<GridFieldIdPB>) -> Self {
  91. Self {
  92. grid_id: grid_id.to_string(),
  93. inserted_fields: vec![],
  94. deleted_fields,
  95. updated_fields: vec![],
  96. }
  97. }
  98. pub fn update(grid_id: &str, updated_fields: Vec<GridFieldPB>) -> Self {
  99. Self {
  100. grid_id: grid_id.to_string(),
  101. inserted_fields: vec![],
  102. deleted_fields: vec![],
  103. updated_fields,
  104. }
  105. }
  106. }
  107. #[derive(Debug, Clone, Default, ProtoBuf)]
  108. pub struct IndexFieldPB {
  109. #[pb(index = 1)]
  110. pub field: GridFieldPB,
  111. #[pb(index = 2)]
  112. pub index: i32,
  113. }
  114. impl IndexFieldPB {
  115. pub fn from_field_rev(field_rev: &Arc<FieldRevision>, index: usize) -> Self {
  116. Self {
  117. field: GridFieldPB::from(field_rev.as_ref().clone()),
  118. index: index as i32,
  119. }
  120. }
  121. }
  122. #[derive(Debug, Default, ProtoBuf)]
  123. pub struct GetEditFieldContextPayloadPB {
  124. #[pb(index = 1)]
  125. pub grid_id: String,
  126. #[pb(index = 2, one_of)]
  127. pub field_id: Option<String>,
  128. #[pb(index = 3)]
  129. pub field_type: FieldType,
  130. }
  131. #[derive(Debug, Default, ProtoBuf)]
  132. pub struct CreateFieldPayloadPB {
  133. #[pb(index = 1)]
  134. pub grid_id: String,
  135. #[pb(index = 2)]
  136. pub field_id: String,
  137. #[pb(index = 3)]
  138. pub field_type: FieldType,
  139. #[pb(index = 4)]
  140. pub create_if_not_exist: bool,
  141. }
  142. pub struct CreateFieldParams {
  143. pub grid_id: String,
  144. pub field_id: String,
  145. pub field_type: FieldType,
  146. }
  147. impl TryInto<CreateFieldParams> for CreateFieldPayloadPB {
  148. type Error = ErrorCode;
  149. fn try_into(self) -> Result<CreateFieldParams, Self::Error> {
  150. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  151. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  152. Ok(CreateFieldParams {
  153. grid_id: grid_id.0,
  154. field_id: field_id.0,
  155. field_type: self.field_type,
  156. })
  157. }
  158. }
  159. #[derive(Debug, Default, ProtoBuf)]
  160. pub struct EditFieldPayloadPB {
  161. #[pb(index = 1)]
  162. pub grid_id: String,
  163. #[pb(index = 2)]
  164. pub field_id: String,
  165. #[pb(index = 3)]
  166. pub field_type: FieldType,
  167. #[pb(index = 4)]
  168. pub create_if_not_exist: bool,
  169. }
  170. pub struct EditFieldParams {
  171. pub grid_id: String,
  172. pub field_id: String,
  173. pub field_type: FieldType,
  174. }
  175. impl TryInto<EditFieldParams> for EditFieldPayloadPB {
  176. type Error = ErrorCode;
  177. fn try_into(self) -> Result<EditFieldParams, Self::Error> {
  178. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  179. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  180. Ok(EditFieldParams {
  181. grid_id: grid_id.0,
  182. field_id: field_id.0,
  183. field_type: self.field_type,
  184. })
  185. }
  186. }
  187. #[derive(Debug, Default, ProtoBuf)]
  188. pub struct GridFieldTypeOptionIdPB {
  189. #[pb(index = 1)]
  190. pub grid_id: String,
  191. #[pb(index = 2)]
  192. pub field_id: String,
  193. #[pb(index = 3)]
  194. pub field_type: FieldType,
  195. }
  196. pub struct GridFieldTypeOptionIdParams {
  197. pub grid_id: String,
  198. pub field_id: String,
  199. pub field_type: FieldType,
  200. }
  201. impl TryInto<GridFieldTypeOptionIdParams> for GridFieldTypeOptionIdPB {
  202. type Error = ErrorCode;
  203. fn try_into(self) -> Result<GridFieldTypeOptionIdParams, Self::Error> {
  204. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  205. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  206. Ok(GridFieldTypeOptionIdParams {
  207. grid_id: grid_id.0,
  208. field_id: field_id.0,
  209. field_type: self.field_type,
  210. })
  211. }
  212. }
  213. #[derive(Debug, Default, ProtoBuf)]
  214. pub struct FieldTypeOptionDataPB {
  215. #[pb(index = 1)]
  216. pub grid_id: String,
  217. #[pb(index = 2)]
  218. pub field: GridFieldPB,
  219. #[pb(index = 3)]
  220. pub type_option_data: Vec<u8>,
  221. }
  222. #[derive(Debug, Default, ProtoBuf)]
  223. pub struct RepeatedGridFieldPB {
  224. #[pb(index = 1)]
  225. pub items: Vec<GridFieldPB>,
  226. }
  227. impl std::ops::Deref for RepeatedGridFieldPB {
  228. type Target = Vec<GridFieldPB>;
  229. fn deref(&self) -> &Self::Target {
  230. &self.items
  231. }
  232. }
  233. impl std::ops::DerefMut for RepeatedGridFieldPB {
  234. fn deref_mut(&mut self) -> &mut Self::Target {
  235. &mut self.items
  236. }
  237. }
  238. impl std::convert::From<Vec<GridFieldPB>> for RepeatedGridFieldPB {
  239. fn from(items: Vec<GridFieldPB>) -> Self {
  240. Self { items }
  241. }
  242. }
  243. #[derive(Debug, Clone, Default, ProtoBuf)]
  244. pub struct RepeatedGridFieldIdPB {
  245. #[pb(index = 1)]
  246. pub items: Vec<GridFieldIdPB>,
  247. }
  248. impl std::ops::Deref for RepeatedGridFieldIdPB {
  249. type Target = Vec<GridFieldIdPB>;
  250. fn deref(&self) -> &Self::Target {
  251. &self.items
  252. }
  253. }
  254. impl std::convert::From<Vec<GridFieldIdPB>> for RepeatedGridFieldIdPB {
  255. fn from(items: Vec<GridFieldIdPB>) -> Self {
  256. RepeatedGridFieldIdPB { items }
  257. }
  258. }
  259. impl std::convert::From<String> for RepeatedGridFieldIdPB {
  260. fn from(s: String) -> Self {
  261. RepeatedGridFieldIdPB {
  262. items: vec![GridFieldIdPB::from(s)],
  263. }
  264. }
  265. }
  266. #[derive(ProtoBuf, Default)]
  267. pub struct InsertFieldPayloadPB {
  268. #[pb(index = 1)]
  269. pub grid_id: String,
  270. #[pb(index = 2)]
  271. pub field: GridFieldPB,
  272. #[pb(index = 3)]
  273. pub type_option_data: Vec<u8>,
  274. #[pb(index = 4, one_of)]
  275. pub start_field_id: Option<String>,
  276. }
  277. #[derive(Clone)]
  278. pub struct InsertFieldParams {
  279. pub grid_id: String,
  280. pub field: GridFieldPB,
  281. pub type_option_data: Vec<u8>,
  282. pub start_field_id: Option<String>,
  283. }
  284. impl TryInto<InsertFieldParams> for InsertFieldPayloadPB {
  285. type Error = ErrorCode;
  286. fn try_into(self) -> Result<InsertFieldParams, Self::Error> {
  287. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  288. let _ = NotEmptyStr::parse(self.field.id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  289. let start_field_id = match self.start_field_id {
  290. None => None,
  291. Some(id) => Some(NotEmptyStr::parse(id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
  292. };
  293. Ok(InsertFieldParams {
  294. grid_id: grid_id.0,
  295. field: self.field,
  296. type_option_data: self.type_option_data,
  297. start_field_id,
  298. })
  299. }
  300. }
  301. #[derive(ProtoBuf, Default)]
  302. pub struct UpdateFieldTypeOptionPayloadPB {
  303. #[pb(index = 1)]
  304. pub grid_id: String,
  305. #[pb(index = 2)]
  306. pub field_id: String,
  307. #[pb(index = 3)]
  308. pub type_option_data: Vec<u8>,
  309. }
  310. #[derive(Clone)]
  311. pub struct UpdateFieldTypeOptionParams {
  312. pub grid_id: String,
  313. pub field_id: String,
  314. pub type_option_data: Vec<u8>,
  315. }
  316. impl TryInto<UpdateFieldTypeOptionParams> for UpdateFieldTypeOptionPayloadPB {
  317. type Error = ErrorCode;
  318. fn try_into(self) -> Result<UpdateFieldTypeOptionParams, Self::Error> {
  319. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  320. let _ = NotEmptyStr::parse(self.field_id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  321. Ok(UpdateFieldTypeOptionParams {
  322. grid_id: grid_id.0,
  323. field_id: self.field_id,
  324. type_option_data: self.type_option_data,
  325. })
  326. }
  327. }
  328. #[derive(ProtoBuf, Default)]
  329. pub struct QueryFieldPayloadPB {
  330. #[pb(index = 1)]
  331. pub grid_id: String,
  332. #[pb(index = 2)]
  333. pub field_ids: RepeatedGridFieldIdPB,
  334. }
  335. pub struct QueryFieldParams {
  336. pub grid_id: String,
  337. pub field_ids: RepeatedGridFieldIdPB,
  338. }
  339. impl TryInto<QueryFieldParams> for QueryFieldPayloadPB {
  340. type Error = ErrorCode;
  341. fn try_into(self) -> Result<QueryFieldParams, Self::Error> {
  342. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  343. Ok(QueryFieldParams {
  344. grid_id: grid_id.0,
  345. field_ids: self.field_ids,
  346. })
  347. }
  348. }
  349. #[derive(Debug, Clone, Default, ProtoBuf)]
  350. pub struct FieldChangesetPayloadPB {
  351. #[pb(index = 1)]
  352. pub field_id: String,
  353. #[pb(index = 2)]
  354. pub grid_id: String,
  355. #[pb(index = 3, one_of)]
  356. pub name: Option<String>,
  357. #[pb(index = 4, one_of)]
  358. pub desc: Option<String>,
  359. #[pb(index = 5, one_of)]
  360. pub field_type: Option<FieldType>,
  361. #[pb(index = 6, one_of)]
  362. pub frozen: Option<bool>,
  363. #[pb(index = 7, one_of)]
  364. pub visibility: Option<bool>,
  365. #[pb(index = 8, one_of)]
  366. pub width: Option<i32>,
  367. #[pb(index = 9, one_of)]
  368. pub type_option_data: Option<Vec<u8>>,
  369. }
  370. impl TryInto<FieldChangesetParams> for FieldChangesetPayloadPB {
  371. type Error = ErrorCode;
  372. fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
  373. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  374. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  375. let field_type = self.field_type.map(FieldTypeRevision::from);
  376. if let Some(type_option_data) = self.type_option_data.as_ref() {
  377. if type_option_data.is_empty() {
  378. return Err(ErrorCode::TypeOptionDataIsEmpty);
  379. }
  380. }
  381. Ok(FieldChangesetParams {
  382. field_id: field_id.0,
  383. grid_id: grid_id.0,
  384. name: self.name,
  385. desc: self.desc,
  386. field_type,
  387. frozen: self.frozen,
  388. visibility: self.visibility,
  389. width: self.width,
  390. type_option_data: self.type_option_data,
  391. })
  392. }
  393. }
  394. #[derive(
  395. Debug,
  396. Clone,
  397. PartialEq,
  398. Hash,
  399. Eq,
  400. ProtoBuf_Enum,
  401. EnumCountMacro,
  402. EnumString,
  403. EnumIter,
  404. Display,
  405. Serialize_repr,
  406. Deserialize_repr,
  407. )]
  408. /// The order of the enum can't be changed. If you want to add a new type,
  409. /// it would be better to append it to the end of the list.
  410. #[repr(u8)]
  411. pub enum FieldType {
  412. RichText = 0,
  413. Number = 1,
  414. DateTime = 2,
  415. SingleSelect = 3,
  416. MultiSelect = 4,
  417. Checkbox = 5,
  418. URL = 6,
  419. }
  420. impl std::default::Default for FieldType {
  421. fn default() -> Self {
  422. FieldType::RichText
  423. }
  424. }
  425. impl AsRef<FieldType> for FieldType {
  426. fn as_ref(&self) -> &FieldType {
  427. self
  428. }
  429. }
  430. impl From<&FieldType> for FieldType {
  431. fn from(field_type: &FieldType) -> Self {
  432. field_type.clone()
  433. }
  434. }
  435. impl FieldType {
  436. pub fn type_id(&self) -> String {
  437. (self.clone() as u8).to_string()
  438. }
  439. pub fn default_cell_width(&self) -> i32 {
  440. match self {
  441. FieldType::DateTime => 180,
  442. _ => 150,
  443. }
  444. }
  445. pub fn is_number(&self) -> bool {
  446. self == &FieldType::Number
  447. }
  448. pub fn is_text(&self) -> bool {
  449. self == &FieldType::RichText
  450. }
  451. pub fn is_checkbox(&self) -> bool {
  452. self == &FieldType::Checkbox
  453. }
  454. pub fn is_date(&self) -> bool {
  455. self == &FieldType::DateTime
  456. }
  457. pub fn is_single_select(&self) -> bool {
  458. self == &FieldType::SingleSelect
  459. }
  460. pub fn is_multi_select(&self) -> bool {
  461. self == &FieldType::MultiSelect
  462. }
  463. pub fn is_url(&self) -> bool {
  464. self == &FieldType::URL
  465. }
  466. pub fn is_select_option(&self) -> bool {
  467. self == &FieldType::MultiSelect || self == &FieldType::SingleSelect
  468. }
  469. }
  470. impl std::convert::From<&FieldType> for FieldTypeRevision {
  471. fn from(ty: &FieldType) -> Self {
  472. ty.clone() as u8
  473. }
  474. }
  475. impl std::convert::From<FieldType> for FieldTypeRevision {
  476. fn from(ty: FieldType) -> Self {
  477. ty as u8
  478. }
  479. }
  480. impl std::convert::From<&FieldTypeRevision> for FieldType {
  481. fn from(ty: &FieldTypeRevision) -> Self {
  482. FieldType::from(*ty)
  483. }
  484. }
  485. impl std::convert::From<FieldTypeRevision> for FieldType {
  486. fn from(ty: FieldTypeRevision) -> Self {
  487. match ty {
  488. 0 => FieldType::RichText,
  489. 1 => FieldType::Number,
  490. 2 => FieldType::DateTime,
  491. 3 => FieldType::SingleSelect,
  492. 4 => FieldType::MultiSelect,
  493. 5 => FieldType::Checkbox,
  494. 6 => FieldType::URL,
  495. _ => {
  496. tracing::error!("Can't parser FieldTypeRevision: {} to FieldType", ty);
  497. FieldType::RichText
  498. }
  499. }
  500. }
  501. }
  502. #[derive(Debug, Clone, Default, ProtoBuf)]
  503. pub struct DuplicateFieldPayloadPB {
  504. #[pb(index = 1)]
  505. pub field_id: String,
  506. #[pb(index = 2)]
  507. pub grid_id: String,
  508. }
  509. #[derive(Debug, Clone, Default, ProtoBuf)]
  510. pub struct GridFieldIdentifierPayloadPB {
  511. #[pb(index = 1)]
  512. pub field_id: String,
  513. #[pb(index = 2)]
  514. pub grid_id: String,
  515. }
  516. impl TryInto<GridFieldIdParams> for DuplicateFieldPayloadPB {
  517. type Error = ErrorCode;
  518. fn try_into(self) -> Result<GridFieldIdParams, Self::Error> {
  519. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  520. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  521. Ok(GridFieldIdParams {
  522. grid_id: grid_id.0,
  523. field_id: field_id.0,
  524. })
  525. }
  526. }
  527. #[derive(Debug, Clone, Default, ProtoBuf)]
  528. pub struct DeleteFieldPayloadPB {
  529. #[pb(index = 1)]
  530. pub field_id: String,
  531. #[pb(index = 2)]
  532. pub grid_id: String,
  533. }
  534. impl TryInto<GridFieldIdParams> for DeleteFieldPayloadPB {
  535. type Error = ErrorCode;
  536. fn try_into(self) -> Result<GridFieldIdParams, Self::Error> {
  537. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  538. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  539. Ok(GridFieldIdParams {
  540. grid_id: grid_id.0,
  541. field_id: field_id.0,
  542. })
  543. }
  544. }
  545. pub struct GridFieldIdParams {
  546. pub field_id: String,
  547. pub grid_id: String,
  548. }