@@ -151,12 +151,14 @@ impl Default for AppConfiguration {
151151#[ derive( Debug ) ]
152152pub struct Configuration {
153153 pub settings : RwLock < AppConfiguration > ,
154+ pub config_path : Option < String > ,
154155}
155156
156157impl Default for Configuration {
157158 fn default ( ) -> Self {
158159 Self {
159160 settings : RwLock :: new ( AppConfiguration :: default ( ) ) ,
161+ config_path : None ,
160162 }
161163 }
162164}
@@ -188,6 +190,7 @@ impl Configuration {
188190
189191 Ok ( Configuration {
190192 settings : RwLock :: new ( torrust_config) ,
193+ config_path : Some ( config_path. to_string ( ) ) ,
191194 } )
192195 }
193196
@@ -207,6 +210,7 @@ impl Configuration {
207210 let torrust_config: AppConfiguration = config_builder. try_deserialize ( ) ?;
208211 Ok ( Configuration {
209212 settings : RwLock :: new ( torrust_config) ,
213+ config_path : None ,
210214 } )
211215 }
212216 Err ( _) => Err ( ConfigError :: Message (
@@ -215,26 +219,36 @@ impl Configuration {
215219 }
216220 }
217221
218- pub async fn save_to_file ( & self , config_path : & str ) -> Result < ( ) , ( ) > {
222+ pub async fn save_to_file ( & self , config_path : & str ) {
219223 let settings = self . settings . read ( ) . await ;
220224
221225 let toml_string = toml:: to_string ( & * settings) . expect ( "Could not encode TOML value" ) ;
222226
223227 drop ( settings) ;
224228
225229 fs:: write ( config_path, toml_string) . expect ( "Could not write to file!" ) ;
226- Ok ( ( ) )
227230 }
228231
229- pub async fn update_settings ( & self , new_settings : AppConfiguration , config_path : & str ) -> Result < ( ) , ( ) > {
230- let mut settings = self . settings . write ( ) . await ;
231- * settings = new_settings;
232-
233- drop ( settings) ;
232+ /// Updates the settings and saves them to the configuration file.
233+ ///
234+ /// # Panics
235+ ///
236+ /// Will panic if the configuration file path is not defined. That happens
237+ /// when the configuration was loaded from the environment variable.
238+ pub async fn update_settings ( & self , new_settings : AppConfiguration ) {
239+ match & self . config_path {
240+ Some ( config_path) => {
241+ let mut settings = self . settings . write ( ) . await ;
242+ * settings = new_settings;
234243
235- let _ = self . save_to_file ( config_path ) . await ;
244+ drop ( settings ) ;
236245
237- Ok ( ( ) )
246+ let _ = self . save_to_file ( config_path) . await ;
247+ }
248+ None => panic ! (
249+ "Cannot update settings when the config file path is not defined. For example: when it's loaded from env var."
250+ ) ,
251+ }
238252 }
239253
240254 pub async fn get_public ( & self ) -> ConfigurationPublic {
0 commit comments