grid.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 content: String,
  400. #[pb(index = 3)]
  401. pub data: String,
  402. }
  403. impl Cell {
  404. pub fn new(field_id: &str, content: String, data: String) -> Self {
  405. Self {
  406. field_id: field_id.to_owned(),
  407. content,
  408. data,
  409. }
  410. }
  411. pub fn empty(field_id: &str) -> Self {
  412. Self {
  413. field_id: field_id.to_owned(),
  414. content: "".to_string(),
  415. data: "".to_string(),
  416. }
  417. }
  418. }
  419. #[derive(Debug, Default, ProtoBuf)]
  420. pub struct RepeatedCell {
  421. #[pb(index = 1)]
  422. pub items: Vec<Cell>,
  423. }
  424. impl std::ops::Deref for RepeatedCell {
  425. type Target = Vec<Cell>;
  426. fn deref(&self) -> &Self::Target {
  427. &self.items
  428. }
  429. }
  430. impl std::ops::DerefMut for RepeatedCell {
  431. fn deref_mut(&mut self) -> &mut Self::Target {
  432. &mut self.items
  433. }
  434. }
  435. impl std::convert::From<Vec<Cell>> for RepeatedCell {
  436. fn from(items: Vec<Cell>) -> Self {
  437. Self { items }
  438. }
  439. }
  440. #[derive(ProtoBuf, Default)]
  441. pub struct CreateGridPayload {
  442. #[pb(index = 1)]
  443. pub name: String,
  444. }
  445. #[derive(Clone, ProtoBuf, Default, Debug)]
  446. pub struct GridId {
  447. #[pb(index = 1)]
  448. pub value: String,
  449. }
  450. impl AsRef<str> for GridId {
  451. fn as_ref(&self) -> &str {
  452. &self.value
  453. }
  454. }
  455. #[derive(Clone, ProtoBuf, Default, Debug)]
  456. pub struct GridBlockId {
  457. #[pb(index = 1)]
  458. pub value: String,
  459. }
  460. impl AsRef<str> for GridBlockId {
  461. fn as_ref(&self) -> &str {
  462. &self.value
  463. }
  464. }
  465. impl std::convert::From<&str> for GridBlockId {
  466. fn from(s: &str) -> Self {
  467. GridBlockId { value: s.to_owned() }
  468. }
  469. }
  470. #[derive(ProtoBuf, Default)]
  471. pub struct CreateRowPayload {
  472. #[pb(index = 1)]
  473. pub grid_id: String,
  474. #[pb(index = 2, one_of)]
  475. pub start_row_id: Option<String>,
  476. }
  477. #[derive(Default)]
  478. pub struct CreateRowParams {
  479. pub grid_id: String,
  480. pub start_row_id: Option<String>,
  481. }
  482. impl TryInto<CreateRowParams> for CreateRowPayload {
  483. type Error = ErrorCode;
  484. fn try_into(self) -> Result<CreateRowParams, Self::Error> {
  485. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  486. Ok(CreateRowParams {
  487. grid_id: grid_id.0,
  488. start_row_id: self.start_row_id,
  489. })
  490. }
  491. }
  492. #[derive(ProtoBuf, Default)]
  493. pub struct InsertFieldPayload {
  494. #[pb(index = 1)]
  495. pub grid_id: String,
  496. #[pb(index = 2)]
  497. pub field: Field,
  498. #[pb(index = 3)]
  499. pub type_option_data: Vec<u8>,
  500. #[pb(index = 4, one_of)]
  501. pub start_field_id: Option<String>,
  502. }
  503. #[derive(Clone)]
  504. pub struct InsertFieldParams {
  505. pub grid_id: String,
  506. pub field: Field,
  507. pub type_option_data: Vec<u8>,
  508. pub start_field_id: Option<String>,
  509. }
  510. impl TryInto<InsertFieldParams> for InsertFieldPayload {
  511. type Error = ErrorCode;
  512. fn try_into(self) -> Result<InsertFieldParams, Self::Error> {
  513. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  514. let _ = NotEmptyStr::parse(self.field.id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  515. let start_field_id = match self.start_field_id {
  516. None => None,
  517. Some(id) => Some(NotEmptyStr::parse(id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
  518. };
  519. Ok(InsertFieldParams {
  520. grid_id: grid_id.0,
  521. field: self.field,
  522. type_option_data: self.type_option_data,
  523. start_field_id,
  524. })
  525. }
  526. }
  527. #[derive(ProtoBuf, Default)]
  528. pub struct UpdateFieldTypeOptionPayload {
  529. #[pb(index = 1)]
  530. pub grid_id: String,
  531. #[pb(index = 2)]
  532. pub field_id: String,
  533. #[pb(index = 3)]
  534. pub type_option_data: Vec<u8>,
  535. }
  536. #[derive(Clone)]
  537. pub struct UpdateFieldTypeOptionParams {
  538. pub grid_id: String,
  539. pub field_id: String,
  540. pub type_option_data: Vec<u8>,
  541. }
  542. impl TryInto<UpdateFieldTypeOptionParams> for UpdateFieldTypeOptionPayload {
  543. type Error = ErrorCode;
  544. fn try_into(self) -> Result<UpdateFieldTypeOptionParams, Self::Error> {
  545. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  546. let _ = NotEmptyStr::parse(self.field_id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  547. Ok(UpdateFieldTypeOptionParams {
  548. grid_id: grid_id.0,
  549. field_id: self.field_id,
  550. type_option_data: self.type_option_data,
  551. })
  552. }
  553. }
  554. #[derive(ProtoBuf, Default)]
  555. pub struct QueryFieldPayload {
  556. #[pb(index = 1)]
  557. pub grid_id: String,
  558. #[pb(index = 2)]
  559. pub field_orders: RepeatedFieldOrder,
  560. }
  561. pub struct QueryFieldParams {
  562. pub grid_id: String,
  563. pub field_orders: RepeatedFieldOrder,
  564. }
  565. impl TryInto<QueryFieldParams> for QueryFieldPayload {
  566. type Error = ErrorCode;
  567. fn try_into(self) -> Result<QueryFieldParams, Self::Error> {
  568. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  569. Ok(QueryFieldParams {
  570. grid_id: grid_id.0,
  571. field_orders: self.field_orders,
  572. })
  573. }
  574. }
  575. #[derive(ProtoBuf, Default)]
  576. pub struct QueryGridBlocksPayload {
  577. #[pb(index = 1)]
  578. pub grid_id: String,
  579. #[pb(index = 2)]
  580. pub block_orders: Vec<GridBlockOrder>,
  581. }
  582. pub struct QueryGridBlocksParams {
  583. pub grid_id: String,
  584. pub block_orders: Vec<GridBlockOrder>,
  585. }
  586. impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayload {
  587. type Error = ErrorCode;
  588. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  589. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  590. Ok(QueryGridBlocksParams {
  591. grid_id: grid_id.0,
  592. block_orders: self.block_orders,
  593. })
  594. }
  595. }
  596. #[derive(Debug, Clone, Default, ProtoBuf)]
  597. pub struct FieldChangesetPayload {
  598. #[pb(index = 1)]
  599. pub field_id: String,
  600. #[pb(index = 2)]
  601. pub grid_id: String,
  602. #[pb(index = 3, one_of)]
  603. pub name: Option<String>,
  604. #[pb(index = 4, one_of)]
  605. pub desc: Option<String>,
  606. #[pb(index = 5, one_of)]
  607. pub field_type: Option<FieldType>,
  608. #[pb(index = 6, one_of)]
  609. pub frozen: Option<bool>,
  610. #[pb(index = 7, one_of)]
  611. pub visibility: Option<bool>,
  612. #[pb(index = 8, one_of)]
  613. pub width: Option<i32>,
  614. #[pb(index = 9, one_of)]
  615. pub type_option_data: Option<Vec<u8>>,
  616. }
  617. #[derive(Debug, Clone, Default)]
  618. pub struct FieldChangesetParams {
  619. pub field_id: String,
  620. pub grid_id: String,
  621. pub name: Option<String>,
  622. pub desc: Option<String>,
  623. pub field_type: Option<FieldType>,
  624. pub frozen: Option<bool>,
  625. pub visibility: Option<bool>,
  626. pub width: Option<i32>,
  627. pub type_option_data: Option<Vec<u8>>,
  628. }
  629. impl TryInto<FieldChangesetParams> for FieldChangesetPayload {
  630. type Error = ErrorCode;
  631. fn try_into(self) -> Result<FieldChangesetParams, Self::Error> {
  632. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  633. let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  634. if let Some(type_option_data) = self.type_option_data.as_ref() {
  635. if type_option_data.is_empty() {
  636. return Err(ErrorCode::TypeOptionDataIsEmpty);
  637. }
  638. }
  639. Ok(FieldChangesetParams {
  640. field_id: field_id.0,
  641. grid_id: grid_id.0,
  642. name: self.name,
  643. desc: self.desc,
  644. field_type: self.field_type,
  645. frozen: self.frozen,
  646. visibility: self.visibility,
  647. width: self.width,
  648. type_option_data: self.type_option_data,
  649. })
  650. }
  651. }
  652. #[derive(Debug, Clone, ProtoBuf_Enum)]
  653. pub enum MoveItemType {
  654. MoveField = 0,
  655. MoveRow = 1,
  656. }
  657. impl std::default::Default for MoveItemType {
  658. fn default() -> Self {
  659. MoveItemType::MoveField
  660. }
  661. }
  662. #[derive(Debug, Clone, Default, ProtoBuf)]
  663. pub struct MoveItemPayload {
  664. #[pb(index = 1)]
  665. pub grid_id: String,
  666. #[pb(index = 2)]
  667. pub item_id: String,
  668. #[pb(index = 3)]
  669. pub from_index: i32,
  670. #[pb(index = 4)]
  671. pub to_index: i32,
  672. #[pb(index = 5)]
  673. pub ty: MoveItemType,
  674. }
  675. #[derive(Clone)]
  676. pub struct MoveItemParams {
  677. pub grid_id: String,
  678. pub item_id: String,
  679. pub from_index: i32,
  680. pub to_index: i32,
  681. pub ty: MoveItemType,
  682. }
  683. impl TryInto<MoveItemParams> for MoveItemPayload {
  684. type Error = ErrorCode;
  685. fn try_into(self) -> Result<MoveItemParams, Self::Error> {
  686. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  687. let item_id = NotEmptyStr::parse(self.item_id).map_err(|_| ErrorCode::InvalidData)?;
  688. Ok(MoveItemParams {
  689. grid_id: grid_id.0,
  690. item_id: item_id.0,
  691. from_index: self.from_index,
  692. to_index: self.to_index,
  693. ty: self.ty,
  694. })
  695. }
  696. }
  697. #[derive(
  698. Debug,
  699. Clone,
  700. PartialEq,
  701. Eq,
  702. ProtoBuf_Enum,
  703. EnumCountMacro,
  704. EnumString,
  705. EnumIter,
  706. Display,
  707. Serialize_repr,
  708. Deserialize_repr,
  709. )]
  710. #[repr(u8)]
  711. pub enum FieldType {
  712. RichText = 0,
  713. Number = 1,
  714. DateTime = 2,
  715. SingleSelect = 3,
  716. MultiSelect = 4,
  717. Checkbox = 5,
  718. }
  719. impl std::default::Default for FieldType {
  720. fn default() -> Self {
  721. FieldType::RichText
  722. }
  723. }
  724. impl AsRef<FieldType> for FieldType {
  725. fn as_ref(&self) -> &FieldType {
  726. self
  727. }
  728. }
  729. impl From<&FieldType> for FieldType {
  730. fn from(field_type: &FieldType) -> Self {
  731. field_type.clone()
  732. }
  733. }
  734. impl FieldType {
  735. pub fn type_id(&self) -> String {
  736. let ty = self.clone();
  737. format!("{}", ty as u8)
  738. }
  739. pub fn default_cell_width(&self) -> i32 {
  740. match self {
  741. FieldType::DateTime => 180,
  742. _ => 150,
  743. }
  744. }
  745. }
  746. #[derive(Debug, Clone, Default, ProtoBuf)]
  747. pub struct CellChangeset {
  748. #[pb(index = 1)]
  749. pub grid_id: String,
  750. #[pb(index = 2)]
  751. pub row_id: String,
  752. #[pb(index = 3)]
  753. pub field_id: String,
  754. #[pb(index = 4, one_of)]
  755. pub cell_content_changeset: Option<String>,
  756. }
  757. impl std::convert::From<CellChangeset> for RowMetaChangeset {
  758. fn from(changeset: CellChangeset) -> Self {
  759. let mut cell_by_field_id = HashMap::with_capacity(1);
  760. let field_id = changeset.field_id;
  761. let cell_meta = CellMeta {
  762. data: changeset.cell_content_changeset.unwrap_or_else(|| "".to_owned()),
  763. };
  764. cell_by_field_id.insert(field_id, cell_meta);
  765. RowMetaChangeset {
  766. row_id: changeset.row_id,
  767. height: None,
  768. visibility: None,
  769. cell_by_field_id,
  770. }
  771. }
  772. }