database_rev.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. use crate::{DatabaseBlockRevision, LayoutSetting};
  2. use bytes::Bytes;
  3. use indexmap::IndexMap;
  4. use nanoid::nanoid;
  5. use serde::{Deserialize, Serialize};
  6. use serde_repr::{Deserialize_repr, Serialize_repr};
  7. use std::sync::Arc;
  8. pub fn gen_database_id() -> String {
  9. // nanoid calculator https://zelark.github.io/nano-id-cc/
  10. format!("d:{}", nanoid!(10))
  11. }
  12. pub fn gen_block_id() -> String {
  13. format!("b:{}", nanoid!(10))
  14. }
  15. pub fn gen_field_id() -> String {
  16. nanoid!(6)
  17. }
  18. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  19. pub struct DatabaseRevision {
  20. #[serde(rename = "grid_id")]
  21. pub database_id: String,
  22. pub fields: Vec<Arc<FieldRevision>>,
  23. pub blocks: Vec<Arc<DatabaseBlockMetaRevision>>,
  24. }
  25. impl DatabaseRevision {
  26. pub fn new(database_id: &str) -> Self {
  27. Self {
  28. database_id: database_id.to_owned(),
  29. fields: vec![],
  30. blocks: vec![],
  31. }
  32. }
  33. pub fn from_build_context(
  34. database_id: &str,
  35. field_revs: Vec<Arc<FieldRevision>>,
  36. block_metas: Vec<DatabaseBlockMetaRevision>,
  37. ) -> Self {
  38. Self {
  39. database_id: database_id.to_owned(),
  40. fields: field_revs,
  41. blocks: block_metas.into_iter().map(Arc::new).collect(),
  42. }
  43. }
  44. }
  45. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  46. pub struct DatabaseBlockMetaRevision {
  47. pub block_id: String,
  48. pub start_row_index: i32,
  49. pub row_count: i32,
  50. }
  51. impl DatabaseBlockMetaRevision {
  52. pub fn len(&self) -> i32 {
  53. self.row_count
  54. }
  55. pub fn is_empty(&self) -> bool {
  56. self.row_count == 0
  57. }
  58. }
  59. impl DatabaseBlockMetaRevision {
  60. pub fn new() -> Self {
  61. DatabaseBlockMetaRevision {
  62. block_id: gen_block_id(),
  63. ..Default::default()
  64. }
  65. }
  66. }
  67. pub struct DatabaseBlockMetaRevisionChangeset {
  68. pub block_id: String,
  69. pub start_row_index: Option<i32>,
  70. pub row_count: Option<i32>,
  71. }
  72. impl DatabaseBlockMetaRevisionChangeset {
  73. pub fn from_row_count(block_id: String, row_count: i32) -> Self {
  74. Self {
  75. block_id,
  76. start_row_index: None,
  77. row_count: Some(row_count),
  78. }
  79. }
  80. }
  81. #[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
  82. pub struct FieldRevision {
  83. pub id: String,
  84. pub name: String,
  85. pub desc: String,
  86. #[serde(rename = "field_type")]
  87. pub ty: FieldTypeRevision,
  88. pub frozen: bool,
  89. pub visibility: bool,
  90. pub width: i32,
  91. /// type_options contains key/value pairs
  92. /// key: id of the FieldType
  93. /// value: type-option data that can be parsed into specified TypeOptionStruct.
  94. ///
  95. /// For example, CheckboxTypeOption, MultiSelectTypeOption etc.
  96. #[serde(with = "indexmap::serde_seq")]
  97. pub type_options: IndexMap<String, String>,
  98. #[serde(default = "DEFAULT_IS_PRIMARY_VALUE")]
  99. pub is_primary: bool,
  100. }
  101. impl AsRef<FieldRevision> for FieldRevision {
  102. fn as_ref(&self) -> &FieldRevision {
  103. self
  104. }
  105. }
  106. const DEFAULT_IS_PRIMARY_VALUE: fn() -> bool = || false;
  107. impl FieldRevision {
  108. pub fn new<T: Into<FieldTypeRevision>>(
  109. name: &str,
  110. desc: &str,
  111. field_type: T,
  112. width: i32,
  113. is_primary: bool,
  114. ) -> Self {
  115. Self {
  116. id: gen_field_id(),
  117. name: name.to_string(),
  118. desc: desc.to_string(),
  119. ty: field_type.into(),
  120. frozen: false,
  121. visibility: true,
  122. width,
  123. type_options: Default::default(),
  124. is_primary,
  125. }
  126. }
  127. pub fn insert_type_option<T>(&mut self, type_option: &T)
  128. where
  129. T: TypeOptionDataSerializer + ?Sized,
  130. {
  131. let id = self.ty.to_string();
  132. self.type_options.insert(id, type_option.json_str());
  133. }
  134. pub fn get_type_option<T: TypeOptionDataDeserializer>(
  135. &self,
  136. field_type_rev: FieldTypeRevision,
  137. ) -> Option<T> {
  138. let id = field_type_rev.to_string();
  139. self.type_options.get(&id).map(|s| T::from_json_str(s))
  140. }
  141. pub fn insert_type_option_str(&mut self, field_type: &FieldTypeRevision, json_str: String) {
  142. let id = field_type.to_string();
  143. self.type_options.insert(id, json_str);
  144. }
  145. pub fn get_type_option_str<T: Into<FieldTypeRevision>>(&self, field_type: T) -> Option<&str> {
  146. let field_type_rev = field_type.into();
  147. let id = field_type_rev.to_string();
  148. self.type_options.get(&id).map(|s| s.as_str())
  149. }
  150. }
  151. /// The macro [impl_type_option] will implement the [TypeOptionDataSerializer] for the type that
  152. /// supports the serde trait and the TryInto<Bytes> trait.
  153. pub trait TypeOptionDataSerializer {
  154. fn json_str(&self) -> String;
  155. fn protobuf_bytes(&self) -> Bytes;
  156. }
  157. /// The macro [impl_type_option] will implement the [TypeOptionDataDeserializer] for the type that
  158. /// supports the serde trait and the TryFrom<Bytes> trait.
  159. pub trait TypeOptionDataDeserializer {
  160. fn from_json_str(s: &str) -> Self;
  161. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  162. }
  163. #[derive(Clone, Default, Deserialize, Serialize)]
  164. pub struct BuildDatabaseContext {
  165. pub field_revs: Vec<Arc<FieldRevision>>,
  166. pub block_metas: Vec<DatabaseBlockMetaRevision>,
  167. pub blocks: Vec<DatabaseBlockRevision>,
  168. pub layout_setting: LayoutSetting,
  169. // String in JSON format. It can be deserialized into [GridViewRevision]
  170. pub database_view_data: String,
  171. }
  172. impl BuildDatabaseContext {
  173. pub fn new() -> Self {
  174. Self::default()
  175. }
  176. }
  177. impl std::convert::From<BuildDatabaseContext> for Bytes {
  178. fn from(ctx: BuildDatabaseContext) -> Self {
  179. let bytes = serde_json::to_vec(&ctx).unwrap_or_else(|_| vec![]);
  180. Bytes::from(bytes)
  181. }
  182. }
  183. impl std::convert::TryFrom<Bytes> for BuildDatabaseContext {
  184. type Error = serde_json::Error;
  185. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  186. let ctx: BuildDatabaseContext = serde_json::from_slice(&bytes)?;
  187. Ok(ctx)
  188. }
  189. }
  190. pub type FieldTypeRevision = u8;
  191. #[derive(Debug, Clone, Serialize, Deserialize)]
  192. pub struct CalendarLayoutSetting {
  193. pub layout_ty: CalendarLayout,
  194. pub first_day_of_week: i32,
  195. pub show_weekends: bool,
  196. pub show_week_numbers: bool,
  197. pub layout_field_id: String,
  198. }
  199. impl CalendarLayoutSetting {
  200. pub fn new(layout_field_id: String) -> Self {
  201. CalendarLayoutSetting {
  202. layout_ty: CalendarLayout::default(),
  203. first_day_of_week: DEFAULT_FIRST_DAY_OF_WEEK,
  204. show_weekends: DEFAULT_SHOW_WEEKENDS,
  205. show_week_numbers: DEFAULT_SHOW_WEEK_NUMBERS,
  206. layout_field_id,
  207. }
  208. }
  209. }
  210. #[derive(Debug, Clone, Eq, PartialEq, Default, Serialize_repr, Deserialize_repr)]
  211. #[repr(u8)]
  212. pub enum CalendarLayout {
  213. #[default]
  214. MonthLayout = 0,
  215. WeekLayout = 1,
  216. DayLayout = 2,
  217. }
  218. pub const DEFAULT_FIRST_DAY_OF_WEEK: i32 = 0;
  219. pub const DEFAULT_SHOW_WEEKENDS: bool = true;
  220. pub const DEFAULT_SHOW_WEEK_NUMBERS: bool = true;