view_rev.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. use crate::{FilterConfiguration, GroupConfiguration, SortConfiguration};
  2. use indexmap::IndexMap;
  3. use nanoid::nanoid;
  4. use serde::{Deserialize, Serialize};
  5. use serde_repr::*;
  6. #[allow(dead_code)]
  7. pub fn gen_grid_view_id() -> String {
  8. nanoid!(6)
  9. }
  10. #[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)]
  11. #[repr(u8)]
  12. pub enum LayoutRevision {
  13. Grid = 0,
  14. Board = 1,
  15. Calendar = 2,
  16. }
  17. impl ToString for LayoutRevision {
  18. fn to_string(&self) -> String {
  19. let layout_rev = self.clone() as u8;
  20. layout_rev.to_string()
  21. }
  22. }
  23. impl std::default::Default for LayoutRevision {
  24. fn default() -> Self {
  25. LayoutRevision::Grid
  26. }
  27. }
  28. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  29. pub struct DatabaseViewRevision {
  30. pub view_id: String,
  31. #[serde(rename = "grid_id")]
  32. pub database_id: String,
  33. #[serde(default)]
  34. pub name: String,
  35. #[serde(default = "DEFAULT_BASE_VALUE")]
  36. pub is_base: bool,
  37. pub layout: LayoutRevision,
  38. #[serde(default)]
  39. pub layout_settings: LayoutSetting,
  40. #[serde(default)]
  41. pub filters: FilterConfiguration,
  42. #[serde(default)]
  43. pub groups: GroupConfiguration,
  44. #[serde(default)]
  45. pub sorts: SortConfiguration,
  46. }
  47. const DEFAULT_BASE_VALUE: fn() -> bool = || true;
  48. impl DatabaseViewRevision {
  49. pub fn new(
  50. database_id: String,
  51. view_id: String,
  52. is_base: bool,
  53. name: String,
  54. layout: LayoutRevision,
  55. ) -> Self {
  56. DatabaseViewRevision {
  57. database_id,
  58. view_id,
  59. layout,
  60. is_base,
  61. name,
  62. layout_settings: Default::default(),
  63. filters: Default::default(),
  64. groups: Default::default(),
  65. sorts: Default::default(),
  66. }
  67. }
  68. pub fn from_json(json: String) -> Result<Self, serde_json::Error> {
  69. serde_json::from_str(&json)
  70. }
  71. }
  72. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  73. #[serde(transparent)]
  74. pub struct LayoutSetting {
  75. #[serde(with = "indexmap::serde_seq")]
  76. inner: IndexMap<LayoutRevision, String>,
  77. }
  78. impl LayoutSetting {
  79. pub fn new() -> Self {
  80. Self {
  81. inner: Default::default(),
  82. }
  83. }
  84. pub fn is_empty(&self) -> bool {
  85. self.inner.is_empty()
  86. }
  87. }
  88. impl std::ops::Deref for LayoutSetting {
  89. type Target = IndexMap<LayoutRevision, String>;
  90. fn deref(&self) -> &Self::Target {
  91. &self.inner
  92. }
  93. }
  94. impl std::ops::DerefMut for LayoutSetting {
  95. fn deref_mut(&mut self) -> &mut Self::Target {
  96. &mut self.inner
  97. }
  98. }
  99. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  100. pub struct RowOrderRevision {
  101. pub row_id: String,
  102. }