block_rev.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. use indexmap::IndexMap;
  2. use nanoid::nanoid;
  3. use serde::{Deserialize, Serialize};
  4. use std::collections::HashMap;
  5. use std::sync::Arc;
  6. pub fn gen_row_id() -> String {
  7. nanoid!(6)
  8. }
  9. pub const DEFAULT_ROW_HEIGHT: i32 = 42;
  10. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  11. pub struct DatabaseBlockRevision {
  12. pub block_id: String,
  13. pub rows: Vec<Arc<RowRevision>>,
  14. }
  15. pub type FieldId = String;
  16. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  17. pub struct RowRevision {
  18. pub id: String,
  19. pub block_id: String,
  20. /// cells contains key/value pairs.
  21. /// key: field id,
  22. /// value: CellMeta
  23. #[serde(with = "indexmap::serde_seq")]
  24. pub cells: IndexMap<FieldId, CellRevision>,
  25. pub height: i32,
  26. pub visibility: bool,
  27. }
  28. impl RowRevision {
  29. pub fn new(block_id: &str) -> Self {
  30. Self {
  31. id: gen_row_id(),
  32. block_id: block_id.to_owned(),
  33. cells: Default::default(),
  34. height: DEFAULT_ROW_HEIGHT,
  35. visibility: true,
  36. }
  37. }
  38. }
  39. #[derive(Debug, Clone, Default)]
  40. pub struct RowChangeset {
  41. pub row_id: String,
  42. pub height: Option<i32>,
  43. pub visibility: Option<bool>,
  44. // Contains the key/value changes represents as the update of the cells. For example,
  45. // if there is one cell was changed, then the `cell_by_field_id` will only have one key/value.
  46. pub cell_by_field_id: HashMap<FieldId, CellRevision>,
  47. }
  48. impl RowChangeset {
  49. pub fn new(row_id: String) -> Self {
  50. Self {
  51. row_id,
  52. height: None,
  53. visibility: None,
  54. cell_by_field_id: Default::default(),
  55. }
  56. }
  57. pub fn is_empty(&self) -> bool {
  58. self.height.is_none() && self.visibility.is_none() && self.cell_by_field_id.is_empty()
  59. }
  60. }
  61. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  62. pub struct CellRevision {
  63. #[serde(rename = "data")]
  64. pub type_cell_data: String,
  65. }
  66. impl CellRevision {
  67. pub fn new(data: String) -> Self {
  68. Self {
  69. type_cell_data: data,
  70. }
  71. }
  72. pub fn is_empty(&self) -> bool {
  73. self.type_cell_data.is_empty()
  74. }
  75. }