grid.rs 20 KB

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