group_rev.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. use crate::revision::{gen_grid_group_id, FieldTypeRevision};
  2. use serde::{Deserialize, Serialize};
  3. use serde_json::Error;
  4. use serde_repr::*;
  5. pub trait GroupConfigurationContentSerde: Sized + Send + Sync {
  6. fn from_configuration_content(s: &str) -> Result<Self, serde_json::Error>;
  7. fn to_configuration_content(&self) -> Result<String, serde_json::Error>;
  8. }
  9. #[derive(Debug, Clone, Serialize, Deserialize, Default)]
  10. pub struct GroupConfigurationRevision {
  11. pub id: String,
  12. pub field_id: String,
  13. pub field_type_rev: FieldTypeRevision,
  14. pub groups: Vec<GroupRecordRevision>,
  15. pub content: String,
  16. }
  17. impl GroupConfigurationRevision {
  18. pub fn new<T>(field_id: String, field_type: FieldTypeRevision, content: T) -> Result<Self, serde_json::Error>
  19. where
  20. T: GroupConfigurationContentSerde,
  21. {
  22. let content = content.to_configuration_content()?;
  23. Ok(Self {
  24. id: gen_grid_group_id(),
  25. field_id,
  26. field_type_rev: field_type,
  27. groups: vec![],
  28. content,
  29. })
  30. }
  31. }
  32. #[derive(Default, Serialize, Deserialize)]
  33. pub struct TextGroupConfigurationRevision {
  34. pub hide_empty: bool,
  35. }
  36. impl GroupConfigurationContentSerde for TextGroupConfigurationRevision {
  37. fn from_configuration_content(s: &str) -> Result<Self, Error> {
  38. serde_json::from_str(s)
  39. }
  40. fn to_configuration_content(&self) -> Result<String, Error> {
  41. serde_json::to_string(self)
  42. }
  43. }
  44. #[derive(Default, Serialize, Deserialize)]
  45. pub struct NumberGroupConfigurationRevision {
  46. pub hide_empty: bool,
  47. }
  48. impl GroupConfigurationContentSerde for NumberGroupConfigurationRevision {
  49. fn from_configuration_content(s: &str) -> Result<Self, Error> {
  50. serde_json::from_str(s)
  51. }
  52. fn to_configuration_content(&self) -> Result<String, Error> {
  53. serde_json::to_string(self)
  54. }
  55. }
  56. #[derive(Default, Serialize, Deserialize)]
  57. pub struct UrlGroupConfigurationRevision {
  58. pub hide_empty: bool,
  59. }
  60. impl GroupConfigurationContentSerde for UrlGroupConfigurationRevision {
  61. fn from_configuration_content(s: &str) -> Result<Self, Error> {
  62. serde_json::from_str(s)
  63. }
  64. fn to_configuration_content(&self) -> Result<String, Error> {
  65. serde_json::to_string(self)
  66. }
  67. }
  68. #[derive(Default, Serialize, Deserialize)]
  69. pub struct CheckboxGroupConfigurationRevision {
  70. pub hide_empty: bool,
  71. }
  72. impl GroupConfigurationContentSerde for CheckboxGroupConfigurationRevision {
  73. fn from_configuration_content(s: &str) -> Result<Self, Error> {
  74. serde_json::from_str(s)
  75. }
  76. fn to_configuration_content(&self) -> Result<String, Error> {
  77. serde_json::to_string(self)
  78. }
  79. }
  80. #[derive(Default, Serialize, Deserialize)]
  81. pub struct SelectOptionGroupConfigurationRevision {
  82. pub hide_empty: bool,
  83. }
  84. impl GroupConfigurationContentSerde for SelectOptionGroupConfigurationRevision {
  85. fn from_configuration_content(s: &str) -> Result<Self, Error> {
  86. serde_json::from_str(s)
  87. }
  88. fn to_configuration_content(&self) -> Result<String, Error> {
  89. serde_json::to_string(self)
  90. }
  91. }
  92. #[derive(Clone, Debug, Default, Serialize, Deserialize)]
  93. pub struct GroupRecordRevision {
  94. pub group_id: String,
  95. #[serde(default = "DEFAULT_GROUP_RECORD_VISIBILITY")]
  96. pub visible: bool,
  97. }
  98. const DEFAULT_GROUP_RECORD_VISIBILITY: fn() -> bool = || true;
  99. impl GroupRecordRevision {
  100. pub fn new(group_id: String) -> Self {
  101. Self {
  102. group_id,
  103. visible: true,
  104. }
  105. }
  106. }
  107. #[derive(Default, Serialize, Deserialize)]
  108. pub struct DateGroupConfigurationRevision {
  109. pub hide_empty: bool,
  110. pub condition: DateCondition,
  111. }
  112. impl GroupConfigurationContentSerde for DateGroupConfigurationRevision {
  113. fn from_configuration_content(s: &str) -> Result<Self, Error> {
  114. serde_json::from_str(s)
  115. }
  116. fn to_configuration_content(&self) -> Result<String, Error> {
  117. serde_json::to_string(self)
  118. }
  119. }
  120. #[derive(Serialize_repr, Deserialize_repr)]
  121. #[repr(u8)]
  122. pub enum DateCondition {
  123. Relative = 0,
  124. Day = 1,
  125. Week = 2,
  126. Month = 3,
  127. Year = 4,
  128. }
  129. impl std::default::Default for DateCondition {
  130. fn default() -> Self {
  131. DateCondition::Relative
  132. }
  133. }
  134. #[cfg(test)]
  135. mod tests {
  136. use crate::revision::{GroupConfigurationRevision, SelectOptionGroupConfigurationRevision};
  137. #[test]
  138. fn group_configuration_serde_test() {
  139. let content = SelectOptionGroupConfigurationRevision { hide_empty: false };
  140. let rev = GroupConfigurationRevision::new("1".to_owned(), 2, content).unwrap();
  141. let json = serde_json::to_string(&rev).unwrap();
  142. let rev: GroupConfigurationRevision = serde_json::from_str(&json).unwrap();
  143. let _content: SelectOptionGroupConfigurationRevision = serde_json::from_str(&rev.content).unwrap();
  144. }
  145. #[test]
  146. fn group_configuration_serde_test2() {
  147. let content = SelectOptionGroupConfigurationRevision { hide_empty: false };
  148. let content_json = serde_json::to_string(&content).unwrap();
  149. let rev = GroupConfigurationRevision::new("1".to_owned(), 2, content).unwrap();
  150. assert_eq!(rev.content, content_json);
  151. }
  152. }