grid.rs 12 KB

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