group_rev.rs 5.0 KB

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