grid_rev.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. use crate::revision::GridSettingRevision;
  2. use bytes::Bytes;
  3. use indexmap::IndexMap;
  4. use nanoid::nanoid;
  5. use serde::{Deserialize, Serialize};
  6. use std::collections::HashMap;
  7. use std::sync::Arc;
  8. pub const DEFAULT_ROW_HEIGHT: i32 = 42;
  9. pub fn gen_grid_id() -> String {
  10. // nanoid calculator https://zelark.github.io/nano-id-cc/
  11. nanoid!(10)
  12. }
  13. pub fn gen_block_id() -> String {
  14. nanoid!(10)
  15. }
  16. pub fn gen_row_id() -> String {
  17. nanoid!(6)
  18. }
  19. pub fn gen_field_id() -> String {
  20. nanoid!(6)
  21. }
  22. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  23. pub struct GridRevision {
  24. pub grid_id: String,
  25. pub fields: Vec<Arc<FieldRevision>>,
  26. pub blocks: Vec<Arc<GridBlockMetaRevision>>,
  27. #[serde(default, skip)]
  28. pub setting: GridSettingRevision,
  29. }
  30. impl GridRevision {
  31. pub fn new(grid_id: &str) -> Self {
  32. Self {
  33. grid_id: grid_id.to_owned(),
  34. fields: vec![],
  35. blocks: vec![],
  36. setting: GridSettingRevision::default(),
  37. }
  38. }
  39. pub fn from_build_context(grid_id: &str, context: BuildGridContext) -> Self {
  40. Self {
  41. grid_id: grid_id.to_owned(),
  42. fields: context.field_revs.into_iter().map(Arc::new).collect(),
  43. blocks: context.blocks.into_iter().map(Arc::new).collect(),
  44. setting: Default::default(),
  45. }
  46. }
  47. }
  48. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  49. pub struct GridBlockMetaRevision {
  50. pub block_id: String,
  51. pub start_row_index: i32,
  52. pub row_count: i32,
  53. }
  54. impl GridBlockMetaRevision {
  55. pub fn len(&self) -> i32 {
  56. self.row_count
  57. }
  58. pub fn is_empty(&self) -> bool {
  59. self.row_count == 0
  60. }
  61. }
  62. impl GridBlockMetaRevision {
  63. pub fn new() -> Self {
  64. GridBlockMetaRevision {
  65. block_id: gen_block_id(),
  66. ..Default::default()
  67. }
  68. }
  69. }
  70. pub struct GridBlockMetaRevisionChangeset {
  71. pub block_id: String,
  72. pub start_row_index: Option<i32>,
  73. pub row_count: Option<i32>,
  74. }
  75. impl GridBlockMetaRevisionChangeset {
  76. pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
  77. Self {
  78. block_id: block_id.to_string(),
  79. start_row_index: None,
  80. row_count: Some(row_count),
  81. }
  82. }
  83. }
  84. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  85. pub struct GridBlockRevision {
  86. pub block_id: String,
  87. pub rows: Vec<Arc<RowRevision>>,
  88. }
  89. #[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
  90. pub struct FieldRevision {
  91. pub id: String,
  92. pub name: String,
  93. pub desc: String,
  94. #[serde(rename = "field_type")]
  95. pub field_type_rev: FieldTypeRevision,
  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. impl AsRef<FieldRevision> for FieldRevision {
  109. fn as_ref(&self) -> &FieldRevision {
  110. self
  111. }
  112. }
  113. const DEFAULT_IS_PRIMARY: fn() -> bool = || false;
  114. impl FieldRevision {
  115. pub fn new<T: Into<FieldTypeRevision>>(
  116. name: &str,
  117. desc: &str,
  118. field_type: T,
  119. width: i32,
  120. is_primary: bool,
  121. ) -> Self {
  122. Self {
  123. id: gen_field_id(),
  124. name: name.to_string(),
  125. desc: desc.to_string(),
  126. field_type_rev: field_type.into(),
  127. frozen: false,
  128. visibility: true,
  129. width,
  130. type_options: Default::default(),
  131. is_primary,
  132. }
  133. }
  134. pub fn insert_type_option_entry<T>(&mut self, entry: &T)
  135. where
  136. T: TypeOptionDataEntry + ?Sized,
  137. {
  138. let id = self.field_type_rev.to_string();
  139. self.type_options.insert(id, entry.json_str());
  140. }
  141. pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type_rev: FieldTypeRevision) -> Option<T> {
  142. let id = field_type_rev.to_string();
  143. self.type_options.get(&id).map(|s| T::from_json_str(s))
  144. }
  145. pub fn insert_type_option_str(&mut self, field_type: &FieldTypeRevision, json_str: String) {
  146. let id = field_type.to_string();
  147. self.type_options.insert(id, json_str);
  148. }
  149. pub fn get_type_option_str<T: Into<FieldTypeRevision>>(&self, field_type: T) -> Option<String> {
  150. let field_type_rev = field_type.into();
  151. let id = field_type_rev.to_string();
  152. self.type_options.get(&id).map(|s| s.to_owned())
  153. }
  154. }
  155. pub trait TypeOptionDataEntry {
  156. fn json_str(&self) -> String;
  157. fn protobuf_bytes(&self) -> Bytes;
  158. }
  159. pub trait TypeOptionDataDeserializer {
  160. fn from_json_str(s: &str) -> Self;
  161. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  162. }
  163. pub type FieldId = String;
  164. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  165. pub struct RowRevision {
  166. pub id: String,
  167. pub block_id: String,
  168. /// cells contains key/value pairs.
  169. /// key: field id,
  170. /// value: CellMeta
  171. #[serde(with = "indexmap::serde_seq")]
  172. pub cells: IndexMap<FieldId, CellRevision>,
  173. pub height: i32,
  174. pub visibility: bool,
  175. }
  176. impl RowRevision {
  177. pub fn new(block_id: &str) -> Self {
  178. Self {
  179. id: gen_row_id(),
  180. block_id: block_id.to_owned(),
  181. cells: Default::default(),
  182. height: DEFAULT_ROW_HEIGHT,
  183. visibility: true,
  184. }
  185. }
  186. }
  187. #[derive(Debug, Clone, Default)]
  188. pub struct RowMetaChangeset {
  189. pub row_id: String,
  190. pub height: Option<i32>,
  191. pub visibility: Option<bool>,
  192. pub cell_by_field_id: HashMap<FieldId, CellRevision>,
  193. }
  194. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
  195. pub struct CellRevision {
  196. pub data: String,
  197. }
  198. impl CellRevision {
  199. pub fn new(data: String) -> Self {
  200. Self { data }
  201. }
  202. }
  203. #[derive(Clone, Default, Deserialize, Serialize)]
  204. pub struct BuildGridContext {
  205. pub field_revs: Vec<FieldRevision>,
  206. pub blocks: Vec<GridBlockMetaRevision>,
  207. pub blocks_meta_data: Vec<GridBlockRevision>,
  208. }
  209. impl BuildGridContext {
  210. pub fn new() -> Self {
  211. Self::default()
  212. }
  213. }
  214. impl std::convert::From<BuildGridContext> for Bytes {
  215. fn from(ctx: BuildGridContext) -> Self {
  216. let bytes = serde_json::to_vec(&ctx).unwrap_or_else(|_| vec![]);
  217. Bytes::from(bytes)
  218. }
  219. }
  220. impl std::convert::TryFrom<Bytes> for BuildGridContext {
  221. type Error = serde_json::Error;
  222. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  223. let ctx: BuildGridContext = serde_json::from_slice(&bytes)?;
  224. Ok(ctx)
  225. }
  226. }
  227. pub type FieldTypeRevision = u8;