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 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}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 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}pub fn load(filename: &str) -> Result<Self, Error>
Sourcepub fn load_or_default<F: FnOnce() -> Config>(
filename: &str,
default: F,
) -> Self
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
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}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 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}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 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
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}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