Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "config-tools"
version = "0.3.2"
version = "0.3.3"
edition = "2021"
authors = ["Gray Logan <literal.gray@gmail.com>"]
description = "A simplified set of tools for working with configuration files."
Expand Down
35 changes: 18 additions & 17 deletions examples/from_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 14 additions & 13 deletions examples/get_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<bool>(None, "console").unwrap();
let log_level = config.get(None, "log_level").unwrap();
Expand Down
17 changes: 9 additions & 8 deletions examples/load_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ impl Config {
})
}

pub fn load_or_default<F: FnOnce() -> 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,
}
}

Expand Down
54 changes: 28 additions & 26 deletions tests/config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<bool>(None, "console").unwrap();
let log_level = config.get(None, "log_level").unwrap();
Expand All @@ -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!(
Expand Down
34 changes: 18 additions & 16 deletions tests/derive_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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();
Expand Down