1
- use config:: Config ;
1
+ use config:: { Config , File } ;
2
+ use std:: path:: Path ;
2
3
3
4
/// Struct to hold the application configuration.
4
5
pub struct AppConfig {
@@ -8,7 +9,7 @@ pub struct AppConfig {
8
9
/// The baud rate for the serial port.
9
10
pub baud_rate : i64 ,
10
11
11
- //Should the GPS sample rate be increased to 10Hz
12
+ // Should the GPS sample rate be increased to 10Hz
12
13
pub set_gps_to_10hz : bool ,
13
14
14
15
/// The MQTT broker host address.
@@ -17,46 +18,62 @@ pub struct AppConfig {
17
18
/// The MQTT broker port number.
18
19
pub mqtt_port : i64 ,
19
20
20
- //The base topic of MQTT where data is pushed
21
+ // The base topic of MQTT where data is pushed
21
22
pub mqtt_base_topic : String ,
23
+
24
+ // Optional: Path to the configuration file
25
+ pub config_path : Option < String > ,
22
26
}
23
27
24
28
/// Load application configuration from a TOML file.
25
29
///
26
- /// This function reads the configuration settings from a TOML file named "settings.toml".
27
- /// It expects the following keys in the TOML file: "port_name", "baud_rate", "mqtt_host", and "mqtt_port".
30
+ /// This function reads the configuration settings from a TOML file.
28
31
///
29
- /// # Panics
30
- /// Panics if any of the required configuration keys are missing or if there is an error reading the configuration file.
32
+ /// # Arguments
33
+ /// - `config_path`: An optional path to the configuration file.
31
34
///
32
35
/// # Returns
33
- /// Returns an `AppConfig` struct containing the loaded configuration.
34
- pub fn load_configuration ( ) -> AppConfig {
35
- // Build a new Config object with a file source.
36
- let settings = Config :: builder ( )
37
- . add_source ( config:: File :: with_name ( "settings.toml" ) )
38
- . build ( )
39
- . unwrap ( ) ;
36
+ /// Returns a `Result` containing either the `AppConfig` struct with the loaded configuration or an error message.
37
+ pub fn load_configuration ( config_path : Option < & str > ) -> Result < AppConfig , String > {
38
+ // Create a default configuration
39
+ let mut settings = Config :: default ( ) ;
40
40
41
- // Create an AppConfig struct by extracting values from the configuration.
42
- AppConfig {
43
- port_name : settings
44
- . get_string ( "port_name" )
45
- . expect ( "Missing port_name in configuration" ) ,
46
- baud_rate : settings
47
- . get_int ( "baud_rate" )
48
- . expect ( "Missing baud_rate in configuration" ) ,
49
- set_gps_to_10hz : settings
50
- . get_bool ( "set_gps_to_10hz" )
51
- . expect ( "Missing set_gps_to_10hz in configuration" ) ,
52
- mqtt_host : settings
53
- . get_string ( "mqtt_host" )
54
- . expect ( "Missing mqtt_host in configuration" ) ,
55
- mqtt_port : settings
56
- . get_int ( "mqtt_port" )
57
- . expect ( "Missing mqtt_port in configuration" ) ,
58
- mqtt_base_topic : settings
59
- . get_string ( "mqtt_base_topic" )
60
- . expect ( "Missing mqtt_base_topic in configuration" ) ,
41
+ // Try to load from the passed config_path
42
+ if let Some ( path) = config_path {
43
+ match Config :: builder ( ) . add_source ( File :: with_name ( path) ) . build ( ) {
44
+ Ok ( config) => settings = config,
45
+ Err ( err) => return Err ( format ! ( "{}" , err) ) ,
46
+ }
47
+ } else {
48
+ // Try to load from the executable's directory
49
+ if let Ok ( exe_dir) = std:: env:: current_exe ( ) {
50
+ let exe_dir = exe_dir. parent ( ) . unwrap_or_else ( || Path :: new ( "." ) ) ;
51
+ let default_path = exe_dir. join ( "settings.toml" ) ;
52
+
53
+ if let Ok ( config) =
54
+ Config :: builder ( ) . add_source ( File :: with_name ( default_path. to_str ( ) . unwrap ( ) ) ) . build ( )
55
+ {
56
+ settings = config;
57
+ }
58
+ }
59
+
60
+ // Try to load from /etc/g86-car-telemetry/speeduino-to-mqtt.toml
61
+ if let Ok ( config) = Config :: builder ( )
62
+ . add_source ( File :: with_name ( "/usr/etc/g86-car-telemetry/gps-to-mqtt.toml" ) )
63
+ . build ( )
64
+ {
65
+ settings = config;
66
+ }
61
67
}
62
- }
68
+
69
+ // Create an AppConfig struct by extracting values from the configuration.
70
+ Ok ( AppConfig {
71
+ port_name : settings. get_string ( "port_name" ) . unwrap_or_else ( |_| "default_port" . to_string ( ) ) ,
72
+ baud_rate : settings. get_int ( "baud_rate" ) . unwrap_or ( 9600 ) ,
73
+ set_gps_to_10hz : settings. get_bool ( "set_gps_to_10hz" ) . unwrap_or ( false ) ,
74
+ mqtt_host : settings. get_string ( "mqtt_host" ) . unwrap_or_else ( |_| "default_host" . to_string ( ) ) ,
75
+ mqtt_port : settings. get_int ( "mqtt_port" ) . unwrap_or ( 1883 ) ,
76
+ mqtt_base_topic : settings. get_string ( "mqtt_base_topic" ) . unwrap_or_else ( |_| "default_topic" . to_string ( ) ) ,
77
+ config_path : config_path. map ( |p| p. to_string ( ) ) ,
78
+ } )
79
+ }
0 commit comments