field_entities.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  2. use flowy_error::ErrorCode;
  3. use grid_rev_model::{FieldRevision, FieldTypeRevision};
  4. use serde_repr::*;
  5. use std::sync::Arc;
  6. use crate::entities::parser::NotEmptyStr;
  7. use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString};
  8. /// [FieldPB] defines a Field's attributes. Such as the name, field_type, and width. etc.
  9. #[derive(Debug, Clone, Default, ProtoBuf)]
  10. pub struct FieldPB {
  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 FieldPB {
  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.ty.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 FieldPB {
  43. fn from(field_rev: Arc<FieldRevision>) -> Self {
  44. let field_rev = field_rev.as_ref().clone();
  45. FieldPB::from(field_rev)
  46. }
  47. }
  48. /// [FieldIdPB] id of the [Field]
  49. #[derive(Debug, Clone, Default, ProtoBuf)]
  50. pub struct FieldIdPB {
  51. #[pb(index = 1)]
  52. pub field_id: String,
  53. }
  54. impl std::convert::From<&str> for FieldIdPB {
  55. fn from(s: &str) -> Self {
  56. FieldIdPB { field_id: s.to_owned() }
  57. }
  58. }
  59. impl std::convert::From<String> for FieldIdPB {
  60. fn from(s: String) -> Self {
  61. FieldIdPB { field_id: s }
  62. }
  63. }
  64. impl std::convert::From<&Arc<FieldRevision>> for FieldIdPB {
  65. fn from(field_rev: &Arc<FieldRevision>) -> Self {
  66. Self {
  67. field_id: field_rev.id.clone(),
  68. }
  69. }
  70. }
  71. #[derive(Debug, Clone, Default, ProtoBuf)]
  72. pub struct GridFieldChangesetPB {
  73. #[pb(index = 1)]
  74. pub grid_id: String,
  75. #[pb(index = 2)]
  76. pub inserted_fields: Vec<IndexFieldPB>,
  77. #[pb(index = 3)]
  78. pub deleted_fields: Vec<FieldIdPB>,
  79. #[pb(index = 4)]
  80. pub updated_fields: Vec<FieldPB>,
  81. }
  82. impl GridFieldChangesetPB {
  83. pub fn insert(grid_id: &str, inserted_fields: Vec<IndexFieldPB>) -> Self {
  84. Self {
  85. grid_id: grid_id.to_owned(),
  86. inserted_fields,
  87. deleted_fields: vec![],
  88. updated_fields: vec![],
  89. }
  90. }
  91. pub fn delete(grid_id: &str, deleted_fields: Vec<FieldIdPB>) -> Self {
  92. Self {
  93. grid_id: grid_id.to_string(),
  94. inserted_fields: vec![],
  95. deleted_fields,
  96. updated_fields: vec![],
  97. }
  98. }
  99. pub fn update(grid_id: &str, updated_fields: Vec<FieldPB>) -> Self {
  100. Self {
  101. grid_id: grid_id.to_string(),
  102. inserted_fields: vec![],
  103. deleted_fields: vec![],
  104. updated_fields,
  105. }
  106. }
  107. }
  108. #[derive(Debug, Clone, Default, ProtoBuf)]
  109. pub struct IndexFieldPB {
  110. #[pb(index = 1)]
  111. pub field: FieldPB,
  112. #[pb(index = 2)]
  113. pub index: i32,
  114. }
  115. impl IndexFieldPB {
  116. pub fn from_field_rev(field_rev: &Arc<FieldRevision>, index: usize) -> Self {
  117. Self {
  118. field: FieldPB::from(field_rev.as_ref().clone()),
  119. index: index as i32,
  120. }
  121. }
  122. }
  123. #[derive(Debug, Default, ProtoBuf)]
  124. pub struct CreateFieldPayloadPB {
  125. #[pb(index = 1)]
  126. pub grid_id: String,
  127. #[pb(index = 2)]
  128. pub field_type: FieldType,
  129. #[pb(index = 3, one_of)]
  130. pub type_option_data: Option<Vec<u8>>,
  131. }
  132. #[derive(Clone)]
  133. pub struct CreateFieldParams {
  134. pub grid_id: String,
  135. pub field_type: FieldType,
  136. pub type_option_data: Option<Vec<u8>>,
  137. }
  138. impl TryInto<CreateFieldParams> for CreateFieldPayloadPB {
  139. type Error = ErrorCode;
  140. fn try_into(self) -> Result<CreateFieldParams, Self::Error> {
  141. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  142. Ok(CreateFieldParams {
  143. grid_id: grid_id.0,
  144. field_type: self.field_type,
  145. type_option_data: self.type_option_data,
  146. })
  147. }
  148. }
  149. #[derive(Debug, Default, ProtoBuf)]
  150. pub struct EditFieldChangesetPB {
  151. #[pb(index = 1)]
  152. pub grid_id: String,
  153. #[pb(index = 2)]
  154. pub field_id: String,
  155. #[pb(index = 3)]
  156. pub field_type: FieldType,
  157. #[pb(index = 4)]
  158. pub create_if_not_exist: bool,
  159. }
  160. pub struct EditFieldParams {
  161. pub grid_id: String,
  162. pub field_id: String,
  163. pub field_type: FieldType,
  164. }
  165. impl TryInto<EditFieldParams> for EditFieldChangesetPB {
  166. type Error = ErrorCode;
  167. fn try_into(self) -> Result<EditFieldParams, Self::Error> {
  168. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  169. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  170. Ok(EditFieldParams {
  171. grid_id: grid_id.0,
  172. field_id: field_id.0,
  173. field_type: self.field_type,
  174. })
  175. }
  176. }
  177. #[derive(Debug, Default, ProtoBuf)]
  178. pub struct TypeOptionPathPB {
  179. #[pb(index = 1)]
  180. pub grid_id: String,
  181. #[pb(index = 2)]
  182. pub field_id: String,
  183. #[pb(index = 3)]
  184. pub field_type: FieldType,
  185. }
  186. pub struct TypeOptionPathParams {
  187. pub grid_id: String,
  188. pub field_id: String,
  189. pub field_type: FieldType,
  190. }
  191. impl TryInto<TypeOptionPathParams> for TypeOptionPathPB {
  192. type Error = ErrorCode;
  193. fn try_into(self) -> Result<TypeOptionPathParams, Self::Error> {
  194. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  195. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  196. Ok(TypeOptionPathParams {
  197. grid_id: grid_id.0,
  198. field_id: field_id.0,
  199. field_type: self.field_type,
  200. })
  201. }
  202. }
  203. #[derive(Debug, Default, ProtoBuf)]
  204. pub struct TypeOptionPB {
  205. #[pb(index = 1)]
  206. pub grid_id: String,
  207. #[pb(index = 2)]
  208. pub field: FieldPB,
  209. #[pb(index = 3)]
  210. pub type_option_data: Vec<u8>,
  211. }
  212. /// Collection of the [FieldPB]
  213. #[derive(Debug, Default, ProtoBuf)]
  214. pub struct RepeatedFieldPB {
  215. #[pb(index = 1)]
  216. pub items: Vec<FieldPB>,
  217. }
  218. impl std::ops::Deref for RepeatedFieldPB {
  219. type Target = Vec<FieldPB>;
  220. fn deref(&self) -> &Self::Target {
  221. &self.items
  222. }
  223. }
  224. impl std::ops::DerefMut for RepeatedFieldPB {
  225. fn deref_mut(&mut self) -> &mut Self::Target {
  226. &mut self.items
  227. }
  228. }
  229. impl std::convert::From<Vec<FieldPB>> for RepeatedFieldPB {
  230. fn from(items: Vec<FieldPB>) -> Self {
  231. Self { items }
  232. }
  233. }
  234. #[derive(Debug, Clone, Default, ProtoBuf)]
  235. pub struct RepeatedFieldIdPB {
  236. #[pb(index = 1)]
  237. pub items: Vec<FieldIdPB>,
  238. }
  239. impl std::ops::Deref for RepeatedFieldIdPB {
  240. type Target = Vec<FieldIdPB>;
  241. fn deref(&self) -> &Self::Target {
  242. &self.items
  243. }
  244. }
  245. impl std::convert::From<Vec<FieldIdPB>> for RepeatedFieldIdPB {
  246. fn from(items: Vec<FieldIdPB>) -> Self {
  247. RepeatedFieldIdPB { items }
  248. }
  249. }
  250. impl std::convert::From<String> for RepeatedFieldIdPB {
  251. fn from(s: String) -> Self {
  252. RepeatedFieldIdPB {
  253. items: vec![FieldIdPB::from(s)],
  254. }
  255. }
  256. }
  257. /// [TypeOptionChangesetPB] is used to update the type-option data.
  258. #[derive(ProtoBuf, Default)]
  259. pub struct TypeOptionChangesetPB {
  260. #[pb(index = 1)]
  261. pub grid_id: String,
  262. #[pb(index = 2)]
  263. pub field_id: String,
  264. /// Check out [TypeOptionPB] for more details.
  265. #[pb(index = 3)]
  266. pub type_option_data: Vec<u8>,
  267. }
  268. #[derive(Clone)]
  269. pub struct TypeOptionChangesetParams {
  270. pub grid_id: String,
  271. pub field_id: String,
  272. pub type_option_data: Vec<u8>,
  273. }
  274. impl TryInto<TypeOptionChangesetParams> for TypeOptionChangesetPB {
  275. type Error = ErrorCode;
  276. fn try_into(self) -> Result<TypeOptionChangesetParams, Self::Error> {
  277. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  278. let _ = NotEmptyStr::parse(self.field_id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  279. Ok(TypeOptionChangesetParams {
  280. grid_id: grid_id.0,
  281. field_id: self.field_id,
  282. type_option_data: self.type_option_data,
  283. })
  284. }
  285. }
  286. #[derive(ProtoBuf, Default)]
  287. pub struct GetFieldPayloadPB {
  288. #[pb(index = 1)]
  289. pub grid_id: String,
  290. #[pb(index = 2, one_of)]
  291. pub field_ids: Option<RepeatedFieldIdPB>,
  292. }
  293. pub struct GetFieldParams {
  294. pub grid_id: String,
  295. pub field_ids: Option<Vec<String>>,
  296. }
  297. impl TryInto<GetFieldParams> for GetFieldPayloadPB {
  298. type Error = ErrorCode;
  299. fn try_into(self) -> Result<GetFieldParams, Self::Error> {
  300. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  301. let field_ids = self.field_ids.map(|repeated| {
  302. repeated
  303. .items
  304. .into_iter()
  305. .map(|item| item.field_id)
  306. .collect::<Vec<String>>()
  307. });
  308. Ok(GetFieldParams {
  309. grid_id: grid_id.0,
  310. field_ids,
  311. })
  312. }
  313. }
  314. /// [FieldChangesetPB] is used to modify the corresponding field. It defines which properties of
  315. /// the field can be modified.
  316. ///
  317. /// Pass in None if you don't want to modify a property
  318. /// Pass in Some(Value) if you want to modify a property
  319. ///
  320. #[derive(Debug, Clone, Default, ProtoBuf)]
  321. pub struct FieldChangesetPB {
  322. #[pb(index = 1)]
  323. pub field_id: String,
  324. #[pb(index = 2)]
  325. pub grid_id: String,
  326. #[pb(index = 3, one_of)]
  327. pub name: Option<String>,
  328. #[pb(index = 4, one_of)]
  329. pub desc: Option<String>,
  330. #[pb(index = 5, one_of)]
  331. pub field_type: Option<FieldType>,
  332. #[pb(index = 6, one_of)]
  333. pub frozen: Option<bool>,
  334. #[pb(index = 7, one_of)]
  335. pub visibility: Option<bool>,
  336. #[pb(index = 8, one_of)]
  337. pub width: Option<i32>,
  338. // #[pb(index = 9, one_of)]
  339. // pub type_option_data: Option<Vec<u8>>,
  340. }
  341. impl TryInto<FieldChangesetParams> for FieldChangesetPB {
  342. type Error = ErrorCode;
  343. fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
  344. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  345. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  346. let field_type = self.field_type.map(FieldTypeRevision::from);
  347. // if let Some(type_option_data) = self.type_option_data.as_ref() {
  348. // if type_option_data.is_empty() {
  349. // return Err(ErrorCode::TypeOptionDataIsEmpty);
  350. // }
  351. // }
  352. Ok(FieldChangesetParams {
  353. field_id: field_id.0,
  354. grid_id: grid_id.0,
  355. name: self.name,
  356. desc: self.desc,
  357. field_type,
  358. frozen: self.frozen,
  359. visibility: self.visibility,
  360. width: self.width,
  361. // type_option_data: self.type_option_data,
  362. })
  363. }
  364. }
  365. #[derive(Debug, Clone, Default)]
  366. pub struct FieldChangesetParams {
  367. pub field_id: String,
  368. pub grid_id: String,
  369. pub name: Option<String>,
  370. pub desc: Option<String>,
  371. pub field_type: Option<FieldTypeRevision>,
  372. pub frozen: Option<bool>,
  373. pub visibility: Option<bool>,
  374. pub width: Option<i32>,
  375. // pub type_option_data: Option<Vec<u8>>,
  376. }
  377. /// Certain field types have user-defined options such as color, date format, number format,
  378. /// or a list of values for a multi-select list. These options are defined within a specialization
  379. /// of the FieldTypeOption class.
  380. ///
  381. /// You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/frontend/grid#fieldtype)
  382. /// for more information.
  383. ///
  384. /// The order of the enum can't be changed. If you want to add a new type,
  385. /// it would be better to append it to the end of the list.
  386. #[derive(
  387. Debug,
  388. Clone,
  389. PartialEq,
  390. Hash,
  391. Eq,
  392. ProtoBuf_Enum,
  393. EnumCountMacro,
  394. EnumString,
  395. EnumIter,
  396. Display,
  397. Serialize_repr,
  398. Deserialize_repr,
  399. )]
  400. #[repr(u8)]
  401. pub enum FieldType {
  402. RichText = 0,
  403. Number = 1,
  404. DateTime = 2,
  405. SingleSelect = 3,
  406. MultiSelect = 4,
  407. Checkbox = 5,
  408. URL = 6,
  409. }
  410. pub const RICH_TEXT_FIELD: FieldType = FieldType::RichText;
  411. pub const NUMBER_FIELD: FieldType = FieldType::Number;
  412. pub const DATE_FIELD: FieldType = FieldType::DateTime;
  413. pub const SINGLE_SELECT_FIELD: FieldType = FieldType::SingleSelect;
  414. pub const MULTI_SELECT_FIELD: FieldType = FieldType::MultiSelect;
  415. pub const CHECKBOX_FIELD: FieldType = FieldType::Checkbox;
  416. pub const URL_FIELD: FieldType = FieldType::URL;
  417. impl std::default::Default for FieldType {
  418. fn default() -> Self {
  419. FieldType::RichText
  420. }
  421. }
  422. impl AsRef<FieldType> for FieldType {
  423. fn as_ref(&self) -> &FieldType {
  424. self
  425. }
  426. }
  427. impl From<&FieldType> for FieldType {
  428. fn from(field_type: &FieldType) -> Self {
  429. field_type.clone()
  430. }
  431. }
  432. impl FieldType {
  433. pub fn type_id(&self) -> String {
  434. (self.clone() as u8).to_string()
  435. }
  436. pub fn default_cell_width(&self) -> i32 {
  437. match self {
  438. FieldType::DateTime => 180,
  439. _ => 150,
  440. }
  441. }
  442. pub fn is_number(&self) -> bool {
  443. self == &NUMBER_FIELD
  444. }
  445. pub fn is_text(&self) -> bool {
  446. self == &RICH_TEXT_FIELD
  447. }
  448. pub fn is_checkbox(&self) -> bool {
  449. self == &CHECKBOX_FIELD
  450. }
  451. pub fn is_date(&self) -> bool {
  452. self == &DATE_FIELD
  453. }
  454. pub fn is_single_select(&self) -> bool {
  455. self == &SINGLE_SELECT_FIELD
  456. }
  457. pub fn is_multi_select(&self) -> bool {
  458. self == &MULTI_SELECT_FIELD
  459. }
  460. pub fn is_url(&self) -> bool {
  461. self == &URL_FIELD
  462. }
  463. pub fn is_select_option(&self) -> bool {
  464. self == &MULTI_SELECT_FIELD || self == &SINGLE_SELECT_FIELD
  465. }
  466. pub fn can_be_group(&self) -> bool {
  467. self.is_select_option()
  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<FieldIdParams> for DuplicateFieldPayloadPB {
  517. type Error = ErrorCode;
  518. fn try_into(self) -> Result<FieldIdParams, 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(FieldIdParams {
  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<FieldIdParams> for DeleteFieldPayloadPB {
  535. type Error = ErrorCode;
  536. fn try_into(self) -> Result<FieldIdParams, 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(FieldIdParams {
  540. grid_id: grid_id.0,
  541. field_id: field_id.0,
  542. })
  543. }
  544. }
  545. pub struct FieldIdParams {
  546. pub field_id: String,
  547. pub grid_id: String,
  548. }