123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- use crate::revision::{gen_grid_group_id, FieldTypeRevision};
- use serde::{Deserialize, Serialize};
- use serde_json::Error;
- use serde_repr::*;
- pub trait GroupConfigurationContentSerde: Sized + Send + Sync {
- fn from_configuration_content(s: &str) -> Result<Self, serde_json::Error>;
- fn to_configuration_content(&self) -> Result<String, serde_json::Error>;
- }
- #[derive(Debug, Clone, Serialize, Deserialize, Default)]
- pub struct GroupConfigurationRevision {
- pub id: String,
- pub field_id: String,
- pub field_type_rev: FieldTypeRevision,
- pub groups: Vec<GroupRecordRevision>,
- pub content: String,
- }
- impl GroupConfigurationRevision {
- pub fn new<T>(field_id: String, field_type: FieldTypeRevision, content: T) -> Result<Self, serde_json::Error>
- where
- T: GroupConfigurationContentSerde,
- {
- let content = content.to_configuration_content()?;
- Ok(Self {
- id: gen_grid_group_id(),
- field_id,
- field_type_rev: field_type,
- groups: vec![],
- content,
- })
- }
- }
- #[derive(Default, Serialize, Deserialize)]
- pub struct TextGroupConfigurationRevision {
- pub hide_empty: bool,
- }
- impl GroupConfigurationContentSerde for TextGroupConfigurationRevision {
- fn from_configuration_content(s: &str) -> Result<Self, Error> {
- serde_json::from_str(s)
- }
- fn to_configuration_content(&self) -> Result<String, Error> {
- serde_json::to_string(self)
- }
- }
- #[derive(Default, Serialize, Deserialize)]
- pub struct NumberGroupConfigurationRevision {
- pub hide_empty: bool,
- }
- impl GroupConfigurationContentSerde for NumberGroupConfigurationRevision {
- fn from_configuration_content(s: &str) -> Result<Self, Error> {
- serde_json::from_str(s)
- }
- fn to_configuration_content(&self) -> Result<String, Error> {
- serde_json::to_string(self)
- }
- }
- #[derive(Default, Serialize, Deserialize)]
- pub struct UrlGroupConfigurationRevision {
- pub hide_empty: bool,
- }
- impl GroupConfigurationContentSerde for UrlGroupConfigurationRevision {
- fn from_configuration_content(s: &str) -> Result<Self, Error> {
- serde_json::from_str(s)
- }
- fn to_configuration_content(&self) -> Result<String, Error> {
- serde_json::to_string(self)
- }
- }
- #[derive(Default, Serialize, Deserialize)]
- pub struct CheckboxGroupConfigurationRevision {
- pub hide_empty: bool,
- }
- impl GroupConfigurationContentSerde for CheckboxGroupConfigurationRevision {
- fn from_configuration_content(s: &str) -> Result<Self, Error> {
- serde_json::from_str(s)
- }
- fn to_configuration_content(&self) -> Result<String, Error> {
- serde_json::to_string(self)
- }
- }
- #[derive(Default, Serialize, Deserialize)]
- pub struct SelectOptionGroupConfigurationRevision {
- pub hide_empty: bool,
- }
- impl GroupConfigurationContentSerde for SelectOptionGroupConfigurationRevision {
- fn from_configuration_content(s: &str) -> Result<Self, Error> {
- serde_json::from_str(s)
- }
- fn to_configuration_content(&self) -> Result<String, Error> {
- serde_json::to_string(self)
- }
- }
- #[derive(Clone, Debug, Default, Serialize, Deserialize)]
- pub struct GroupRecordRevision {
- pub group_id: String,
- #[serde(default = "DEFAULT_GROUP_RECORD_VISIBILITY")]
- pub visible: bool,
- }
- const DEFAULT_GROUP_RECORD_VISIBILITY: fn() -> bool = || true;
- impl GroupRecordRevision {
- pub fn new(group_id: String) -> Self {
- Self {
- group_id,
- visible: true,
- }
- }
- }
- #[derive(Default, Serialize, Deserialize)]
- pub struct DateGroupConfigurationRevision {
- pub hide_empty: bool,
- pub condition: DateCondition,
- }
- impl GroupConfigurationContentSerde for DateGroupConfigurationRevision {
- fn from_configuration_content(s: &str) -> Result<Self, Error> {
- serde_json::from_str(s)
- }
- fn to_configuration_content(&self) -> Result<String, Error> {
- serde_json::to_string(self)
- }
- }
- #[derive(Serialize_repr, Deserialize_repr)]
- #[repr(u8)]
- pub enum DateCondition {
- Relative = 0,
- Day = 1,
- Week = 2,
- Month = 3,
- Year = 4,
- }
- impl std::default::Default for DateCondition {
- fn default() -> Self {
- DateCondition::Relative
- }
- }
- #[cfg(test)]
- mod tests {
- use crate::revision::{GroupConfigurationRevision, SelectOptionGroupConfigurationRevision};
- #[test]
- fn group_configuration_serde_test() {
- let content = SelectOptionGroupConfigurationRevision { hide_empty: false };
- let rev = GroupConfigurationRevision::new("1".to_owned(), 2, content).unwrap();
- let json = serde_json::to_string(&rev).unwrap();
- let rev: GroupConfigurationRevision = serde_json::from_str(&json).unwrap();
- let _content: SelectOptionGroupConfigurationRevision = serde_json::from_str(&rev.content).unwrap();
- }
- #[test]
- fn group_configuration_serde_test2() {
- let content = SelectOptionGroupConfigurationRevision { hide_empty: false };
- let content_json = serde_json::to_string(&content).unwrap();
- let rev = GroupConfigurationRevision::new("1".to_owned(), 2, content).unwrap();
- assert_eq!(rev.content, content_json);
- }
- }
|