@@ -67,8 +67,8 @@ impl Configuration {
6767 section : Option < & ' static str > ,
6868 key : & ' static str ,
6969 ) -> Result < T , ConfigurationError > {
70- if self . custom_settings . is_some ( ) {
71- match get_optional ( section, key, self . custom_settings . as_ref ( ) . unwrap ( ) ) {
70+ if let Some ( custom_settings ) = & self . custom_settings {
71+ match get_optional ( section, key, custom_settings) {
7272 Ok ( result) => match result {
7373 Some ( value) => Ok ( value) ,
7474 _ => Err ( FieldNotFound ( key) ) ,
@@ -81,14 +81,8 @@ impl Configuration {
8181 }
8282
8383 pub fn set < T : Into < String > > ( & mut self , section : Option < & str > , key : & str , value : T ) {
84- if self . custom_settings . is_none ( ) {
85- self . custom_settings = Some ( Ini :: default ( ) )
86- }
87- self . custom_settings
88- . as_mut ( )
89- . unwrap ( )
90- . with_section ( section)
91- . set ( key, value) ;
84+ let custom_settings = self . custom_settings . get_or_insert_with ( Ini :: default) ;
85+ custom_settings. with_section ( section) . set ( key, value) ;
9286 }
9387
9488 /// Get a list of values from configuration, separated by commas
@@ -97,8 +91,8 @@ impl Configuration {
9791 section : Option < & ' static str > ,
9892 key : & ' static str ,
9993 ) -> Result < Vec < T > , ConfigurationError > {
100- if self . custom_settings . is_some ( ) {
101- match get_optional_list ( section, key, self . custom_settings . as_ref ( ) . unwrap ( ) ) {
94+ if let Some ( custom_settings ) = & self . custom_settings {
95+ match get_optional_list ( section, key, custom_settings) {
10296 Ok ( result) => match result {
10397 Some ( values) => Ok ( values) ,
10498 None => Ok ( Vec :: new ( ) ) , // Return empty vec if not found
@@ -611,8 +605,10 @@ use_tls = false
611605
612606 #[ test]
613607 fn get_list_no_custom_settings_error ( ) {
614- let mut configuration = Configuration :: default ( ) ;
615- configuration. custom_settings = None ;
608+ let configuration = Configuration {
609+ custom_settings : None ,
610+ ..Default :: default ( )
611+ } ;
616612 let result = configuration. get_list :: < String > ( Some ( "test" ) , "list_field" ) ;
617613 assert ! ( matches!( result, Err ( NoCustomSettings ) ) ) ;
618614 }
@@ -648,7 +644,7 @@ use_tls = false
648644 // Tests for get_optional_list functionality
649645 #[ test]
650646 fn get_optional_list_ok ( ) {
651- let mut properties = ini :: Properties :: new ( ) ;
647+ let mut properties = Properties :: new ( ) ;
652648 properties. insert ( "test_list" , "a,b,c" . to_string ( ) ) ;
653649 let result = get_optional_list_from_properties :: < String > ( "test_list" , & properties) . unwrap ( ) ;
654650 assert_eq ! (
@@ -659,14 +655,14 @@ use_tls = false
659655
660656 #[ test]
661657 fn get_optional_list_missing_field ( ) {
662- let properties = ini :: Properties :: new ( ) ;
658+ let properties = Properties :: new ( ) ;
663659 let result = get_optional_list_from_properties :: < String > ( "missing" , & properties) . unwrap ( ) ;
664660 assert ! ( result. is_none( ) ) ;
665661 }
666662
667663 #[ test]
668664 fn get_optional_list_type_error ( ) {
669- let mut properties = ini :: Properties :: new ( ) ;
665+ let mut properties = Properties :: new ( ) ;
670666 properties. insert ( "test_list" , "not_a_number,123" . to_string ( ) ) ;
671667 let result = get_optional_list_from_properties :: < u32 > ( "test_list" , & properties) ;
672668 assert ! ( result. is_err( ) ) ;
0 commit comments