grid_rev.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. use crate::entities::{CellChangeset, Field, FieldOrder, FieldType, RowOrder};
  2. use crate::revision::GridSettingRevision;
  3. use bytes::Bytes;
  4. use indexmap::IndexMap;
  5. use nanoid::nanoid;
  6. use serde::{Deserialize, Serialize};
  7. use std::collections::HashMap;
  8. use std::sync::Arc;
  9. pub const DEFAULT_ROW_HEIGHT: i32 = 42;
  10. pub fn gen_grid_id() -> String {
  11. // nanoid calculator https://zelark.github.io/nano-id-cc/
  12. nanoid!(10)
  13. }
  14. pub fn gen_block_id() -> String {
  15. nanoid!(10)
  16. }
  17. pub fn gen_row_id() -> String {
  18. nanoid!(6)
  19. }
  20. pub fn gen_field_id() -> String {
  21. nanoid!(6)
  22. }
  23. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  24. pub struct GridRevision {
  25. pub grid_id: String,
  26. pub fields: Vec<FieldRevision>,
  27. pub blocks: Vec<Arc<GridBlockMetaRevision>>,
  28. #[serde(default)]
  29. pub setting: GridSettingRevision,
  30. }
  31. impl GridRevision {
  32. pub fn new(grid_id: &str) -> Self {
  33. Self {
  34. grid_id: grid_id.to_owned(),
  35. fields: vec![],
  36. blocks: vec![],
  37. setting: GridSettingRevision::default(),
  38. }
  39. }
  40. pub fn from_build_context(grid_id: &str, context: BuildGridContext) -> Self {
  41. Self {
  42. grid_id: grid_id.to_owned(),
  43. fields: context.field_revs,
  44. blocks: context.blocks.into_iter().map(Arc::new).collect(),
  45. setting: Default::default(),
  46. }
  47. }
  48. }
  49. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  50. pub struct GridBlockMetaRevision {
  51. pub block_id: String,
  52. pub start_row_index: i32,
  53. pub row_count: i32,
  54. }
  55. impl GridBlockMetaRevision {
  56. pub fn len(&self) -> i32 {
  57. self.row_count
  58. }
  59. pub fn is_empty(&self) -> bool {
  60. self.row_count == 0
  61. }
  62. }
  63. impl GridBlockMetaRevision {
  64. pub fn new() -> Self {
  65. GridBlockMetaRevision {
  66. block_id: gen_block_id(),
  67. ..Default::default()
  68. }
  69. }
  70. }
  71. pub struct GridBlockMetaRevisionChangeset {
  72. pub block_id: String,
  73. pub start_row_index: Option<i32>,
  74. pub row_count: Option<i32>,
  75. }
  76. impl GridBlockMetaRevisionChangeset {
  77. pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
  78. Self {
  79. block_id: block_id.to_string(),
  80. start_row_index: None,
  81. row_count: Some(row_count),
  82. }
  83. }
  84. }
  85. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  86. pub struct GridBlockRevision {
  87. pub block_id: String,
  88. pub rows: Vec<Arc<RowRevision>>,
  89. }
  90. #[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
  91. pub struct FieldRevision {
  92. pub id: String,
  93. pub name: String,
  94. pub desc: String,
  95. pub field_type: FieldType,
  96. pub frozen: bool,
  97. pub visibility: bool,
  98. pub width: i32,
  99. /// type_options contains key/value pairs
  100. /// key: id of the FieldType
  101. /// value: type option data that can be parsed into specified TypeOptionStruct.
  102. /// For example, CheckboxTypeOption, MultiSelectTypeOption etc.
  103. #[serde(with = "indexmap::serde_seq")]
  104. pub type_options: IndexMap<String, String>,
  105. #[serde(default = "DEFAULT_IS_PRIMARY")]
  106. pub is_primary: bool,
  107. }
  108. const DEFAULT_IS_PRIMARY: fn() -> bool = || false;
  109. impl FieldRevision {
  110. pub fn new(name: &str, desc: &str, field_type: FieldType, is_primary: bool) -> Self {
  111. let width = field_type.default_cell_width();
  112. Self {
  113. id: gen_field_id(),
  114. name: name.to_string(),
  115. desc: desc.to_string(),
  116. field_type,
  117. frozen: false,
  118. visibility: true,
  119. width,
  120. type_options: Default::default(),
  121. is_primary,
  122. }
  123. }
  124. pub fn insert_type_option_entry<T>(&mut self, entry: &T)
  125. where
  126. T: TypeOptionDataEntry + ?Sized,
  127. {
  128. self.type_options.insert(entry.field_type().type_id(), entry.json_str());
  129. }
  130. pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type: &FieldType) -> Option<T> {
  131. self.type_options
  132. .get(&field_type.type_id())
  133. .map(|s| T::from_json_str(s))
  134. }
  135. pub fn insert_type_option_str(&mut self, field_type: &FieldType, json_str: String) {
  136. self.type_options.insert(field_type.type_id(), json_str);
  137. }
  138. pub fn get_type_option_str(&self, field_type: &FieldType) -> Option<String> {
  139. self.type_options.get(&field_type.type_id()).map(|s| s.to_owned())
  140. }
  141. }
  142. impl std::convert::From<FieldRevision> for Field {
  143. fn from(field_rev: FieldRevision) -> Self {
  144. Self {
  145. id: field_rev.id,
  146. name: field_rev.name,
  147. desc: field_rev.desc,
  148. field_type: field_rev.field_type,
  149. frozen: field_rev.frozen,
  150. visibility: field_rev.visibility,
  151. width: field_rev.width,
  152. is_primary: field_rev.is_primary,
  153. }
  154. }
  155. }
  156. impl std::convert::From<&FieldRevision> for FieldOrder {
  157. fn from(field_rev: &FieldRevision) -> Self {
  158. Self {
  159. field_id: field_rev.id.clone(),
  160. }
  161. }
  162. }
  163. pub trait TypeOptionDataEntry {
  164. fn field_type(&self) -> FieldType;
  165. fn json_str(&self) -> String;
  166. fn protobuf_bytes(&self) -> Bytes;
  167. }
  168. pub trait TypeOptionDataDeserializer {
  169. fn from_json_str(s: &str) -> Self;
  170. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  171. }
  172. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  173. pub struct RowRevision {
  174. pub id: String,
  175. pub block_id: String,
  176. /// cells contains key/value pairs.
  177. /// key: field id,
  178. /// value: CellMeta
  179. #[serde(with = "indexmap::serde_seq")]
  180. pub cells: IndexMap<String, CellRevision>,
  181. pub height: i32,
  182. pub visibility: bool,
  183. }
  184. impl RowRevision {
  185. pub fn new(block_id: &str) -> Self {
  186. Self {
  187. id: gen_row_id(),
  188. block_id: block_id.to_owned(),
  189. cells: Default::default(),
  190. height: DEFAULT_ROW_HEIGHT,
  191. visibility: true,
  192. }
  193. }
  194. }
  195. impl std::convert::From<&RowRevision> for RowOrder {
  196. fn from(row: &RowRevision) -> Self {
  197. Self {
  198. row_id: row.id.clone(),
  199. block_id: row.block_id.clone(),
  200. height: row.height,
  201. }
  202. }
  203. }
  204. impl std::convert::From<&Arc<RowRevision>> for RowOrder {
  205. fn from(row: &Arc<RowRevision>) -> Self {
  206. Self {
  207. row_id: row.id.clone(),
  208. block_id: row.block_id.clone(),
  209. height: row.height,
  210. }
  211. }
  212. }
  213. #[derive(Debug, Clone, Default)]
  214. pub struct RowMetaChangeset {
  215. pub row_id: String,
  216. pub height: Option<i32>,
  217. pub visibility: Option<bool>,
  218. pub cell_by_field_id: HashMap<String, CellRevision>,
  219. }
  220. impl std::convert::From<CellChangeset> for RowMetaChangeset {
  221. fn from(changeset: CellChangeset) -> Self {
  222. let mut cell_by_field_id = HashMap::with_capacity(1);
  223. let field_id = changeset.field_id;
  224. let cell_rev = CellRevision {
  225. data: changeset.cell_content_changeset.unwrap_or_else(|| "".to_owned()),
  226. };
  227. cell_by_field_id.insert(field_id, cell_rev);
  228. RowMetaChangeset {
  229. row_id: changeset.row_id,
  230. height: None,
  231. visibility: None,
  232. cell_by_field_id,
  233. }
  234. }
  235. }
  236. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
  237. pub struct CellRevision {
  238. pub data: String,
  239. }
  240. impl CellRevision {
  241. pub fn new(data: String) -> Self {
  242. Self { data }
  243. }
  244. }
  245. #[derive(Clone, Default, Deserialize, Serialize)]
  246. pub struct BuildGridContext {
  247. pub field_revs: Vec<FieldRevision>,
  248. pub blocks: Vec<GridBlockMetaRevision>,
  249. pub blocks_meta_data: Vec<GridBlockRevision>,
  250. }
  251. impl BuildGridContext {
  252. pub fn new() -> Self {
  253. Self::default()
  254. }
  255. }
  256. impl std::convert::From<BuildGridContext> for Bytes {
  257. fn from(ctx: BuildGridContext) -> Self {
  258. let bytes = serde_json::to_vec(&ctx).unwrap_or_else(|_| vec![]);
  259. Bytes::from(bytes)
  260. }
  261. }
  262. impl std::convert::TryFrom<Bytes> for BuildGridContext {
  263. type Error = serde_json::Error;
  264. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  265. let ctx: BuildGridContext = serde_json::from_slice(&bytes)?;
  266. Ok(ctx)
  267. }
  268. }