Struct Config

Source
pub struct Config {
    pub sections: BTreeMap<String, BTreeMap<String, String>>,
    pub general_values: BTreeMap<String, String>,
}

Fields§

§sections: BTreeMap<String, BTreeMap<String, String>>§general_values: BTreeMap<String, String>

Implementations§

Source§

impl Config

Source

pub fn general(&self) -> &BTreeMap<String, String>

Source

pub fn get(&self, section: Option<&str>, key: &str) -> Option<&String>

Examples found in repository?
examples/get_values.rs (line 26)
10fn main() {
11    let config = Config::load_or_default("get-values.ini", || {
12        return sectioned_defaults! {
13            {
14                "console" => "true",
15                "log_level" => "info",
16            }
17            ["Server"] {
18                "address" => "127.0.0.1",
19                "port" => "8080",
20                "threads" => "4",
21            }
22        }
23    });
24
25    let console = config.get_as::<bool>(None, "console").unwrap();
26    let log_level = config.get(None, "log_level").unwrap();
27
28    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
29
30    println!(
31        "General:\n    console={:?}\n    log_level={:?}",
32        console, log_level
33    );
34    println!(
35        "Server:\n    address={:?}\n    port={:?}\n    threads={:?}",
36        server_settings.address, server_settings.port, server_settings.threads
37    );
38}
Source

pub fn get_as<T>(&self, section: Option<&str>, key: &str) -> Option<T>
where T: FromStr + Debug,

Examples found in repository?
examples/get_values.rs (line 25)
10fn main() {
11    let config = Config::load_or_default("get-values.ini", || {
12        return sectioned_defaults! {
13            {
14                "console" => "true",
15                "log_level" => "info",
16            }
17            ["Server"] {
18                "address" => "127.0.0.1",
19                "port" => "8080",
20                "threads" => "4",
21            }
22        }
23    });
24
25    let console = config.get_as::<bool>(None, "console").unwrap();
26    let log_level = config.get(None, "log_level").unwrap();
27
28    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
29
30    println!(
31        "General:\n    console={:?}\n    log_level={:?}",
32        console, log_level
33    );
34    println!(
35        "Server:\n    address={:?}\n    port={:?}\n    threads={:?}",
36        server_settings.address, server_settings.port, server_settings.threads
37    );
38}
Source

pub fn load(filename: &str) -> Result<Self, Error>

Source

pub fn load_or_default<F: FnOnce() -> Config>( filename: &str, default: F, ) -> Self

Examples found in repository?
examples/load_file.rs (lines 11-18)
3fn main() {
4    let filename = "load-file.ini";
5
6    // If you want to handle errors manually, use Config::load() instead.
7    // Returns Result<config_tools::Config, config_tools::Error>
8    // let config = Config::load(filename);
9
10    // Load and use defaults on failure
11    let config = Config::load_or_default(filename, || {
12        return sectioned_defaults! {
13            {
14                "host" => "127.0.0.1",
15                "port" => "8080",
16            }
17        }
18    });
19
20    config.save(filename).expect("Failed to save config.");
21
22    println!("{config:#?}");
23}
More examples
Hide additional examples
examples/from_section.rs (lines 18-34)
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}
examples/get_values.rs (lines 11-23)
10fn main() {
11    let config = Config::load_or_default("get-values.ini", || {
12        return sectioned_defaults! {
13            {
14                "console" => "true",
15                "log_level" => "info",
16            }
17            ["Server"] {
18                "address" => "127.0.0.1",
19                "port" => "8080",
20                "threads" => "4",
21            }
22        }
23    });
24
25    let console = config.get_as::<bool>(None, "console").unwrap();
26    let log_level = config.get(None, "log_level").unwrap();
27
28    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
29
30    println!(
31        "General:\n    console={:?}\n    log_level={:?}",
32        console, log_level
33    );
34    println!(
35        "Server:\n    address={:?}\n    port={:?}\n    threads={:?}",
36        server_settings.address, server_settings.port, server_settings.threads
37    );
38}
Source

pub fn builder() -> ConfigBuilder

