group_rev.rs 5.0 KB

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