grid.rs 19 KB

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