From 714d02dc309e20661fc3768890f2f288b2969288 Mon Sep 17 00:00:00 2001 From: Gray Logan Date: Fri, 14 Mar 2025 11:16:24 -0400 Subject: [PATCH] Remove FnOnce requirement --- Cargo.toml | 2 +- examples/from_section.rs | 35 +++++++++++++------------- examples/get_values.rs | 27 ++++++++++---------- examples/load_file.rs | 17 +++++++------ src/config.rs | 4 +-- tests/config_tests.rs | 54 +++++++++++++++++++++------------------- tests/derive_tests.rs | 34 +++++++++++++------------ 7 files changed, 90 insertions(+), 83 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 87223ea..b78d268 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "config-tools" -version = "0.3.2" +version = "0.3.3" edition = "2021" authors = ["Gray Logan "] description = "A simplified set of tools for working with configuration files." diff --git a/examples/from_section.rs b/examples/from_section.rs index f7d3073..f2ca878 100644 --- a/examples/from_section.rs +++ b/examples/from_section.rs @@ -15,23 +15,24 @@ struct LdapSettings { } fn main() { - let config = Config::load_or_default("get-values.ini", || { - return sectioned_defaults! { - { - "console" => "true", - "log_level" => "info", - } - ["Server"] { - "address" => "127.0.0.1", - "port" => "8080", - "threads" => "4", - } - ["LDAP"] { - "host" => "ldap://localhost:389", - "domain" => "example.com", - } - } - }); + let config = Config::load_or_default( + "get-values.ini", + sectioned_defaults! { + { + "console" => "true", + "log_level" => "info", + } + ["Server"] { + "address" => "127.0.0.1", + "port" => "8080", + "threads" => "4", + } + ["LDAP"] { + "host" => "ldap://localhost:389", + "domain" => "example.com", + } + }, + ); let ldap_settings = LdapSettings::from_section(&config.section("LDAP").unwrap()).unwrap(); let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap(); diff --git a/examples/get_values.rs b/examples/get_values.rs index 684d34a..f6194bc 100644 --- a/examples/get_values.rs +++ b/examples/get_values.rs @@ -8,19 +8,20 @@ struct ServerSettings { } fn main() { - let config = Config::load_or_default("get-values.ini", || { - return sectioned_defaults! { - { - "console" => "true", - "log_level" => "info", - } - ["Server"] { - "address" => "127.0.0.1", - "port" => "8080", - "threads" => "4", - } - } - }); + let config = Config::load_or_default( + "get-values.ini", + sectioned_defaults! { + { + "console" => "true", + "log_level" => "info", + } + ["Server"] { + "address" => "127.0.0.1", + "port" => "8080", + "threads" => "4", + } + }, + ); let console = config.get_as::(None, "console").unwrap(); let log_level = config.get(None, "log_level").unwrap(); diff --git a/examples/load_file.rs b/examples/load_file.rs index df08fda..64858a3 100644 --- a/examples/load_file.rs +++ b/examples/load_file.rs @@ -8,14 +8,15 @@ fn main() { // let config = Config::load(filename); // Load and use defaults on failure - let config = Config::load_or_default(filename, || { - return sectioned_defaults! { - { - "host" => "127.0.0.1", - "port" => "8080", - } - } - }); + let config = Config::load_or_default( + filename, + sectioned_defaults! { + { + "host" => "127.0.0.1", + "port" => "8080", + } + }, + ); config.save(filename).expect("Failed to save config."); diff --git a/src/config.rs b/src/config.rs index 336975b..05e8315 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,10 +58,10 @@ impl Config { }) } - pub fn load_or_default Config>(filename: &str, default: F) -> Self { + pub fn load_or_default(filename: &str, default: Config) -> Self { match Self::load(filename) { Ok(config) => config, - Err(_) => default(), + Err(_) => default, } } diff --git a/tests/config_tests.rs b/tests/config_tests.rs index 1b30259..8a987f8 100644 --- a/tests/config_tests.rs +++ b/tests/config_tests.rs @@ -53,19 +53,20 @@ fn test_config_builder_update() { #[test] fn test_default_config_loading() { use config_tools::sectioned_defaults; - let config = Config::load_or_default("nonexistent.ini", || { - return sectioned_defaults! { - { - "console" => "true", - "log_level" => "info", - } - ["Server"] { - "address" => "127.0.0.1", - "port" => "8080", - "threads" => "4", - } - } - }); + let config = Config::load_or_default( + "nonexistent.ini", + sectioned_defaults! { + { + "console" => "true", + "log_level" => "info", + } + ["Server"] { + "address" => "127.0.0.1", + "port" => "8080", + "threads" => "4", + } + }, + ); let console = config.get_as::(None, "console").unwrap(); let log_level = config.get(None, "log_level").unwrap(); @@ -83,19 +84,20 @@ fn test_default_config_loading() { #[test] fn test_default_config_loading_with_missing_keys() { use config_tools::sectioned_defaults; - let config = Config::load_or_default("nonexistent.ini", || { - return sectioned_defaults! { - { - "console" => "true", - "log_level" => "info", - } - ["Server"] { - "address" => "127.0.0.1", - "port" => "8080", - "threads" => "4", - } - } - }); + let config = Config::load_or_default( + "nonexistent.ini", + sectioned_defaults! { + { + "console" => "true", + "log_level" => "info", + } + ["Server"] { + "address" => "127.0.0.1", + "port" => "8080", + "threads" => "4", + } + }, + ); // Try to access non-existent key assert!( diff --git a/tests/derive_tests.rs b/tests/derive_tests.rs index 25b62e5..66a0f72 100644 --- a/tests/derive_tests.rs +++ b/tests/derive_tests.rs @@ -9,13 +9,14 @@ struct ServerSettings { #[test] fn test_incomplete_section_parsing() { - let config = Config::load_or_default("nonexistent.ini", || { - return sectioned_defaults! { - ["Server"] { - "address" => "192.168.1.1", // Missing `port` and `threads` - } - } - }); + let config = Config::load_or_default( + "nonexistent.ini", + sectioned_defaults! { + ["Server"] { + "address" => "192.168.1.1", // Missing `port` and `threads` + } + }, + ); let server_settings_result = ServerSettings::from_section(&config.section("Server").unwrap()); @@ -27,15 +28,16 @@ fn test_incomplete_section_parsing() { #[test] fn test_section_parsing_into_struct() { - let config = Config::load_or_default("nonexistent.ini", || { - return sectioned_defaults! { - ["Server"] { - "address" => "192.168.1.1", - "port" => "8000", - "threads" => "8", - } - } - }); + let config = Config::load_or_default( + "nonexistent.ini", + sectioned_defaults! { + ["Server"] { + "address" => "192.168.1.1", + "port" => "8000", + "threads" => "8", + } + }, + ); // Parse section into a struct let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();