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
impl Config
pub fn general(&self) -> &BTreeMap<String, String>
Sourcepub fn get(&self, section: Option<&str>, key: &str) -> Option<&String>
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}Sourcepub fn get_as<T>(&self, section: Option<&str>, key: &str) -> Option<T>
pub fn get_as<T>(&self, section: Option<&str>, key: &str) -> Option<T>
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}pub fn load(filename: &str) -> Result<Self, Error>
Sourcepub fn load_or_default(filename: &str, default: Config) -> Self
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
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}Sourcepub fn builder() -> ConfigBuilder
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
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}Sourcepub fn save(&self, filename: &str) -> Result<&Self, Error>
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
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}Sourcepub fn section(&self, title: &str) -> Option<&BTreeMap<String, String>>
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
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}pub fn sections(&self) -> &BTreeMap<String, BTreeMap<String, String>>
pub fn update( &mut self, section: Option<&str>, key: &str, value: &str, ) -> &mut Self
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Config
impl<'de> Deserialize<'de> for Config
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more