1- use serde:: { Serialize , Deserialize } ;
1+ use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
22use std:: path:: { Path , PathBuf } ;
33use std:: fs;
4-
4+ use std:: sync:: Mutex ;
5+ use parking_lot:: RwLock ;
56
67
78#[ derive( Serialize , Deserialize , Debug , Clone ) ]
@@ -33,17 +34,43 @@ impl StrategyText{
3334}
3435
3536#[ derive( Serialize , Deserialize , Debug , Clone ) ]
36- pub struct Strategy {
37+ pub struct StrategyMessage {
3738 pub strategy_text : StrategyText ,
3839 pub answer : String ,
39- // event: Event,
4040
4141}
4242
4343#[ derive( Serialize , Deserialize , Debug ) ]
44+ pub struct StrategyOrder {
45+ pub unique_prefix : String ,
46+ pub static_data : Option < String > ,
47+ pub availability_data : RwLock < Option < Vec < String > > > ,
48+ }
49+ impl StrategyOrder {
50+ pub fn get_availability ( & self ) -> Option < String > {
51+ let mut data_guard = self . availability_data . write ( ) ;
52+ if let Some ( data) = data_guard. as_mut ( ) {
53+ if !data. is_empty ( ) {
54+ let response = data. remove ( 0 ) ;
55+ if data. is_empty ( ) {
56+ * data_guard = None ;
57+ }
58+ return Some ( response) ;
59+ }
60+ }
61+ None
62+ }
63+ }
64+ #[ derive( Serialize , Deserialize ) ]
4465pub struct Strategies {
45- pub strategies : Vec < Strategy > ,
66+ pub message : Vec < StrategyMessage > ,
67+ pub order : Vec < StrategyOrder > ,
68+ #[ serde( skip) ]
69+ path_config : PathBuf ,
4670}
71+
72+
73+
4774impl Strategies {
4875 pub fn new ( path_config : Option < String > ) -> Result < Self , Box < dyn std:: error:: Error > > {
4976 let path = match path_config {
@@ -54,13 +81,16 @@ impl Strategies {
5481 return Err ( format ! ( "File config no find {}" , path. display( ) ) . into ( ) ) ;
5582 }
5683 let text = fs:: read_to_string ( & path) ?;
57-
58- let config: Strategies = serde_json:: from_str ( & text) ?;
59-
84+ let mut config: Strategies = serde_json:: from_str ( & text) ?;
85+ config. path_config =path;
6086 Ok ( config)
6187 }
62- fn reload ( ) {
63-
88+ fn reload ( & self ) {
89+ }
90+
91+ pub fn save ( & self ) {
92+ let config_json = serde_json:: to_string_pretty ( self ) . unwrap ( ) ;
93+ fs:: write ( & self . path_config , config_json) . unwrap ( ) ;
6494 }
6595
6696}
0 commit comments