grid_rev.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. use crate::revision::GridBlockRevision;
  2. use bytes::Bytes;
  3. use indexmap::IndexMap;
  4. use nanoid::nanoid;
  5. use serde::{Deserialize, Serialize};
  6. use std::sync::Arc;
  7. pub fn gen_grid_id() -> String {
  8. // nanoid calculator https://zelark.github.io/nano-id-cc/
  9. nanoid!(10)
  10. }
  11. pub fn gen_block_id() -> String {
  12. nanoid!(10)
  13. }
  14. pub fn gen_field_id() -> String {
  15. nanoid!(6)
  16. }
  17. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  18. pub struct GridRevision {
  19. pub grid_id: String,
  20. pub fields: Vec<Arc<FieldRevision>>,
  21. pub blocks: Vec<Arc<GridBlockMetaRevision>>,
  22. }
  23. impl GridRevision {
  24. pub fn new(grid_id: &str) -> Self {
  25. Self {
  26. grid_id: grid_id.to_owned(),
  27. fields: vec![],
  28. blocks: vec![],
  29. }
  30. }
  31. pub fn from_build_context(grid_id: &str, context: BuildGridContext) -> Self {
  32. Self {
  33. grid_id: grid_id.to_owned(),
  34. fields: context.field_revs,
  35. blocks: context.block_metas.into_iter().map(Arc::new).collect(),
  36. }
  37. }
  38. }
  39. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  40. pub struct GridBlockMetaRevision {
  41. pub block_id: String,
  42. pub start_row_index: i32,
  43. pub row_count: i32,
  44. }
  45. impl GridBlockMetaRevision {
  46. pub fn len(&self) -> i32 {
  47. self.row_count
  48. }
  49. pub fn is_empty(&self) -> bool {
  50. self.row_count == 0
  51. }
  52. }
  53. impl GridBlockMetaRevision {
  54. pub fn new() -> Self {
  55. GridBlockMetaRevision {
  56. block_id: gen_block_id(),
  57. ..Default::default()
  58. }
  59. }
  60. }
  61. pub struct GridBlockMetaRevisionChangeset {
  62. pub block_id: String,
  63. pub start_row_index: Option<i32>,
  64. pub row_count: Option<i32>,
  65. }
  66. impl GridBlockMetaRevisionChangeset {
  67. pub fn from_row_count(block_id: String, row_count: i32) -> Self {
  68. Self {
  69. block_id,
  70. start_row_index: None,
  71. row_count: Some(row_count),
  72. }
  73. }
  74. }
  75. #[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
  76. pub struct FieldRevision {
  77. pub id: String,
  78. pub name: String,
  79. pub desc: String,
  80. #[serde(rename = "field_type")]
  81. pub ty: FieldTypeRevision,
  82. pub frozen: bool,
  83. pub visibility: bool,
  84. pub width: i32,
  85. /// type_options contains key/value pairs
  86. /// key: id of the FieldType
  87. /// value: type option data that can be parsed into specified TypeOptionStruct.
  88. ///
  89. /// For example, CheckboxTypeOption, MultiSelectTypeOption etc.
  90. #[serde(with = "indexmap::serde_seq")]
  91. pub type_options: IndexMap<String, String>,
  92. #[serde(default = "DEFAULT_IS_PRIMARY")]
  93. pub is_primary: bool,
  94. }
  95. impl AsRef<FieldRevision> for FieldRevision {
  96. fn as_ref(&self) -> &FieldRevision {
  97. self
  98. }
  99. }
  100. const DEFAULT_IS_PRIMARY: fn() -> bool = || false;
  101. impl FieldRevision {
  102. pub fn new<T: Into<FieldTypeRevision>>(
  103. name: &str,
  104. desc: &str,
  105. field_type: T,
  106. width: i32,
  107. is_primary: bool,
  108. ) -> Self {
  109. Self {
  110. id: gen_field_id(),
  111. name: name.to_string(),
  112. desc: desc.to_string(),
  113. ty: field_type.into(),
  114. frozen: false,
  115. visibility: true,
  116. width,
  117. type_options: Default::default(),
  118. is_primary,
  119. }
  120. }
  121. pub fn insert_type_option_entry<T>(&mut self, entry: &T)
  122. where
  123. T: TypeOptionDataEntry + ?Sized,
  124. {
  125. let id = self.ty.to_string();
  126. self.type_options.insert(id, entry.json_str());
  127. }
  128. pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type_rev: FieldTypeRevision) -> Option<T> {
  129. let id = field_type_rev.to_string();
  130. // TODO: cache the deserialized type option
  131. self.type_options.get(&id).map(|s| T::from_json_str(s))
  132. }
  133. pub fn insert_type_option_str(&mut self, field_type: &FieldTypeRevision, json_str: String) {
  134. let id = field_type.to_string();
  135. self.type_options.insert(id, json_str);
  136. }
  137. pub fn get_type_option_str<T: Into<FieldTypeRevision>>(&self, field_type: T) -> Option<String> {
  138. let field_type_rev = field_type.into();
  139. let id = field_type_rev.to_string();
  140. self.type_options.get(&id).map(|s| s.to_owned())
  141. }
  142. }
  143. /// The macro [impl_type_option] will implement the [TypeOptionDataEntry] for the type that
  144. /// supports the serde trait and the TryInto<Bytes> trait.
  145. pub trait TypeOptionDataEntry {
  146. fn json_str(&self) -> String;
  147. fn protobuf_bytes(&self) -> Bytes;
  148. }
  149. /// The macro [impl_type_option] will implement the [TypeOptionDataDeserializer] for the type that
  150. /// supports the serde trait and the TryFrom<Bytes> trait.
  151. pub trait TypeOptionDataDeserializer {
  152. fn from_json_str(s: &str) -> Self;
  153. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  154. }
  155. #[derive(Clone, Default, Deserialize, Serialize)]
  156. pub struct BuildGridContext {
  157. pub field_revs: Vec<Arc<FieldRevision>>,
  158. pub block_metas: Vec<GridBlockMetaRevision>,
  159. pub blocks: Vec<GridBlockRevision>,
  160. }
  161. impl BuildGridContext {
  162. pub fn new() -> Self {
  163. Self::default()
  164. }
  165. }
  166. impl std::convert::From<BuildGridContext> for Bytes {
  167. fn from(ctx: BuildGridContext) -> Self {
  168. let bytes = serde_json::to_vec(&ctx).unwrap_or_else(|_| vec![]);
  169. Bytes::from(bytes)
  170. }
  171. }
  172. impl std::convert::TryFrom<Bytes> for BuildGridContext {
  173. type Error = serde_json::Error;
  174. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  175. let ctx: BuildGridContext = serde_json::from_slice(&bytes)?;
  176. Ok(ctx)
  177. }
  178. }
  179. pub type FieldTypeRevision = u8;