grid.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. use crate::entities::{CellMeta, FieldMeta, RowMeta, RowMetaChangeset};
  2. use crate::parser::NotEmptyStr;
  3. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  4. use flowy_error_code::ErrorCode;
  5. use serde_repr::*;
  6. use std::collections::HashMap;
  7. use std::sync::Arc;
  8. use strum_macros::{Display, EnumCount as EnumCountMacro, EnumIter, EnumString};
  9. #[derive(Debug, Clone, Default, ProtoBuf)]
  10. pub struct Grid {
  11. #[pb(index = 1)]
  12. pub id: String,
  13. #[pb(index = 2)]
  14. pub field_orders: Vec<FieldOrder>,
  15. #[pb(index = 3)]
  16. pub block_orders: Vec<GridBlockOrder>,
  17. }
  18. #[derive(Debug, Clone, Default, ProtoBuf)]
  19. pub struct Field {
  20. #[pb(index = 1)]
  21. pub id: String,
  22. #[pb(index = 2)]
  23. pub name: String,
  24. #[pb(index = 3)]
  25. pub desc: String,
  26. #[pb(index = 4)]
  27. pub field_type: FieldType,
  28. #[pb(index = 5)]
  29. pub frozen: bool,
  30. #[pb(index = 6)]
  31. pub visibility: bool,
  32. #[pb(index = 7)]
  33. pub width: i32,
  34. #[pb(index = 8)]
  35. pub is_primary: bool,
  36. }
  37. impl std::convert::From<FieldMeta> for Field {
  38. fn from(field_meta: FieldMeta) -> Self {
  39. Self {
  40. id: field_meta.id,
  41. name: field_meta.name,
  42. desc: field_meta.desc,
  43. field_type: field_meta.field_type,
  44. frozen: field_meta.frozen,
  45. visibility: field_meta.visibility,
  46. width: field_meta.width,
  47. is_primary: field_meta.is_primary,
  48. }
  49. }
  50. }
  51. #[derive(Debug, Clone, Default, ProtoBuf)]
  52. pub struct FieldOrder {
  53. #[pb(index = 1)]
  54. pub field_id: String,
  55. }
  56. impl std::convert::From<&FieldMeta> for FieldOrder {
  57. fn from(field_meta: &FieldMeta) -> Self {
  58. Self {
  59. field_id: field_meta.id.clone(),
  60. }
  61. }
  62. }
  63. impl std::convert::From<&str> for FieldOrder {
  64. fn from(s: &str) -> Self {
  65. FieldOrder { field_id: s.to_owned() }
  66. }
  67. }
  68. impl std::convert::From<String> for FieldOrder {
  69. fn from(s: String) -> Self {
  70. FieldOrder { field_id: s }
  71. }
  72. }
  73. #[derive(Debug, Clone, Default, ProtoBuf)]
  74. pub struct GridFieldChangeset {
  75. #[pb(index = 1)]
  76. pub grid_id: String,
  77. #[pb(index = 2)]
  78. pub inserted_fields: Vec<IndexField>,
  79. #[pb(index = 3)]
  80. pub deleted_fields: Vec<FieldOrder>,
  81. #[pb(index = 4)]
  82. pub updated_fields: Vec<Field>,
  83. }
  84. impl GridFieldChangeset {
  85. pub fn insert(grid_id: &str, inserted_fields: Vec<IndexField>) -> Self {
  86. Self {
  87. grid_id: grid_id.to_owned(),
  88. inserted_fields,
  89. deleted_fields: vec![],
  90. updated_fields: vec![],
  91. }
  92. }
  93. pub fn delete(grid_id: &str, deleted_fields: Vec<FieldOrder>) -> Self {
  94. Self {
  95. grid_id: grid_id.to_string(),
  96. inserted_fields: vec![],
  97. deleted_fields,
  98. updated_fields: vec![],
  99. }
  100. }
  101. pub fn update(grid_id: &str, updated_fields: Vec<Field>) -> Self {
  102. Self {
  103. grid_id: grid_id.to_string(),
  104. inserted_fields: vec![],
  105. deleted_fields: vec![],
  106. updated_fields,
  107. }
  108. }
  109. }
  110. #[derive(Debug, Clone, Default, ProtoBuf)]
  111. pub struct IndexField {
  112. #[pb(index = 1)]
  113. pub field: Field,
  114. #[pb(index = 2)]
  115. pub index: i32,
  116. }
  117. impl IndexField {
  118. pub fn from_field_meta(field_meta: &FieldMeta, index: usize) -> Self {
  119. Self {
  120. field: Field::from(field_meta.clone()),
  121. index: index as i32,
  122. }
  123. }
  124. }
  125. #[derive(Debug, Default, ProtoBuf)]
  126. pub struct GetEditFieldContextPayload {
  127. #[pb(index = 1)]
  128. pub grid_id: String,
  129. #[pb(index = 2, one_of)]
  130. pub field_id: Option<String>,
  131. #[pb(index = 3)]
  132. pub field_type: FieldType,
  133. }
  134. #[derive(Debug, Default, ProtoBuf)]
  135. pub struct EditFieldPayload {
  136. #[pb(index = 1)]
  137. pub grid_id: String,
  138. #[pb(index = 2)]
  139. pub field_id: String,
  140. #[pb(index = 3)]
  141. pub field_type: FieldType,
  142. #[pb(index = 4)]
  143. pub create_if_not_exist: bool,
  144. }
  145. pub struct EditFieldParams {
  146. pub grid_id: String,
  147. pub field_id: String,
  148. pub field_type: FieldType,
  149. }
  150. impl TryInto<EditFieldParams> for EditFieldPayload {
  151. type Error = ErrorCode;
  152. fn try_into(self) -> Result<EditFieldParams, Self::Error> {
  153. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  154. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  155. Ok(EditFieldParams {
  156. grid_id: grid_id.0,
  157. field_id: field_id.0,
  158. field_type: self.field_type,
  159. })
  160. }
  161. }
  162. pub struct CreateFieldParams {
  163. pub grid_id: String,
  164. pub field_type: FieldType,
  165. }
  166. impl TryInto<CreateFieldParams> for EditFieldPayload {
  167. type Error = ErrorCode;
  168. fn try_into(self) -> Result<CreateFieldParams, Self::Error> {
  169. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  170. Ok(CreateFieldParams {
  171. grid_id: grid_id.0,
  172. field_type: self.field_type,
  173. })
  174. }
  175. }
  176. #[derive(Debug, Default, ProtoBuf)]
  177. pub struct FieldTypeOptionContext {
  178. #[pb(index = 1)]
  179. pub grid_id: String,
  180. #[pb(index = 2)]
  181. pub grid_field: Field,
  182. #[pb(index = 3)]
  183. pub type_option_data: Vec<u8>,
  184. }
  185. #[derive(Debug, Default, ProtoBuf)]
  186. pub struct FieldTypeOptionData {
  187. #[pb(index = 1)]
  188. pub grid_id: String,
  189. #[pb(index = 2)]
  190. pub field: Field,
  191. #[pb(index = 3)]
  192. pub type_option_data: Vec<u8>,
  193. }
  194. #[derive(Debug, Default, ProtoBuf)]
  195. pub struct RepeatedField {
  196. #[pb(index = 1)]
  197. pub items: Vec<Field>,
  198. }
  199. impl std::ops::Deref for RepeatedField {
  200. type Target = Vec<Field>;
  201. fn deref(&self) -> &Self::Target {
  202. &self.items
  203. }
  204. }
  205. impl std::ops::DerefMut for RepeatedField {
  206. fn deref_mut(&mut self) -> &mut Self::Target {
  207. &mut self.items
  208. }
  209. }
  210. impl std::convert::From<Vec<Field>> for RepeatedField {
  211. fn from(items: Vec<Field>) -> Self {
  212. Self { items }
  213. }
  214. }
  215. #[derive(Debug, Clone, Default, ProtoBuf)]
  216. pub struct RepeatedFieldOrder {
  217. #[pb(index = 1)]
  218. pub items: Vec<FieldOrder>,
  219. }
  220. impl std::ops::Deref for RepeatedFieldOrder {
  221. type Target = Vec<FieldOrder>;
  222. fn deref(&self) -> &Self::Target {
  223. &self.items
  224. }
  225. }
  226. impl std::convert::From<Vec<FieldOrder>> for RepeatedFieldOrder {
  227. fn from(field_orders: Vec<FieldOrder>) -> Self {
  228. RepeatedFieldOrder { items: field_orders }
  229. }
  230. }
  231. impl std::convert::From<String> for RepeatedFieldOrder {
  232. fn from(s: String) -> Self {
  233. RepeatedFieldOrder {
  234. items: vec![FieldOrder::from(s)],
  235. }
  236. }
  237. }
  238. #[derive(Debug, Default, Clone, ProtoBuf)]
  239. pub struct RowOrder {
  240. #[pb(index = 1)]
  241. pub row_id: String,
  242. #[pb(index = 2)]
  243. pub block_id: String,
  244. #[pb(index = 3)]
  245. pub height: i32,
  246. }
  247. impl std::convert::From<&RowMeta> for RowOrder {
  248. fn from(row: &RowMeta) -> Self {
  249. Self {
  250. row_id: row.id.clone(),
  251. block_id: row.block_id.clone(),
  252. height: row.height,
  253. }
  254. }
  255. }
  256. impl std::convert::From<&Arc<RowMeta>> for RowOrder {
  257. fn from(row: &Arc<RowMeta>) -> Self {
  258. Self {
  259. row_id: row.id.clone(),
  260. block_id: row.block_id.clone(),
  261. height: row.height,
  262. }
  263. }
  264. }
  265. #[derive(Debug, Default, ProtoBuf)]
  266. pub struct Row {
  267. #[pb(index = 1)]
  268. pub id: String,
  269. #[pb(index = 2)]
  270. pub cell_by_field_id: HashMap<String, Cell>,
  271. #[pb(index = 3)]
  272. pub height: i32,
  273. }
  274. #[derive(Debug, Default, ProtoBuf)]
  275. pub struct RepeatedRow {
  276. #[pb(index = 1)]
  277. pub items: Vec<Row>,
  278. }
  279. impl std::convert::From<Vec<Row>> for RepeatedRow {
  280. fn from(items: Vec<Row>) -> Self {
  281. Self { items }
  282. }
  283. }
  284. #[derive(Debug, Default, ProtoBuf)]
  285. pub struct RepeatedGridBlock {
  286. #[pb(index = 1)]
  287. pub items: Vec<GridBlock>,
  288. }
  289. impl std::convert::From<Vec<GridBlock>> for RepeatedGridBlock {
  290. fn from(items: Vec<GridBlock>) -> Self {
  291. Self { items }
  292. }
  293. }
  294. #[derive(Debug, Clone, Default, ProtoBuf)]
  295. pub struct GridBlockOrder {
  296. #[pb(index = 1)]
  297. pub block_id: String,
  298. #[pb(index = 2)]
  299. pub row_orders: Vec<RowOrder>,
  300. }
  301. impl GridBlockOrder {
  302. pub fn new(block_id: &str) -> Self {
  303. GridBlockOrder {
  304. block_id: block_id.to_owned(),
  305. row_orders: vec![],
  306. }
  307. }
  308. }
  309. #[derive(Debug, Clone, Default, ProtoBuf)]
  310. pub struct IndexRowOrder {
  311. #[pb(index = 1)]
  312. pub row_order: RowOrder,
  313. #[pb(index = 2, one_of)]
  314. pub index: Option<i32>,
  315. }
  316. #[derive(Debug, Default, ProtoBuf)]
  317. pub struct UpdatedRowOrder {
  318. #[pb(index = 1)]
  319. pub row_order: RowOrder,
  320. #[pb(index = 2)]
  321. pub row: Row,
  322. }
  323. impl UpdatedRowOrder {
  324. pub fn new(row_meta: &RowMeta, row: Row) -> Self {
  325. Self {
  326. row_order: RowOrder::from(row_meta),
  327. row,
  328. }
  329. }
  330. }
  331. #[derive(Debug, Default, ProtoBuf)]
  332. pub struct GridRowsChangeset {
  333. #[pb(index = 1)]
  334. pub block_id: String,
  335. #[pb(index = 2)]
  336. pub inserted_rows: Vec<IndexRowOrder>,
  337. #[pb(index = 3)]
  338. pub deleted_rows: Vec<RowOrder>,
  339. #[pb(index = 4)]
  340. pub updated_rows: Vec<UpdatedRowOrder>,
  341. }
  342. impl std::convert::From<RowOrder> for IndexRowOrder {
  343. fn from(row_order: RowOrder) -> Self {
  344. Self { row_order, index: None }
  345. }
  346. }
  347. impl std::convert::From<&RowMeta> for IndexRowOrder {
  348. fn from(row: &RowMeta) -> Self {
  349. let row_order = RowOrder::from(row);
  350. Self::from(row_order)
  351. }
  352. }
  353. impl GridRowsChangeset {
  354. pub fn insert(block_id: &str, inserted_rows: Vec<IndexRowOrder>) -> Self {
  355. Self {
  356. block_id: block_id.to_owned(),
  357. inserted_rows,
  358. deleted_rows: vec![],
  359. updated_rows: vec![],
  360. }
  361. }
  362. pub fn delete(block_id: &str, deleted_rows: Vec<RowOrder>) -> Self {
  363. Self {
  364. block_id: block_id.to_owned(),
  365. inserted_rows: vec![],
  366. deleted_rows,
  367. updated_rows: vec![],
  368. }
  369. }
  370. pub fn update(block_id: &str, updated_rows: Vec<UpdatedRowOrder>) -> Self {
  371. Self {
  372. block_id: block_id.to_owned(),
  373. inserted_rows: vec![],
  374. deleted_rows: vec![],
  375. updated_rows,
  376. }
  377. }
  378. }
  379. #[derive(Debug, Default, ProtoBuf)]
  380. pub struct GridBlock {
  381. #[pb(index = 1)]
  382. pub id: String,
  383. #[pb(index = 2)]
  384. pub row_orders: Vec<RowOrder>,
  385. }
  386. impl GridBlock {
  387. pub fn new(block_id: &str, row_orders: Vec<RowOrder>) -> Self {
  388. Self {
  389. id: block_id.to_owned(),
  390. row_orders,
  391. }
  392. }
  393. }
  394. #[derive(Debug, Default, ProtoBuf)]
  395. pub struct Cell {
  396. #[pb(index = 1)]
  397. pub field_id: String,
  398. #[pb(index = 2)]
  399. pub data: Vec<u8>,
  400. }
  401. impl Cell {
  402. pub fn new(field_id: &str, data: Vec<u8>) -> Self {
  403. Self {
  404. field_id: field_id.to_owned(),
  405. data,
  406. }
  407. }
  408. pub fn empty(field_id: &str) -> Self {
  409. Self {
  410. field_id: field_id.to_owned(),
  411. data: vec![],
  412. }
  413. }
  414. }
  415. #[derive(Debug, Default, ProtoBuf)]
  416. pub struct RepeatedCell {
  417. #[pb(index = 1)]
  418. pub items: Vec<Cell>,
  419. }
  420. impl std::ops::Deref for RepeatedCell {
  421. type Target = Vec<Cell>;
  422. fn deref(&self) -> &Self::Target {
  423. &self.items
  424. }
  425. }
  426. impl std::ops::DerefMut for RepeatedCell {
  427. fn deref_mut(&mut self) -> &mut Self::Target {
  428. &mut self.items
  429. }
  430. }
  431. impl std::convert::From<Vec<Cell>> for RepeatedCell {
  432. fn from(items: Vec<Cell>) -> Self {
  433. Self { items }
  434. }
  435. }
  436. #[derive(ProtoBuf, Default)]
  437. pub struct CreateGridPayload {
  438. #[pb(index = 1)]
  439. pub name: String,
  440. }
  441. #[derive(Clone, ProtoBuf, Default, Debug)]
  442. pub struct GridId {
  443. #[pb(index = 1)]
  444. pub value: String,
  445. }
  446. impl AsRef<str> for GridId {
  447. fn as_ref(&self) -> &str {
  448. &self.value
  449. }
  450. }
  451. #[derive(Clone, ProtoBuf, Default, Debug)]
  452. pub struct GridBlockId {
  453. #[pb(index = 1)]
  454. pub value: String,
  455. }
  456. impl AsRef<str> for GridBlockId {
  457. fn as_ref(&self) -> &str {
  458. &self.value
  459. }
  460. }
  461. impl std::convert::From<&str> for GridBlockId {
  462. fn from(s: &str) -> Self {
  463. GridBlockId { value: s.to_owned() }
  464. }
  465. }
  466. #[derive(ProtoBuf, Default)]
  467. pub struct CreateRowPayload {
  468. #[pb(index = 1)]
  469. pub grid_id: String,
  470. #[pb(index = 2, one_of)]
  471. pub start_row_id: Option<String>,
  472. }
  473. #[derive(Default)]
  474. pub struct CreateRowParams {
  475. pub grid_id: String,
  476. pub start_row_id: Option<String>,
  477. }
  478. impl TryInto<CreateRowParams> for CreateRowPayload {
  479. type Error = ErrorCode;
  480. fn try_into(self) -> Result<CreateRowParams, Self::Error> {
  481. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  482. Ok(CreateRowParams {
  483. grid_id: grid_id.0,
  484. start_row_id: self.start_row_id,
  485. })
  486. }
  487. }
  488. #[derive(ProtoBuf, Default)]
  489. pub struct InsertFieldPayload {
  490. #[pb(index = 1)]
  491. pub grid_id: String,
  492. #[pb(index = 2)]
  493. pub field: Field,
  494. #[pb(index = 3)]
  495. pub type_option_data: Vec<u8>,
  496. #[pb(index = 4, one_of)]
  497. pub start_field_id: Option<String>,
  498. }
  499. #[derive(Clone)]
  500. pub struct InsertFieldParams {
  501. pub grid_id: String,
  502. pub field: Field,
  503. pub type_option_data: Vec<u8>,
  504. pub start_field_id: Option<String>,
  505. }
  506. impl TryInto<InsertFieldParams> for InsertFieldPayload {
  507. type Error = ErrorCode;
  508. fn try_into(self) -> Result<InsertFieldParams, Self::Error> {
  509. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  510. let _ = NotEmptyStr::parse(self.field.id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  511. let start_field_id = match self.start_field_id {
  512. None => None,
  513. Some(id) => Some(NotEmptyStr::parse(id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
  514. };
  515. Ok(InsertFieldParams {
  516. grid_id: grid_id.0,
  517. field: self.field,
  518. type_option_data: self.type_option_data,
  519. start_field_id,
  520. })
  521. }
  522. }
  523. #[derive(ProtoBuf, Default)]
  524. pub struct UpdateFieldTypeOptionPayload {
  525. #[pb(index = 1)]
  526. pub grid_id: String,
  527. #[pb(index = 2)]
  528. pub field_id: String,
  529. #[pb(index = 3)]
  530. pub type_option_data: Vec<u8>,
  531. }
  532. #[derive(Clone)]
  533. pub struct UpdateFieldTypeOptionParams {
  534. pub grid_id: String,
  535. pub field_id: String,
  536. pub type_option_data: Vec<u8>,
  537. }
  538. impl TryInto<UpdateFieldTypeOptionParams> for UpdateFieldTypeOptionPayload {
  539. type Error = ErrorCode;
  540. fn try_into(self) -> Result<UpdateFieldTypeOptionParams, Self::Error> {
  541. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  542. let _ = NotEmptyStr::parse(self.field_id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  543. Ok(UpdateFieldTypeOptionParams {
  544. grid_id: grid_id.0,
  545. field_id: self.field_id,
  546. type_option_data: self.type_option_data,
  547. })
  548. }
  549. }
  550. #[derive(ProtoBuf, Default)]
  551. pub struct QueryFieldPayload {
  552. #[pb(index = 1)]
  553. pub grid_id: String,
  554. #[pb(index = 2)]
  555. pub field_orders: RepeatedFieldOrder,
  556. }
  557. pub struct QueryFieldParams {
  558. pub grid_id: String,
  559. pub field_orders: RepeatedFieldOrder,
  560. }
  561. impl TryInto<QueryFieldParams> for QueryFieldPayload {
  562. type Error = ErrorCode;
  563. fn try_into(self) -> Result<QueryFieldParams, Self::Error> {
  564. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  565. Ok(QueryFieldParams {
  566. grid_id: grid_id.0,
  567. field_orders: self.field_orders,
  568. })
  569. }
  570. }
  571. #[derive(ProtoBuf, Default)]
  572. pub struct QueryGridBlocksPayload {
  573. #[pb(index = 1)]
  574. pub grid_id: String,
  575. #[pb(index = 2)]
  576. pub block_orders: Vec<GridBlockOrder>,
  577. }
  578. pub struct QueryGridBlocksParams {
  579. pub grid_id: String,
  580. pub block_orders: Vec<GridBlockOrder>,
  581. }
  582. impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayload {
  583. type Error = ErrorCode;
  584. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  585. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  586. Ok(QueryGridBlocksParams {
  587. grid_id: grid_id.0,
  588. block_orders: self.block_orders,
  589. })
  590. }
  591. }
  592. #[derive(Debug, Clone, Default, ProtoBuf)]
  593. pub struct FieldChangesetPayload {
  594. #[pb(index = 1)]
  595. pub field_id: String,
  596. #[pb(index = 2)]
  597. pub grid_id: String,
  598. #[pb(index = 3, one_of)]
  599. pub name: Option<String>,
  600. #[pb(index = 4, one_of)]
  601. pub desc: Option<String>,
  602. #[pb(index = 5, one_of)]
  603. pub field_type: Option<FieldType>,
  604. #[pb(index = 6, one_of)]
  605. pub frozen: Option<bool>,
  606. #[pb(index = 7, one_of)]
  607. pub visibility: Option<bool>,
  608. #[pb(index = 8, one_of)]
  609. pub width: Option<i32>,
  610. #[pb(index = 9, one_of)]
  611. pub type_option_data: Option<Vec<u8>>,
  612. }
  613. #[derive(Debug, Clone, Default)]
  614. pub struct FieldChangesetParams {
  615. pub field_id: String,
  616. pub grid_id: String,
  617. pub name: Option<String>,
  618. pub desc: Option<String>,
  619. pub field_type: Option<FieldType>,
  620. pub frozen: Option<bool>,
  621. pub visibility: Option<bool>,
  622. pub width: Option<i32>,
  623. pub type_option_data: Option<Vec<u8>>,
  624. }
  625. impl TryInto<FieldChangesetParams> for FieldChangesetPayload {
  626. type Error = ErrorCode;
  627. fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
  628. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  629. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  630. if let Some(type_option_data) = self.type_option_data.as_ref() {
  631. if type_option_data.is_empty() {
  632. return Err(ErrorCode::TypeOptionDataIsEmpty);
  633. }
  634. }
  635. Ok(FieldChangesetParams {
  636. field_id: field_id.0,
  637. grid_id: grid_id.0,
  638. name: self.name,
  639. desc: self.desc,
  640. field_type: self.field_type,
  641. frozen: self.frozen,
  642. visibility: self.visibility,
  643. width: self.width,
  644. type_option_data: self.type_option_data,
  645. })
  646. }
  647. }
  648. #[derive(Debug, Clone, ProtoBuf_Enum)]
  649. pub enum MoveItemType {
  650. MoveField = 0,
  651. MoveRow = 1,
  652. }
  653. impl std::default::Default for MoveItemType {
  654. fn default() -> Self {
  655. MoveItemType::MoveField
  656. }
  657. }
  658. #[derive(Debug, Clone, Default, ProtoBuf)]
  659. pub struct MoveItemPayload {
  660. #[pb(index = 1)]
  661. pub grid_id: String,
  662. #[pb(index = 2)]
  663. pub item_id: String,
  664. #[pb(index = 3)]
  665. pub from_index: i32,
  666. #[pb(index = 4)]
  667. pub to_index: i32,
  668. #[pb(index = 5)]
  669. pub ty: MoveItemType,
  670. }
  671. #[derive(Clone)]
  672. pub struct MoveItemParams {
  673. pub grid_id: String,
  674. pub item_id: String,
  675. pub from_index: i32,
  676. pub to_index: i32,
  677. pub ty: MoveItemType,
  678. }
  679. impl TryInto<MoveItemParams> for MoveItemPayload {
  680. type Error = ErrorCode;
  681. fn try_into(self) -> Result<MoveItemParams, Self::Error> {
  682. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  683. let item_id = NotEmptyStr::parse(self.item_id).map_err(|_| ErrorCode::InvalidData)?;
  684. Ok(MoveItemParams {
  685. grid_id: grid_id.0,
  686. item_id: item_id.0,
  687. from_index: self.from_index,
  688. to_index: self.to_index,
  689. ty: self.ty,
  690. })
  691. }
  692. }
  693. #[derive(
  694. Debug,
  695. Clone,
  696. PartialEq,
  697. Eq,
  698. ProtoBuf_Enum,
  699. EnumCountMacro,
  700. EnumString,
  701. EnumIter,
  702. Display,
  703. Serialize_repr,
  704. Deserialize_repr,
  705. )]
  706. #[repr(u8)]
  707. pub enum FieldType {
  708. RichText = 0,
  709. Number = 1,
  710. DateTime = 2,
  711. SingleSelect = 3,
  712. MultiSelect = 4,
  713. Checkbox = 5,
  714. }
  715. impl std::default::Default for FieldType {
  716. fn default() -> Self {
  717. FieldType::RichText
  718. }
  719. }
  720. impl AsRef<FieldType> for FieldType {
  721. fn as_ref(&self) -> &FieldType {
  722. self
  723. }
  724. }
  725. impl From<&FieldType> for FieldType {
  726. fn from(field_type: &FieldType) -> Self {
  727. field_type.clone()
  728. }
  729. }
  730. impl FieldType {
  731. pub fn type_id(&self) -> String {
  732. let ty = self.clone();
  733. format!("{}", ty as u8)
  734. }
  735. pub fn default_cell_width(&self) -> i32 {
  736. match self {
  737. FieldType::DateTime => 180,
  738. _ => 150,
  739. }
  740. }
  741. pub fn is_number(&self) -> bool {
  742. self == &FieldType::Number
  743. }
  744. pub fn is_text(&self) -> bool {
  745. self == &FieldType::RichText
  746. }
  747. pub fn is_checkbox(&self) -> bool {
  748. self == &FieldType::Checkbox
  749. }
  750. pub fn is_date(&self) -> bool {
  751. self == &FieldType::DateTime
  752. }
  753. pub fn is_single_select(&self) -> bool {
  754. self == &FieldType::SingleSelect
  755. }
  756. pub fn is_multi_select(&self) -> bool {
  757. self == &FieldType::MultiSelect
  758. }
  759. pub fn is_select_option(&self) -> bool {
  760. self == &FieldType::MultiSelect || self == &FieldType::SingleSelect
  761. }
  762. }
  763. #[derive(Debug, Clone, Default, ProtoBuf)]
  764. pub struct CellChangeset {
  765. #[pb(index = 1)]
  766. pub grid_id: String,
  767. #[pb(index = 2)]
  768. pub row_id: String,
  769. #[pb(index = 3)]
  770. pub field_id: String,
  771. #[pb(index = 4, one_of)]
  772. pub cell_content_changeset: Option<String>,
  773. }
  774. impl std::convert::From<CellChangeset> for RowMetaChangeset {
  775. fn from(changeset: CellChangeset) -> Self {
  776. let mut cell_by_field_id = HashMap::with_capacity(1);
  777. let field_id = changeset.field_id;
  778. let cell_meta = CellMeta {
  779. data: changeset.cell_content_changeset.unwrap_or_else(|| "".to_owned()),
  780. };
  781. cell_by_field_id.insert(field_id, cell_meta);
  782. RowMetaChangeset {
  783. row_id: changeset.row_id,
  784. height: None,
  785. visibility: None,
  786. cell_by_field_id,
  787. }
  788. }
  789. }