grid.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. use crate::entities::FieldOrder;
  2. use crate::parser::NotEmptyStr;
  3. use crate::revision::RowRevision;
  4. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  5. use flowy_error_code::ErrorCode;
  6. #[derive(Debug, Clone, Default, ProtoBuf)]
  7. pub struct Grid {
  8. #[pb(index = 1)]
  9. pub id: String,
  10. #[pb(index = 2)]
  11. pub field_orders: Vec<FieldOrder>,
  12. #[pb(index = 3)]
  13. pub blocks: Vec<GridBlock>,
  14. }
  15. #[derive(Debug, Default, Clone, ProtoBuf)]
  16. pub struct RowOrder {
  17. #[pb(index = 1)]
  18. pub row_id: String,
  19. #[pb(index = 2)]
  20. pub block_id: String,
  21. #[pb(index = 3)]
  22. pub height: i32,
  23. }
  24. #[derive(Debug, Default, ProtoBuf)]
  25. pub struct Row {
  26. #[pb(index = 1)]
  27. pub id: String,
  28. #[pb(index = 2)]
  29. pub height: i32,
  30. }
  31. #[derive(Debug, Default, ProtoBuf)]
  32. pub struct OptionalRow {
  33. #[pb(index = 1, one_of)]
  34. pub row: Option<Row>,
  35. }
  36. #[derive(Debug, Default, ProtoBuf)]
  37. pub struct RepeatedRow {
  38. #[pb(index = 1)]
  39. pub items: Vec<Row>,
  40. }
  41. impl std::convert::From<Vec<Row>> for RepeatedRow {
  42. fn from(items: Vec<Row>) -> Self {
  43. Self { items }
  44. }
  45. }
  46. #[derive(Debug, Default, ProtoBuf)]
  47. pub struct RepeatedGridBlock {
  48. #[pb(index = 1)]
  49. pub items: Vec<GridBlock>,
  50. }
  51. impl std::convert::From<Vec<GridBlock>> for RepeatedGridBlock {
  52. fn from(items: Vec<GridBlock>) -> Self {
  53. Self { items }
  54. }
  55. }
  56. #[derive(Debug, Clone, Default, ProtoBuf)]
  57. pub struct IndexRowOrder {
  58. #[pb(index = 1)]
  59. pub row_order: RowOrder,
  60. #[pb(index = 2, one_of)]
  61. pub index: Option<i32>,
  62. }
  63. #[derive(Debug, Default, ProtoBuf)]
  64. pub struct UpdatedRowOrder {
  65. #[pb(index = 1)]
  66. pub row_order: RowOrder,
  67. #[pb(index = 2)]
  68. pub row: Row,
  69. }
  70. impl UpdatedRowOrder {
  71. pub fn new(row_rev: &RowRevision, row: Row) -> Self {
  72. Self {
  73. row_order: RowOrder::from(row_rev),
  74. row,
  75. }
  76. }
  77. }
  78. #[derive(Debug, Default, ProtoBuf)]
  79. pub struct GridRowsChangeset {
  80. #[pb(index = 1)]
  81. pub block_id: String,
  82. #[pb(index = 2)]
  83. pub inserted_rows: Vec<IndexRowOrder>,
  84. #[pb(index = 3)]
  85. pub deleted_rows: Vec<RowOrder>,
  86. #[pb(index = 4)]
  87. pub updated_rows: Vec<UpdatedRowOrder>,
  88. }
  89. impl std::convert::From<RowOrder> for IndexRowOrder {
  90. fn from(row_order: RowOrder) -> Self {
  91. Self { row_order, index: None }
  92. }
  93. }
  94. impl std::convert::From<&RowRevision> for IndexRowOrder {
  95. fn from(row: &RowRevision) -> Self {
  96. let row_order = RowOrder::from(row);
  97. Self::from(row_order)
  98. }
  99. }
  100. impl GridRowsChangeset {
  101. pub fn insert(block_id: &str, inserted_rows: Vec<IndexRowOrder>) -> Self {
  102. Self {
  103. block_id: block_id.to_owned(),
  104. inserted_rows,
  105. deleted_rows: vec![],
  106. updated_rows: vec![],
  107. }
  108. }
  109. pub fn delete(block_id: &str, deleted_rows: Vec<RowOrder>) -> Self {
  110. Self {
  111. block_id: block_id.to_owned(),
  112. inserted_rows: vec![],
  113. deleted_rows,
  114. updated_rows: vec![],
  115. }
  116. }
  117. pub fn update(block_id: &str, updated_rows: Vec<UpdatedRowOrder>) -> Self {
  118. Self {
  119. block_id: block_id.to_owned(),
  120. inserted_rows: vec![],
  121. deleted_rows: vec![],
  122. updated_rows,
  123. }
  124. }
  125. }
  126. #[derive(Debug, Clone, Default, ProtoBuf)]
  127. pub struct GridBlock {
  128. #[pb(index = 1)]
  129. pub id: String,
  130. #[pb(index = 2)]
  131. pub row_orders: Vec<RowOrder>,
  132. }
  133. impl GridBlock {
  134. pub fn new(block_id: &str, row_orders: Vec<RowOrder>) -> Self {
  135. Self {
  136. id: block_id.to_owned(),
  137. row_orders,
  138. }
  139. }
  140. }
  141. #[derive(Debug, Default, ProtoBuf)]
  142. pub struct Cell {
  143. #[pb(index = 1)]
  144. pub field_id: String,
  145. #[pb(index = 2)]
  146. pub data: Vec<u8>,
  147. }
  148. impl Cell {
  149. pub fn new(field_id: &str, data: Vec<u8>) -> Self {
  150. Self {
  151. field_id: field_id.to_owned(),
  152. data,
  153. }
  154. }
  155. pub fn empty(field_id: &str) -> Self {
  156. Self {
  157. field_id: field_id.to_owned(),
  158. data: vec![],
  159. }
  160. }
  161. }
  162. #[derive(Debug, Default, ProtoBuf)]
  163. pub struct RepeatedCell {
  164. #[pb(index = 1)]
  165. pub items: Vec<Cell>,
  166. }
  167. impl std::ops::Deref for RepeatedCell {
  168. type Target = Vec<Cell>;
  169. fn deref(&self) -> &Self::Target {
  170. &self.items
  171. }
  172. }
  173. impl std::ops::DerefMut for RepeatedCell {
  174. fn deref_mut(&mut self) -> &mut Self::Target {
  175. &mut self.items
  176. }
  177. }
  178. impl std::convert::From<Vec<Cell>> for RepeatedCell {
  179. fn from(items: Vec<Cell>) -> Self {
  180. Self { items }
  181. }
  182. }
  183. #[derive(ProtoBuf, Default)]
  184. pub struct CreateGridPayload {
  185. #[pb(index = 1)]
  186. pub name: String,
  187. }
  188. #[derive(Clone, ProtoBuf, Default, Debug)]
  189. pub struct GridId {
  190. #[pb(index = 1)]
  191. pub value: String,
  192. }
  193. impl AsRef<str> for GridId {
  194. fn as_ref(&self) -> &str {
  195. &self.value
  196. }
  197. }
  198. #[derive(Clone, ProtoBuf, Default, Debug)]
  199. pub struct GridBlockId {
  200. #[pb(index = 1)]
  201. pub value: String,
  202. }
  203. impl AsRef<str> for GridBlockId {
  204. fn as_ref(&self) -> &str {
  205. &self.value
  206. }
  207. }
  208. impl std::convert::From<&str> for GridBlockId {
  209. fn from(s: &str) -> Self {
  210. GridBlockId { value: s.to_owned() }
  211. }
  212. }
  213. #[derive(ProtoBuf, Default)]
  214. pub struct CreateRowPayload {
  215. #[pb(index = 1)]
  216. pub grid_id: String,
  217. #[pb(index = 2, one_of)]
  218. pub start_row_id: Option<String>,
  219. }
  220. #[derive(Default)]
  221. pub struct CreateRowParams {
  222. pub grid_id: String,
  223. pub start_row_id: Option<String>,
  224. }
  225. impl TryInto<CreateRowParams> for CreateRowPayload {
  226. type Error = ErrorCode;
  227. fn try_into(self) -> Result<CreateRowParams, Self::Error> {
  228. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  229. Ok(CreateRowParams {
  230. grid_id: grid_id.0,
  231. start_row_id: self.start_row_id,
  232. })
  233. }
  234. }
  235. #[derive(ProtoBuf, Default)]
  236. pub struct QueryGridBlocksPayload {
  237. #[pb(index = 1)]
  238. pub grid_id: String,
  239. #[pb(index = 2)]
  240. pub block_ids: Vec<String>,
  241. }
  242. pub struct QueryGridBlocksParams {
  243. pub grid_id: String,
  244. pub block_ids: Vec<String>,
  245. }
  246. impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayload {
  247. type Error = ErrorCode;
  248. fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
  249. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  250. Ok(QueryGridBlocksParams {
  251. grid_id: grid_id.0,
  252. block_ids: self.block_ids,
  253. })
  254. }
  255. }
  256. #[derive(Debug, Clone, ProtoBuf_Enum)]
  257. pub enum MoveItemType {
  258. MoveField = 0,
  259. MoveRow = 1,
  260. }
  261. impl std::default::Default for MoveItemType {
  262. fn default() -> Self {
  263. MoveItemType::MoveField
  264. }
  265. }
  266. #[derive(Debug, Clone, Default, ProtoBuf)]
  267. pub struct MoveItemPayload {
  268. #[pb(index = 1)]
  269. pub grid_id: String,
  270. #[pb(index = 2)]
  271. pub item_id: String,
  272. #[pb(index = 3)]
  273. pub from_index: i32,
  274. #[pb(index = 4)]
  275. pub to_index: i32,
  276. #[pb(index = 5)]
  277. pub ty: MoveItemType,
  278. }
  279. #[derive(Clone)]
  280. pub struct MoveItemParams {
  281. pub grid_id: String,
  282. pub item_id: String,
  283. pub from_index: i32,
  284. pub to_index: i32,
  285. pub ty: MoveItemType,
  286. }
  287. impl TryInto<MoveItemParams> for MoveItemPayload {
  288. type Error = ErrorCode;
  289. fn try_into(self) -> Result<MoveItemParams, Self::Error> {
  290. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  291. let item_id = NotEmptyStr::parse(self.item_id).map_err(|_| ErrorCode::InvalidData)?;
  292. Ok(MoveItemParams {
  293. grid_id: grid_id.0,
  294. item_id: item_id.0,
  295. from_index: self.from_index,
  296. to_index: self.to_index,
  297. ty: self.ty,
  298. })
  299. }
  300. }
  301. #[derive(Debug, Clone, Default, ProtoBuf)]
  302. pub struct CellChangeset {
  303. #[pb(index = 1)]
  304. pub grid_id: String,
  305. #[pb(index = 2)]
  306. pub row_id: String,
  307. #[pb(index = 3)]
  308. pub field_id: String,
  309. #[pb(index = 4, one_of)]
  310. pub cell_content_changeset: Option<String>,
  311. }