Examples found in repository?
examples/general_manual.rs (line 4)
3fn main() {
4    let config = Config::builder()
5        .set("host", "127.0.0.1")
6        .set("port", "8080")
7        .build();
8
9    config
10        .save("general-manual.ini")
11        .expect("Failed to save config.");
12
13    println!("{:#?}", config);
14}
More examples
Hide additional examples
examples/sectioned_manual.rs (line 4)
3fn main() {
4    let config = Config::builder()
5        .section("Server")
6        .set("host", "127.0.0.1")
7        .set("port", "8080")
8        .section("Window")
9        .set("width", "720")
10        .set("height", "480")
11        .general()
12        .set("console", "true")
13        .build();
14
15    config
16        .save("sectioned-manual.ini")
17        .expect("Failed to save config.");
18
19    println!("{:#?}", config);
20}
Source

pub fn save(&self, filename: &str) -> Result<&Self, Error>

Examples found in repository?
examples/general_macro.rs (line 8)
1fn main() {
2    let config = config_tools::general_defaults! {
3        "host" => "127.0.0.1",
4        "port" => "8080",
5    };
6
7    config
8        .save("general-manual.ini")
9        .expect("Failed to save config.");
10
11    println!("{:#?}", config);
12}
More examples
Hide additional examples
examples/general_manual.rs (line 10)
3fn main() {
4    let config = Config::builder()
5        .set("host", "127.0.0.1")
6        .set("port", "8080")
7        .build();
8
9    config
10        .save("general-manual.ini")
11        .expect("Failed to save config.");
12
13    println!("{:#?}", config);
14}
examples/sectioned_manual.rs (line 16)
3fn main() {
4    let config = Config::builder()
5        .section("Server")
6        .set("host", "127.0.0.1")
7        .set("port", "8080")
8        .section("Window")
9        .set("width", "720")
10        .set("height", "480")
11        .general()
12        .set("console", "true")
13        .build();
14
15    config
16        .save("sectioned-manual.ini")
17        .expect("Failed to save config.");
18
19    println!("{:#?}", config);
20}
examples/sectioned_macro.rs (line 19)
1fn main() {
2    let config = config_tools::sectioned_defaults! {
3        {
4            "console" => "true"
5        }
6
7        ["Application"] {
8            "host" => "127.0.0.1",
9            "port" => "8080",
10        }
11
12        ["Window"] {
13            "width" => "720",
14            "height" => "480",
15        }
16    };
17
18    config
19        .save("sectioned-macro.ini")
20        .expect("Failed to save config.");
21
22    println!("{:#?}", config);
23}
examples/load_file.rs (line 20)
3fn main() {
4    let filename = "load-file.ini";
5
6    // If you want to handle errors manually, use Config::load() instead.
7    // Returns Result<config_tools::Config, config_tools::Error>
8    // let config = Config::load(filename);
9
10    // Load and use defaults on failure
11    let config = Config::load_or_default(filename, || {
12        return sectioned_defaults! {
13            {
14                "host" => "127.0.0.1",
15                "port" => "8080",
16            }
17        }
18    });
19
20    config.save(filename).expect("Failed to save config.");
21
22    println!("{config:#?}");
23}
Source

pub fn section(&self, title: &str) -> Option<&BTreeMap<String, String>>

Examples found in repository?
examples/from_section.rs (line 36)
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}
More examples
Hide additional examples
examples/get_values.rs (line 28)
10fn main() {
11    let config = Config::load_or_default("get-values.ini", || {
12        return sectioned_defaults! {
13            {
14                "console" => "true",
15                "log_level" => "info",
16            }
17            ["Server"] {
18                "address" => "127.0.0.1",
19                "port" => "8080",
20                "threads" => "4",
21            }
22        }
23    });
24
25    let console = config.get_as::<bool>(None, "console").unwrap();
26    let log_level = config.get(None, "log_level").unwrap();
27
28    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
29
30    println!(
31        "General:\n    console={:?}\n    log_level={:?}",
32        console, log_level
33    );
34    println!(
35        "Server:\n    address={:?}\n    port={:?}\n    threads={:?}",
36        server_settings.address, server_settings.port, server_settings.threads
37    );
38}
Source

pub fn sections(&self) -> &BTreeMap<String, BTreeMap<String, String>>

Source

pub fn update( &mut self, section: Option<&str>, key: &str, value: &str, ) -> &mut Self

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Config

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Config

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,