grid.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. use crate::entities::{FieldMeta, FieldType, RowMeta};
  2. use flowy_derive::ProtoBuf;
  3. use std::collections::HashMap;
  4. use std::sync::Arc;
  5. #[derive(Debug, Clone, Default, ProtoBuf)]
  6. pub struct Grid {
  7. #[pb(index = 1)]
  8. pub id: String,
  9. #[pb(index = 2)]
  10. pub field_orders: Vec<FieldOrder>,
  11. #[pb(index = 3)]
  12. pub block_orders: Vec<GridBlockOrder>,
  13. }
  14. #[derive(Debug, Clone, Default, ProtoBuf)]
  15. pub struct Field {
  16. #[pb(index = 1)]
  17. pub id: String,
  18. #[pb(index = 2)]
  19. pub name: String,
  20. #[pb(index = 3)]
  21. pub desc: String,
  22. #[pb(index = 4)]
  23. pub field_type: FieldType,
  24. #[pb(index = 5)]
  25. pub frozen: bool,
  26. #[pb(index = 6)]
  27. pub visibility: bool,
  28. #[pb(index = 7)]
  29. pub width: i32,
  30. }
  31. #[derive(Debug, Clone, Default, ProtoBuf)]
  32. pub struct FieldOrder {
  33. #[pb(index = 1)]
  34. pub field_id: String,
  35. }
  36. impl std::convert::From<&FieldMeta> for FieldOrder {
  37. fn from(field_meta: &FieldMeta) -> Self {
  38. Self {
  39. field_id: field_meta.id.clone(),
  40. }
  41. }
  42. }
  43. impl std::convert::From<FieldMeta> for Field {
  44. fn from(field_meta: FieldMeta) -> Self {
  45. Self {
  46. id: field_meta.id,
  47. name: field_meta.name,
  48. desc: field_meta.desc,
  49. field_type: field_meta.field_type,
  50. frozen: field_meta.frozen,
  51. visibility: field_meta.visibility,
  52. width: field_meta.width,
  53. }
  54. }
  55. }
  56. #[derive(Debug, Default, ProtoBuf)]
  57. pub struct CreateEditFieldContextParams {
  58. #[pb(index = 1)]
  59. pub grid_id: String,
  60. #[pb(index = 2)]
  61. pub field_type: FieldType,
  62. }
  63. #[derive(Debug, Default, ProtoBuf)]
  64. pub struct EditFieldContext {
  65. #[pb(index = 1)]
  66. pub grid_id: String,
  67. #[pb(index = 2)]
  68. pub grid_field: Field,
  69. #[pb(index = 3)]
  70. pub type_option_data: Vec<u8>,
  71. }
  72. #[derive(Debug, Default, ProtoBuf)]
  73. pub struct RepeatedField {
  74. #[pb(index = 1)]
  75. pub items: Vec<Field>,
  76. }
  77. impl std::ops::Deref for RepeatedField {
  78. type Target = Vec<Field>;
  79. fn deref(&self) -> &Self::Target {
  80. &self.items
  81. }
  82. }
  83. impl std::ops::DerefMut for RepeatedField {
  84. fn deref_mut(&mut self) -> &mut Self::Target {
  85. &mut self.items
  86. }
  87. }
  88. impl std::convert::From<Vec<Field>> for RepeatedField {
  89. fn from(items: Vec<Field>) -> Self {
  90. Self { items }
  91. }
  92. }
  93. #[derive(Debug, Clone, Default, ProtoBuf)]
  94. pub struct RepeatedFieldOrder {
  95. #[pb(index = 1)]
  96. pub items: Vec<FieldOrder>,
  97. }
  98. impl std::ops::Deref for RepeatedFieldOrder {
  99. type Target = Vec<FieldOrder>;
  100. fn deref(&self) -> &Self::Target {
  101. &self.items
  102. }
  103. }
  104. #[derive(Debug, Default, Clone, ProtoBuf)]
  105. pub struct RowOrder {
  106. #[pb(index = 1)]
  107. pub row_id: String,
  108. #[pb(index = 2)]
  109. pub block_id: String,
  110. #[pb(index = 3)]
  111. pub height: i32,
  112. }
  113. impl std::convert::From<&RowMeta> for RowOrder {
  114. fn from(row: &RowMeta) -> Self {
  115. Self {
  116. row_id: row.id.clone(),
  117. block_id: row.block_id.clone(),
  118. height: row.height,
  119. }
  120. }
  121. }
  122. impl std::convert::From<&Arc<RowMeta>> for RowOrder {
  123. fn from(row: &Arc<RowMeta>) -> Self {
  124. Self {
  125. row_id: row.id.clone(),
  126. block_id: row.block_id.clone(),
  127. height: row.height,
  128. }
  129. }
  130. }
  131. #[derive(Debug, Default, ProtoBuf)]
  132. pub struct Row {
  133. #[pb(index = 1)]
  134. pub id: String,
  135. #[pb(index = 2)]
  136. pub cell_by_field_id: HashMap<String, Cell>,
  137. #[pb(index = 3)]
  138. pub height: i32,
  139. }
  140. #[derive(Debug, Default, ProtoBuf)]
  141. pub struct RepeatedRow {
  142. #[pb(index = 1)]
  143. pub items: Vec<Row>,
  144. }
  145. impl std::convert::From<Vec<Row>> for RepeatedRow {
  146. fn from(items: Vec<Row>) -> Self {
  147. Self { items }
  148. }
  149. }
  150. #[derive(Debug, Default, ProtoBuf)]
  151. pub struct RepeatedGridBlock {
  152. #[pb(index = 1)]
  153. pub items: Vec<GridBlock>,
  154. }
  155. impl std::convert::From<Vec<GridBlock>> for RepeatedGridBlock {
  156. fn from(items: Vec<GridBlock>) -> Self {
  157. Self { items }
  158. }
  159. }
  160. #[derive(Debug, Clone, Default, ProtoBuf)]
  161. pub struct GridBlockOrder {
  162. #[pb(index = 1)]
  163. pub block_id: String,
  164. }
  165. impl std::convert::From<&str> for GridBlockOrder {
  166. fn from(s: &str) -> Self {
  167. GridBlockOrder { block_id: s.to_owned() }
  168. }
  169. }
  170. #[derive(Debug, Default, ProtoBuf)]
  171. pub struct GridBlock {
  172. #[pb(index = 1)]
  173. pub id: String,
  174. #[pb(index = 2)]
  175. pub row_orders: Vec<RowOrder>,
  176. }
  177. impl GridBlock {
  178. pub fn new(block_id: &str, row_orders: Vec<RowOrder>) -> Self {
  179. Self {
  180. id: block_id.to_owned(),
  181. row_orders,
  182. }
  183. }
  184. }
  185. #[derive(Debug, Default, ProtoBuf)]
  186. pub struct Cell {
  187. #[pb(index = 1)]
  188. pub field_id: String,
  189. #[pb(index = 2)]
  190. pub content: String,
  191. }
  192. impl Cell {
  193. pub fn new(field_id: &str, content: String) -> Self {
  194. Self {
  195. field_id: field_id.to_owned(),
  196. content,
  197. }
  198. }
  199. }
  200. #[derive(Debug, Default, ProtoBuf)]
  201. pub struct RepeatedCell {
  202. #[pb(index = 1)]
  203. pub items: Vec<Cell>,
  204. }
  205. impl std::ops::Deref for RepeatedCell {
  206. type Target = Vec<Cell>;
  207. fn deref(&self) -> &Self::Target {
  208. &self.items
  209. }
  210. }
  211. impl std::ops::DerefMut for RepeatedCell {
  212. fn deref_mut(&mut self) -> &mut Self::Target {
  213. &mut self.items
  214. }
  215. }
  216. impl std::convert::From<Vec<Cell>> for RepeatedCell {
  217. fn from(items: Vec<Cell>) -> Self {
  218. Self { items }
  219. }
  220. }
  221. #[derive(ProtoBuf, Default)]
  222. pub struct CreateGridPayload {
  223. #[pb(index = 1)]
  224. pub name: String,
  225. }
  226. #[derive(Clone, ProtoBuf, Default, Debug)]
  227. pub struct GridId {
  228. #[pb(index = 1)]
  229. pub value: String,
  230. }
  231. impl AsRef<str> for GridId {
  232. fn as_ref(&self) -> &str {
  233. &self.value
  234. }
  235. }
  236. #[derive(Clone, ProtoBuf, Default, Debug)]
  237. pub struct GridBlockId {
  238. #[pb(index = 1)]
  239. pub value: String,
  240. }
  241. impl AsRef<str> for GridBlockId {
  242. fn as_ref(&self) -> &str {
  243. &self.value
  244. }
  245. }
  246. impl std::convert::From<&str> for GridBlockId {
  247. fn from(s: &str) -> Self {
  248. GridBlockId { value: s.to_owned() }
  249. }
  250. }
  251. #[derive(ProtoBuf, Default)]
  252. pub struct CreateRowPayload {
  253. #[pb(index = 1)]
  254. pub grid_id: String,
  255. #[pb(index = 2, one_of)]
  256. pub start_row_id: Option<String>,
  257. }
  258. #[derive(ProtoBuf, Default)]
  259. pub struct CreateFieldPayload {
  260. #[pb(index = 1)]
  261. pub grid_id: String,
  262. #[pb(index = 2)]
  263. pub field: Field,
  264. #[pb(index = 3)]
  265. pub type_option_data: Vec<u8>,
  266. #[pb(index = 4, one_of)]
  267. pub start_field_id: Option<String>,
  268. }
  269. #[derive(ProtoBuf, Default)]
  270. pub struct QueryFieldPayload {
  271. #[pb(index = 1)]
  272. pub grid_id: String,
  273. #[pb(index = 2)]
  274. pub field_orders: RepeatedFieldOrder,
  275. }
  276. #[derive(ProtoBuf, Default)]
  277. pub struct QueryGridBlocksPayload {
  278. #[pb(index = 1)]
  279. pub grid_id: String,
  280. #[pb(index = 2)]
  281. pub block_orders: Vec<GridBlockOrder>,
  282. }
  283. #[derive(ProtoBuf, Default)]
  284. pub struct QueryRowPayload {
  285. #[pb(index = 1)]
  286. pub grid_id: String,
  287. #[pb(index = 2)]
  288. pub block_id: String,
  289. #[pb(index = 3)]
  290. pub row_id: String,
  291. }