config_tools

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)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    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();

    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();

    println!("General:\n    console={:?}\n    log_level={:?}", console, log_level);
    println!("Server:\n    address={:?}\n    port={:?}\n    threads={:?}", server_settings.address, server_settings.port, server_settings.threads);
}
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 24)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    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();

    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();

    println!("General:\n    console={:?}\n    log_level={:?}", console, log_level);
    println!("Server:\n    address={:?}\n    port={:?}\n    threads={:?}", server_settings.address, server_settings.port, server_settings.threads);
}
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-16)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    let filename = "load-file.ini";

    // If you want to handle errors manually, use Config::load() instead.
    // Returns Result<config_tools::Config, config_tools::Error>
    // let config = Config::load(filename);

    // Load and use defaults on failure
    let config = Config::load_or_default(filename, || sectioned_defaults! {
        {
            "host" => "127.0.0.1",
            "port" => "8080",
        }
    });

    config.save(filename).expect("Failed to save config.");

    println!("{config:#?}");
}
More examples
Hide additional examples
examples/get_values.rs (lines 11-21)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    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();

    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();

    println!("General:\n    console={:?}\n    log_level={:?}", console, log_level);
    println!("Server:\n    address={:?}\n    port={:?}\n    threads={:?}", server_settings.address, server_settings.port, server_settings.threads);
}
source

pub fn builder() -> ConfigBuilder

Examples found in repository?
examples/general_manual.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    let config = Config::builder()
        .set("host", "127.0.0.1")
        .set("port", "8080")
        .build();

    config
        .save("general-manual.ini")
        .expect("Failed to save config.");

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

    config
        .save("sectioned-manual.ini")
        .expect("Failed to save config.");

    println!("{:#?}", config);
}
source

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

Examples found in repository?
examples/general_macro.rs (line 8)
1
2
3
4
5
6
7
8
9
10
11
12
fn main() {
    let config = config_tools::general_defaults! {
        "host" => "127.0.0.1",
        "port" => "8080",
    };

    config
        .save("general-manual.ini")
        .expect("Failed to save config.");

    println!("{:#?}", config);
}
More examples
Hide additional examples
examples/general_manual.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    let config = Config::builder()
        .set("host", "127.0.0.1")
        .set("port", "8080")
        .build();

    config
        .save("general-manual.ini")
        .expect("Failed to save config.");

    println!("{:#?}", config);
}
examples/sectioned_manual.rs (line 16)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    let config = Config::builder()
        .section("Server")
        .set("host", "127.0.0.1")
        .set("port", "8080")
        .section("Window")
        .set("width", "720")
        .set("height", "480")
        .general()
        .set("console", "true")
        .build();

    config
        .save("sectioned-manual.ini")
        .expect("Failed to save config.");

    println!("{:#?}", config);
}
examples/sectioned_macro.rs (line 19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    let config = config_tools::sectioned_defaults! {
        {
            "console" => "true"
        }

        ["Application"] {
            "host" => "127.0.0.1",
            "port" => "8080",
        }

        ["Window"] {
            "width" => "720",
            "height" => "480",
        }
    };

    config
        .save("sectioned-macro.ini")
        .expect("Failed to save config.");

    println!("{:#?}", config);
}
examples/load_file.rs (line 18)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    let filename = "load-file.ini";

    // If you want to handle errors manually, use Config::load() instead.
    // Returns Result<config_tools::Config, config_tools::Error>
    // let config = Config::load(filename);

    // Load and use defaults on failure
    let config = Config::load_or_default(filename, || sectioned_defaults! {
        {
            "host" => "127.0.0.1",
            "port" => "8080",
        }
    });

    config.save(filename).expect("Failed to save config.");

    println!("{config:#?}");
}
source

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

Examples found in repository?
examples/get_values.rs (line 30)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    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();

    let server_settings = ServerSettings::from_section(&config.section("Server").unwrap()).unwrap();

    println!("General:\n    console={:?}\n    log_level={:?}", console, log_level);
    println!("Server:\n    address={:?}\n    port={:?}\n    threads={:?}", server_settings.address, server_settings.port, server_settings.threads);
}
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 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> 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, 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>,