55//! config file. See the
66//! [example project's config file](https://github.com/Validus-Risk-Management/hotfix/blob/main/examples/simple-new-order/config/test-config.toml)
77//! for more detail.
8+ use chrono:: { NaiveTime , Weekday } ;
89use serde:: Deserialize ;
910use std:: fs;
1011use std:: path:: Path ;
@@ -30,6 +31,17 @@ pub struct TlsConfig {
3031 pub ca_certificate_path : String ,
3132}
3233
34+ /// Session schedule configuration
35+ #[ derive( Clone , Debug , Deserialize , PartialEq ) ]
36+ pub struct ScheduleConfig {
37+ pub start_time : Option < NaiveTime > ,
38+ pub end_time : Option < NaiveTime > ,
39+ pub start_day : Option < Weekday > ,
40+ pub end_day : Option < Weekday > ,
41+ #[ serde( default ) ]
42+ pub weekdays : Vec < Weekday > ,
43+ }
44+
3345fn default_reconnect_interval ( ) -> u64 {
3446 30
3547}
@@ -61,12 +73,16 @@ pub struct SessionConfig {
6173 /// The interval we should attempt to reconnect at in seconds.
6274 pub reconnect_interval : u64 ,
6375 /// Specifies whether we should reset the state of the message store on logon.
76+ #[ serde( default ) ]
6477 pub reset_on_logon : bool ,
78+ /// The schedule configuration for the session
79+ pub schedule : Option < ScheduleConfig > ,
6580}
6681
6782#[ cfg( test) ]
6883mod tests {
6984 use crate :: config:: { Config , TlsConfig } ;
85+ use chrono:: { NaiveTime , Weekday } ;
7086
7187 #[ test]
7288 fn test_simple_config ( ) {
@@ -104,4 +120,77 @@ reset_on_logon = false
104120 assert_eq ! ( session_config. tls_config, Some ( expected_tls_config) ) ;
105121 assert_eq ! ( session_config. reconnect_interval, 30 ) ;
106122 }
123+
124+ #[ test]
125+ fn test_schedule_config_weekdays ( ) {
126+ let config_contents = r#"
127+ [[sessions]]
128+ begin_string = "FIX.4.4"
129+ sender_comp_id = "send-comp-id"
130+ target_comp_id = "target-comp-id"
131+ heartbeat_interval = 30
132+
133+ connection_port = 443
134+ connection_host = "127.0.0.1"
135+
136+ [sessions.schedule]
137+ start_time = "00:00:00"
138+ end_time = "23:55:00"
139+ weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
140+ "# ;
141+
142+ let config: Config = toml:: from_str ( config_contents) . unwrap ( ) ;
143+ assert_eq ! ( config. sessions. len( ) , 1 ) ;
144+ let session = config. sessions . first ( ) . unwrap ( ) ;
145+
146+ assert_eq ! ( session. schedule. is_some( ) , true ) ;
147+ let schedule = session. schedule . as_ref ( ) . unwrap ( ) ;
148+
149+ assert_eq ! ( schedule. start_time, NaiveTime :: from_hms_opt( 0 , 0 , 0 ) ) ;
150+ assert_eq ! ( schedule. end_time, NaiveTime :: from_hms_opt( 23 , 55 , 0 ) ) ;
151+ assert_eq ! (
152+ schedule. weekdays,
153+ vec![
154+ Weekday :: Mon ,
155+ Weekday :: Tue ,
156+ Weekday :: Wed ,
157+ Weekday :: Thu ,
158+ Weekday :: Fri
159+ ]
160+ ) ;
161+ assert_eq ! ( schedule. start_day, None ) ;
162+ assert_eq ! ( schedule. end_day, None ) ;
163+ }
164+
165+ #[ test]
166+ fn test_schedule_config_weeklong_session ( ) {
167+ let config_contents = r#"
168+ [[sessions]]
169+ begin_string = "FIX.4.4"
170+ sender_comp_id = "send-comp-id"
171+ target_comp_id = "target-comp-id"
172+ heartbeat_interval = 30
173+
174+ connection_port = 443
175+ connection_host = "127.0.0.1"
176+
177+ [sessions.schedule]
178+ start_time = "00:00:00"
179+ end_time = "23:55:00"
180+ start_day = "Monday"
181+ end_day = "Friday"
182+ "# ;
183+
184+ let config: Config = toml:: from_str ( config_contents) . unwrap ( ) ;
185+ assert_eq ! ( config. sessions. len( ) , 1 ) ;
186+ let session = config. sessions . first ( ) . unwrap ( ) ;
187+
188+ assert_eq ! ( session. schedule. is_some( ) , true ) ;
189+ let schedule = session. schedule . as_ref ( ) . unwrap ( ) ;
190+
191+ assert_eq ! ( schedule. start_time, NaiveTime :: from_hms_opt( 0 , 0 , 0 ) ) ;
192+ assert_eq ! ( schedule. end_time, NaiveTime :: from_hms_opt( 23 , 55 , 0 ) ) ;
193+ assert_eq ! ( schedule. start_day, Some ( Weekday :: Mon ) ) ;
194+ assert_eq ! ( schedule. end_day, Some ( Weekday :: Fri ) ) ;
195+ }
107196}
0 commit comments