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 get(&self, section: Option<&str>, key: &str) -> Option<&String>

source

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

Examples found in repository?
examples/load.rs (line 5)
3
4
5
6
7
fn main() {
    let filename = "config.ini"; // Change as needed
    let config = Config::load(filename).expect("Failed to load config file");
    println!("{config:#?}");
}
source

pub fn new<'a>() -> ConfigBuilder<'a>

Examples found in repository?
examples/general_manual.rs (line 4)
3
4
5
6
7
8
9
10
fn main() -> Result<(), config_tools::Error> {
    let config = Config::new()
        .set("host", "127.0.0.1")
        .set("port", "8080")
        .build();

    config.save("config-general-manual.ini")
}
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
fn main() -> Result<(), config_tools::Error> {
    let config = Config::new()
        .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("config-sectioned-manual.ini")
}
source

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

Examples found in repository?
examples/general_manual.rs (line 9)
3
4
5
6
7
8
9
10
fn main() -> Result<(), config_tools::Error> {
    let config = Config::new()
        .set("host", "127.0.0.1")
        .set("port", "8080")
        .build();

    config.save("config-general-manual.ini")
}
More examples
Hide additional examples
examples/general_macro.rs (line 9)
1
2
3
4
5
6
7
8
9
10
fn main() -> Result<(), config_tools::Error> {
    let mut config = config_tools::general_defaults! {
        "host" => "127.0.0.1",
        "port" => "8080",
    };

    config.update(None, "host", "0.0.0.0");

    config.save("config-general-macro.ini")
}
examples/sectioned_manual.rs (line 15)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() -> Result<(), config_tools::Error> {
    let config = Config::new()
        .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("config-sectioned-manual.ini")
}
examples/sectioned_macro.rs (line 20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() -> Result<(), config_tools::Error> {
    let mut config = config_tools::sectioned_defaults! {
        {
            "console" => "true"
        }

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

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

    config.update(Some("Application"), "host", "0.0.0.0");

    config.save("config-sectioned-macro.ini")
}
source

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

source

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

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

    config.update(None, "host", "0.0.0.0");

    config.save("config-general-macro.ini")
}
More examples
Hide additional examples
examples/sectioned_macro.rs (line 18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() -> Result<(), config_tools::Error> {
    let mut config = config_tools::sectioned_defaults! {
        {
            "console" => "true"
        }

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

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

    config.update(Some("Application"), "host", "0.0.0.0");

    config.save("config-sectioned-macro.ini")
}

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>,