grid.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. use crate::entities::{FieldMeta, FieldType, RowMeta};
  2. use crate::parser::{NotEmptyStr, NotEmptyUuid};
  3. use flowy_derive::ProtoBuf;
  4. use flowy_error_code::ErrorCode;
  5. use std::collections::HashMap;
  6. use std::sync::Arc;
  7. #[derive(Debug, Clone, Default, ProtoBuf)]
  8. pub struct Grid {
  9. #[pb(index = 1)]
  10. pub id: String,
  11. #[pb(index = 2)]
  12. pub field_orders: Vec<FieldOrder>,
  13. #[pb(index = 3)]
  14. pub block_orders: Vec<GridBlockOrder>,
  15. }
  16. #[derive(Debug, Clone, Default, ProtoBuf)]
  17. pub struct Field {
  18. #[pb(index = 1)]
  19. pub id: String,
  20. #[pb(index = 2)]
  21. pub name: String,
  22. #[pb(index = 3)]
  23. pub desc: String,
  24. #[pb(index = 4)]
  25. pub field_type: FieldType,
  26. #[pb(index = 5)]
  27. pub frozen: bool,
  28. #[pb(index = 6)]
  29. pub visibility: bool,
  30. #[pb(index = 7)]
  31. pub width: i32,
  32. }
  33. impl std::convert::From<FieldMeta> for Field {
  34. fn from(field_meta: FieldMeta) -> Self {
  35. Self {
  36. id: field_meta.id,
  37. name: field_meta.name,
  38. desc: field_meta.desc,
  39. field_type: field_meta.field_type,
  40. frozen: field_meta.frozen,
  41. visibility: field_meta.visibility,
  42. width: field_meta.width,
  43. }
  44. }
  45. }
  46. #[derive(Debug, Clone, Default, ProtoBuf)]
  47. pub struct FieldIdentifierPayload {
  48. #[pb(index = 1)]
  49. pub field_id: String,
  50. #[pb(index = 2)]
  51. pub grid_id: String,
  52. }
  53. pub struct FieldIdentifierParams {
  54. pub field_id: String,
  55. pub grid_id: String,
  56. }
  57. impl TryInto<FieldIdentifierParams> for FieldIdentifierPayload {
  58. type Error = ErrorCode;
  59. fn try_into(self) -> Result<FieldIdentifierParams, Self::Error> {
  60. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  61. let field_id = NotEmptyUuid::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  62. Ok(FieldIdentifierParams {
  63. grid_id: grid_id.0,
  64. field_id: field_id.0,
  65. })
  66. }
  67. }
  68. #[derive(Debug, Clone, Default, ProtoBuf)]
  69. pub struct FieldOrder {
  70. #[pb(index = 1)]
  71. pub field_id: String,
  72. }
  73. impl std::convert::From<&FieldMeta> for FieldOrder {
  74. fn from(field_meta: &FieldMeta) -> Self {
  75. Self {
  76. field_id: field_meta.id.clone(),
  77. }
  78. }
  79. }
  80. impl std::convert::From<&str> for FieldOrder {
  81. fn from(s: &str) -> Self {
  82. FieldOrder { field_id: s.to_owned() }
  83. }
  84. }
  85. impl std::convert::From<String> for FieldOrder {
  86. fn from(s: String) -> Self {
  87. FieldOrder { field_id: s }
  88. }
  89. }
  90. #[derive(Debug, Default, ProtoBuf)]
  91. pub struct GetEditFieldContextPayload {
  92. #[pb(index = 1)]
  93. pub grid_id: String,
  94. #[pb(index = 2, one_of)]
  95. pub field_id: Option<String>,
  96. #[pb(index = 3)]
  97. pub field_type: FieldType,
  98. }
  99. #[derive(Debug, Default, ProtoBuf)]
  100. pub struct EditFieldPayload {
  101. #[pb(index = 1)]
  102. pub grid_id: String,
  103. #[pb(index = 2)]
  104. pub field_id: String,
  105. #[pb(index = 3)]
  106. pub field_type: FieldType,
  107. }
  108. pub struct EditFieldParams {
  109. pub grid_id: String,
  110. pub field_id: String,
  111. pub field_type: FieldType,
  112. }
  113. impl TryInto<EditFieldParams> for EditFieldPayload {
  114. type Error = ErrorCode;
  115. fn try_into(self) -> Result<EditFieldParams, Self::Error> {
  116. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  117. let field_id = NotEmptyUuid::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  118. Ok(EditFieldParams {
  119. grid_id: grid_id.0,
  120. field_id: field_id.0,
  121. field_type: self.field_type,
  122. })
  123. }
  124. }
  125. #[derive(Debug, Default, ProtoBuf)]
  126. pub struct EditFieldContext {
  127. #[pb(index = 1)]
  128. pub grid_id: String,
  129. #[pb(index = 2)]
  130. pub grid_field: Field,
  131. #[pb(index = 3)]
  132. pub type_option_data: Vec<u8>,
  133. }
  134. #[derive(Debug, Default, ProtoBuf)]
  135. pub struct RepeatedField {
  136. #[pb(index = 1)]
  137. pub items: Vec<Field>,
  138. }
  139. impl std::ops::Deref for RepeatedField {
  140. type Target = Vec<Field>;
  141. fn deref(&self) -> &Self::Target {
  142. &self.items
  143. }
  144. }
  145. impl std::ops::DerefMut for RepeatedField {
  146. fn deref_mut(&mut self) -> &mut Self::Target {
  147. &mut self.items
  148. }
  149. }
  150. impl std::convert::From<Vec<Field>> for RepeatedField {
  151. fn from(items: Vec<Field>) -> Self {
  152. Self { items }
  153. }
  154. }
  155. #[derive(Debug, Clone, Default, ProtoBuf)]
  156. pub struct RepeatedFieldOrder {
  157. #[pb(index = 1)]
  158. pub items: Vec<FieldOrder>,
  159. }
  160. impl std::ops::Deref for RepeatedFieldOrder {
  161. type Target = Vec<FieldOrder>;
  162. fn deref(&self) -> &Self::Target {
  163. &self.items
  164. }
  165. }
  166. impl std::convert::From<Vec<FieldOrder>> for RepeatedFieldOrder {
  167. fn from(field_orders: Vec<FieldOrder>) -> Self {
  168. RepeatedFieldOrder { items: field_orders }
  169. }
  170. }
  171. impl std::convert::From<String> for RepeatedFieldOrder {
  172. fn from(s: String) -> Self {
  173. RepeatedFieldOrder {
  174. items: vec![FieldOrder::from(s)],
  175. }
  176. }
  177. }
  178. #[derive(Debug, Default, Clone, ProtoBuf)]
  179. pub struct RowOrder {
  180. #[pb(index = 1)]
  181. pub row_id: String,
  182. #[pb(index = 2)]
  183. pub block_id: String,
  184. #[pb(index = 3)]
  185. pub height: i32,
  186. }
  187. impl std::convert::From<&RowMeta> for RowOrder {
  188. fn from(row: &RowMeta) -> Self {
  189. Self {
  190. row_id: row.id.clone(),
  191. block_id: row.block_id.clone(),
  192. height: row.height,
  193. }
  194. }
  195. }
  196. impl std::convert::From<&Arc<RowMeta>> for RowOrder {
  197. fn from(row: &Arc<RowMeta>) -> Self {
  198. Self {
  199. row_id: row.id.clone(),
  200. block_id: row.block_id.clone(),
  201. height: row.height,
  202. }
  203. }
  204. }
  205. #[derive(Debug, Default, ProtoBuf)]
  206. pub struct Row {
  207. #[pb(index = 1)]
  208. pub id: String,
  209. #[pb(index = 2)]
  210. pub cell_by_field_id: HashMap<String, Cell>,
  211. #[pb(index = 3)]
  212. pub height: i32,
  213. }
  214. #[derive(Debug, Default, ProtoBuf)]
  215. pub struct RepeatedRow {
  216. #[pb(index = 1)]
  217. pub items: Vec<Row>,
  218. }
  219. impl std::convert::From<Vec<Row>> for RepeatedRow {
  220. fn from(items: Vec<Row>) -> Self {
  221. Self { items }
  222. }
  223. }
  224. #[derive(Debug, Default, ProtoBuf)]
  225. pub struct RepeatedGridBlock {
  226. #[pb(index = 1)]
  227. pub items: Vec<GridBlock>,
  228. }
  229. impl std::convert::From<Vec<GridBlock>> for RepeatedGridBlock {
  230. fn from(items: Vec<GridBlock>) -> Self {
  231. Self { items }
  232. }
  233. }
  234. #[derive(Debug, Clone, Default, ProtoBuf)]
  235. pub struct GridBlockOrder {
  236. #[pb(index = 1)]
  237. pub block_id: String,
  238. }
  239. impl std::convert::From<&str> for GridBlockOrder {
  240. fn from(s: &str) -> Self {
  241. GridBlockOrder { block_id: s.to_owned() }
  242. }
  243. }
  244. #[derive(Debug, Default, ProtoBuf)]
  245. pub struct GridBlock {
  246. #[pb(index = 1)]
  247. pub id: String,
  248. #[pb(index = 2)]
  249. pub row_orders: Vec<RowOrder>,
  250. }
  251. impl GridBlock {
  252. pub fn new(block_id: &str, row_orders: Vec<RowOrder>) -> Self {
  253. Self {
  254. id: block_id.to_owned(),
  255. row_orders,
  256. }
  257. }
  258. }
  259. #[derive(Debug, Default, ProtoBuf)]
  260. pub struct Cell {
  261. #[pb(index = 1)]
  262. pub field_id: String,
  263. #[pb(index = 2)]
  264. pub content: String,
  265. }
  266. impl Cell {
  267. pub fn new(field_id: &str, content: String) -> Self {
  268. Self {
  269. field_id: field_id.to_owned(),
  270. content,
  271. }
  272. }
  273. }
  274. #[derive(Debug, Clone, Default, ProtoBuf)]
  275. pub struct CellIdentifierPayload {
  276. #[pb(index = 1)]
  277. pub grid_id: String,
  278. #[pb(index = 2)]
  279. pub field_id: String,
  280. #[pb(index = 3)]
  281. pub row_id: String,
  282. }
  283. pub struct CellIdentifier {
  284. pub grid_id: String,
  285. pub field_id: String,
  286. pub row_id: String,
  287. }
  288. impl TryInto<CellIdentifier> for CellIdentifierPayload {
  289. type Error = ErrorCode;
  290. fn try_into(self) -> Result<CellIdentifier, Self::Error> {
  291. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  292. let field_id = NotEmptyUuid::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  293. let row_id = NotEmptyUuid::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  294. Ok(CellIdentifier {
  295. grid_id: grid_id.0,
  296. field_id: field_id.0,
  297. row_id: row_id.0,
  298. })
  299. }
  300. }
  301. #[derive(Debug, Clone, Default, ProtoBuf)]
  302. pub struct CellNotificationData {
  303. #[pb(index = 1)]
  304. pub grid_id: String,
  305. #[pb(index = 2)]
  306. pub field_id: String,
  307. #[pb(index = 3)]
  308. pub row_id: String,
  309. #[pb(index = 4, one_of)]
  310. pub content: Option<String>,
  311. }
  312. #[derive(Debug, Default, ProtoBuf)]
  313. pub struct RepeatedCell {
  314. #[pb(index = 1)]
  315. pub items: Vec<Cell>,
  316. }
  317. impl std::ops::Deref for RepeatedCell {
  318. type Target = Vec<Cell>;
  319. fn deref(&self) -> &Self::Target {
  320. &self.items
  321. }
  322. }
  323. impl std::ops::DerefMut for RepeatedCell {
  324. fn deref_mut(&mut self) -> &mut Self::Target {
  325. &mut self.items
  326. }
  327. }
  328. impl std::convert::From<Vec<Cell>> for RepeatedCell {
  329. fn from(items: Vec<Cell>) -> Self {
  330. Self { items }
  331. }
  332. }
  333. #[derive(ProtoBuf, Default)]
  334. pub struct CreateGridPayload {
  335. #[pb(index = 1)]
  336. pub name: String,
  337. }
  338. #[derive(Clone, ProtoBuf, Default, Debug)]
  339. pub struct GridId {
  340. #[pb(index = 1)]
  341. pub value: String,
  342. }
  343. impl AsRef<str> for GridId {
  344. fn as_ref(&self) -> &str {
  345. &self.value
  346. }
  347. }
  348. #[derive(Clone, ProtoBuf, Default, Debug)]
  349. pub struct GridBlockId {
  350. #[pb(index = 1)]
  351. pub value: String,
  352. }
  353. impl AsRef<str> for GridBlockId {
  354. fn as_ref(&self) -> &str {
  355. &self.value
  356. }
  357. }
  358. impl std::convert::From<&str> for GridBlockId {
  359. fn from(s: &str) -> Self {
  360. GridBlockId { value: s.to_owned() }
  361. }
  362. }
  363. #[derive(ProtoBuf, Default)]
  364. pub struct CreateRowPayload {
  365. #[pb(index = 1)]
  366. pub grid_id: String,
  367. #[pb(index = 2, one_of)]
  368. pub start_row_id: Option<String>,
  369. }
  370. #[derive(Default)]
  371. pub struct CreateRowParams {
  372. pub grid_id: String,
  373. pub start_row_id: Option<String>,
  374. }
  375. impl TryInto<CreateRowParams> for CreateRowPayload {
  376. type Error = ErrorCode;
  377. fn try_into(self) -> Result<CreateRowParams, Self::Error> {
  378. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  379. Ok(CreateRowParams {
  380. grid_id: grid_id.0,
  381. start_row_id: self.start_row_id,
  382. })
  383. }
  384. }
  385. #[derive(ProtoBuf, Default)]
  386. pub struct CreateFieldPayload {
  387. #[pb(index = 1)]
  388. pub grid_id: String,
  389. #[pb(index = 2)]
  390. pub field: Field,
  391. #[pb(index = 3)]
  392. pub type_option_data: Vec<u8>,
  393. #[pb(index = 4, one_of)]
  394. pub start_field_id: Option<String>,
  395. }
  396. pub struct CreateFieldParams {
  397. pub grid_id: String,
  398. pub field: Field,
  399. pub type_option_data: Vec<u8>,
  400. pub start_field_id: Option<String>,
  401. }
  402. impl TryInto<CreateFieldParams> for CreateFieldPayload {
  403. type Error = ErrorCode;
  404. fn try_into(self) -> Result<CreateFieldParams, Self::Error> {
  405. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  406. let _ = NotEmptyUuid::parse(self.field.id.clone()).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
  407. let start_field_id = match self.start_field_id {
  408. None => None,
  409. Some(id) => Some(NotEmptyUuid::parse(id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
  410. };
  411. Ok(CreateFieldParams {
  412. grid_id: grid_id.0,
  413. field: self.field,
  414. type_option_data: self.type_option_data,
  415. start_field_id,
  416. })
  417. }
  418. }
  419. #[derive(ProtoBuf, Default)]
  420. pub struct QueryFieldPayload {
  421. #[pb(index = 1)]
  422. pub grid_id: String,
  423. #[pb(index = 2)]
  424. pub field_orders: RepeatedFieldOrder,
  425. }
  426. pub struct QueryFieldParams {
  427. pub grid_id: String,
  428. pub field_orders: RepeatedFieldOrder,
  429. }
  430. impl TryInto<QueryFieldParams> for QueryFieldPayload {
  431. type Error = ErrorCode;
  432. fn try_into(self) -> Result<QueryFieldParams, Self::Error> {
  433. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  434. Ok(QueryFieldParams {
  435. grid_id: grid_id.0,
  436. field_orders: self.field_orders,
  437. })
  438. }
  439. }
  440. #[derive(ProtoBuf, Default)]
  441. pub struct QueryGridBlocksPayload {
  442. #[pb(index = 1)]
  443. pub grid_id: String,
  444. #[pb(index = 2)]
  445. pub block_orders: Vec<GridBlockOrder>,
  446. }
  447. pub struct QueryGridBlocksParams {
  448. pub grid_id: String,
  449. pub block_orders: Vec<GridBlockOrder>,
  450. }
  451. impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayload {
  452. type Error = ErrorCode;
  453. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  454. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  455. Ok(QueryGridBlocksParams {
  456. grid_id: grid_id.0,
  457. block_orders: self.block_orders,
  458. })
  459. }
  460. }
  461. #[derive(ProtoBuf, Default)]
  462. pub struct QueryRowPayload {
  463. #[pb(index = 1)]
  464. pub grid_id: String,
  465. #[pb(index = 3)]
  466. pub row_id: String,
  467. }
  468. pub struct QueryRowParams {
  469. pub grid_id: String,
  470. pub row_id: String,
  471. }
  472. impl TryInto<QueryRowParams> for QueryRowPayload {
  473. type Error = ErrorCode;
  474. fn try_into(self) -> Result<QueryRowParams, Self::Error> {
  475. let grid_id = NotEmptyUuid::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  476. let row_id = NotEmptyUuid::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
  477. Ok(QueryRowParams {
  478. grid_id: grid_id.0,
  479. row_id: row_id.0,
  480. })
  481. }
  482. }
  483. #[derive(ProtoBuf, Default)]
  484. pub struct CreateSelectOptionPayload {
  485. #[pb(index = 1)]
  486. pub option_name: String,
  487. #[pb(index = 2)]
  488. pub selected: bool,
  489. }
  490. pub struct CreateSelectOptionParams {
  491. pub option_name: String,
  492. pub selected: bool,
  493. }
  494. impl TryInto<CreateSelectOptionParams> for CreateSelectOptionPayload {
  495. type Error = ErrorCode;
  496. fn try_into(self) -> Result<CreateSelectOptionParams, Self::Error> {
  497. let option_name = NotEmptyStr::parse(self.option_name).map_err(|_| ErrorCode::SelectOptionNameIsEmpty)?;
  498. Ok(CreateSelectOptionParams {
  499. option_name: option_name.0,
  500. selected: self.selected,
  501. })
  502. }
  503. }