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)
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);
}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 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);
}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-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
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);
}sourcepub fn builder() -> ConfigBuilder
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
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);
}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)
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
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:#?}");
}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/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);
}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