from_section/
from_section.rs

1#![allow(dead_code)]
2use config_tools::{sectioned_defaults, Config, FromSection, Section};
3
4#[derive(Debug, FromSection)]
5struct ServerSettings {
6    address: String,
7    port: u16,
8    threads: u16,
9}
10
11#[derive(Debug, FromSection)]
12struct LdapSettings {
13    host: String,
14    domain: String,
15}
16
17fn main() {
18    let config = Config::load_or_default("get-values.ini", || {
19        return sectioned_defaults! {
20            {
21                "console" => "true",
22                "log_level" => "info",
23            }
24            ["Server"] {
25                "address" => "127.0.0.1",
26                "port" => "8080",
27                "threads" => "4",
28            }
29            ["LDAP"] {
30                "host" => "ldap://localhost:389",
31                "domain" => "example.com",
32            }
33        }
34    });
35
36    let ldap_settings = LdapSettings::from_section(&config.section("LDAP").unwrap()).unwrap();
37    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
38
39    println!("{ldap_settings:#?}");
40    println!("{server_settings:#?}");
41}