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

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

Source

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

Examples found in repository?
examples/load_file.rs (lines 11-19)
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(
12        filename,
13        sectioned_defaults! {
14                {
15                    "host" => "127.0.0.1",
16                    "port" => "8080",
17                }
18        },
19    );
20
21    config.save(filename).expect("Failed to save config.");
22
23    println!("{config:#?}");
24}
More examples
Hide additional examples
examples/from_section.rs (lines 18-35)
17fn main() {
18    let config = Config::load_or_default(
19        "get-values.ini",
20        sectioned_defaults! {
21                {
22                    "console" => "true",
23                    "log_level" => "info",
24                }
25                ["Server"] {
26                    "address" => "127.0.0.1",
27                    "port" => "8080",
28                    "threads" => "4",
29                }
30                ["LDAP"] {
31                    "host" => "ldap://localhost:389",
32                    "domain" => "example.com",
33                }
34        },
35    );
36
37    let ldap_settings = LdapSettings::from_section(&config.section("LDAP").unwrap()).unwrap();
38    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
39
40    println!("{ldap_settings:#?}");
41    println!("{server_settings:#?}");
42}
examples/get_values.rs (lines 11-24)
10fn main() {
11    let config = Config::load_or_default(
12        "get-values.ini",
13        sectioned_defaults! {
14                {
15                    "console" => "true",
16                    "log_level" => "info",
17                }
18                ["Server"] {
19                    "address" => "127.0.0.1",
20                    "port" => "8080",
21                    "threads" => "4",
22                }
23        },
24    );
25
26    let console = config.get_as::<bool>(None, "console").unwrap();
27    let log_level = config.get(None, "log_level").unwrap();
28
29    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();
30
31    println!(
32        "General:\n    console={:?}\n    log_level={:?}",
33        console, log_level
34    );
35    println!(
36        "Server:\n    address={:?}\n    port={:?}\n    threads={:?}",
37        server_settings.address, server_settings.port, server_settings.threads
38    );
39}
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 17)
1fn main() {
2    let config = config_tools::sectioned_defaults! {
3        { "console" => "true" }
4
5        ["Application"] {
6            "host" => "127.0.0.1",
7            "port" => "8080",
8        }
9
10        ["Window"] {
11            "width" => "720",
12            "height" => "480",
13        }
14    };
15
16    config
17        .save("sectioned-macro.ini")
18        .expect("Failed to save config.");
19
20    println!("{:#?}", config);
21}
examples/load_file.rs (line 21)
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(
12        filename,
13        sectioned_defaults! {
14                {
15                    "host" => "127.0.0.1",
16                    "port" => "8080",
17                }
18        },
19    );
20
21    config.save(filename).expect("Failed to save config.");
22
23    println!("{config:#?}");
24}
Source

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

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