1- use std:: collections:: HashMap ;
1+ use std:: { collections:: HashMap , sync :: Arc } ;
22
33use collab:: preclude:: { Any , ArrayRef } ;
4+ use serde:: { Deserialize , Serialize } ;
5+ use yrs:: encoding:: serde:: { from_any, to_any} ;
6+
7+ use crate :: database:: gen_database_group_id;
48
59/// [GroupSettingArray] contains list of [GroupSettingMap]
610pub type GroupSettingArray = Vec < Any > ;
@@ -15,3 +19,86 @@ pub type GroupSettingBuilder = HashMap<String, Any>;
1519pub type GroupMap = HashMap < String , Any > ;
1620/// [GroupMapBuilder] is the builder for [GroupMap]
1721pub type GroupMapBuilder = HashMap < String , Any > ;
22+
23+ #[ derive( Debug , Clone , Default , Deserialize ) ]
24+ pub struct GroupSetting {
25+ pub id : String ,
26+ pub field_id : String ,
27+ #[ serde( rename = "ty" ) ]
28+ pub field_type : i64 ,
29+ #[ serde( default ) ]
30+ pub groups : Vec < Group > ,
31+ #[ serde( default ) ]
32+ pub content : String ,
33+ }
34+
35+ impl GroupSetting {
36+ pub fn new ( field_id : String , field_type : i64 , content : String ) -> Self {
37+ Self {
38+ id : gen_database_group_id ( ) ,
39+ field_id,
40+ field_type,
41+ groups : vec ! [ ] ,
42+ content,
43+ }
44+ }
45+ }
46+
47+ const GROUP_ID : & str = "id" ;
48+ const FIELD_ID : & str = "field_id" ;
49+ const FIELD_TYPE : & str = "ty" ;
50+ const GROUPS : & str = "groups" ;
51+ const CONTENT : & str = "content" ;
52+
53+ impl TryFrom < GroupSettingMap > for GroupSetting {
54+ type Error = anyhow:: Error ;
55+
56+ fn try_from ( value : GroupSettingMap ) -> Result < Self , Self :: Error > {
57+ from_any ( & Any :: from ( value) ) . map_err ( |e| e. into ( ) )
58+ }
59+ }
60+
61+ impl From < GroupSetting > for GroupSettingMap {
62+ fn from ( setting : GroupSetting ) -> Self {
63+ let groups = to_any ( & setting. groups ) . unwrap_or_else ( |_| Any :: Array ( Arc :: from ( [ ] ) ) ) ;
64+ GroupSettingBuilder :: from ( [
65+ ( GROUP_ID . into ( ) , setting. id . into ( ) ) ,
66+ ( FIELD_ID . into ( ) , setting. field_id . into ( ) ) ,
67+ ( FIELD_TYPE . into ( ) , Any :: BigInt ( setting. field_type ) ) ,
68+ ( GROUPS . into ( ) , groups) ,
69+ ( CONTENT . into ( ) , setting. content . into ( ) ) ,
70+ ] )
71+ }
72+ }
73+
74+ #[ derive( Debug , Clone , Serialize , Deserialize , Default ) ]
75+ pub struct Group {
76+ pub id : String ,
77+ #[ serde( default = "GROUP_VISIBILITY" ) ]
78+ pub visible : bool ,
79+ }
80+
81+ impl TryFrom < GroupMap > for Group {
82+ type Error = anyhow:: Error ;
83+
84+ fn try_from ( value : GroupMap ) -> Result < Self , Self :: Error > {
85+ from_any ( & Any :: from ( value) ) . map_err ( |e| e. into ( ) )
86+ }
87+ }
88+
89+ impl From < Group > for GroupMap {
90+ fn from ( group : Group ) -> Self {
91+ GroupMapBuilder :: from ( [
92+ ( "id" . into ( ) , group. id . into ( ) ) ,
93+ ( "visible" . into ( ) , group. visible . into ( ) ) ,
94+ ] )
95+ }
96+ }
97+
98+ const GROUP_VISIBILITY : fn ( ) -> bool = || true ;
99+
100+ impl Group {
101+ pub fn new ( id : String ) -> Self {
102+ Self { id, visible : true }
103+ }
104+ }
0 commit comments