grid_rev.rs 6.9 KB

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