grid.rs 19 KB

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