pub struct Config { /* private fields */ }Expand description
Configuration structure to hold parsed values
Simple example:
#[cfg(features = "json")]
{
use ronf::{Config, File, FileFormat};
let config = Config::builder().add_file(File::new_str("test_file", FileFormat::Json, "{\"key\":
\"value\"}")).build().unwrap();
println!("\"key\": {}", config.get("key").unwrap());
}Implementations§
Source§impl Config
impl Config
Sourcepub fn builder() -> ConfigBuilder
pub fn builder() -> ConfigBuilder
Creates a ConfigBuilder
Examples found in repository?
More examples
examples/changes.rs (line 4)
3fn main() {
4 let mut config = Config::builder()
5 .add_file(File::new_str(
6 "test_file",
7 FileFormat::Json,
8 "{\"key\": \"value\"}",
9 ))
10 .build()
11 .unwrap();
12 println!("\"key\": {}", config.get("key").unwrap());
13 config.set("key", "another value".into());
14 println!("\"key\" after change: {}", config.get("key").unwrap());
15}examples/saves.rs (line 6)
3fn main() {
4 let default_file = File::new_str("test_file", FileFormat::Json, "{\"key\": \"value\"}");
5 let save = {
6 let mut config = Config::builder()
7 .add_file(default_file.clone())
8 .build()
9 .unwrap();
10 println!("\"key\": {}", config.get("key").unwrap());
11 config.set("key", "another value".into());
12 println!("\"key\" after change: {}", config.get("key").unwrap());
13 config.save(FileFormat::Json).unwrap()
14 };
15
16 let loaded_config = Config::builder()
17 .add_file(default_file.clone())
18 .load(File::new("save.json".to_string(), FileFormat::Json, save))
19 .unwrap()
20 .build()
21 .unwrap();
22 println!("\"key\" after load: {}", loaded_config.get("key").unwrap());
23}Sourcepub fn get(&self, key: &str) -> Option<&Value>
pub fn get(&self, key: &str) -> Option<&Value>
Get a value from config using a key
Examples found in repository?
More examples
examples/changes.rs (line 12)
3fn main() {
4 let mut config = Config::builder()
5 .add_file(File::new_str(
6 "test_file",
7 FileFormat::Json,
8 "{\"key\": \"value\"}",
9 ))
10 .build()
11 .unwrap();
12 println!("\"key\": {}", config.get("key").unwrap());
13 config.set("key", "another value".into());
14 println!("\"key\" after change: {}", config.get("key").unwrap());
15}examples/saves.rs (line 10)
3fn main() {
4 let default_file = File::new_str("test_file", FileFormat::Json, "{\"key\": \"value\"}");
5 let save = {
6 let mut config = Config::builder()
7 .add_file(default_file.clone())
8 .build()
9 .unwrap();
10 println!("\"key\": {}", config.get("key").unwrap());
11 config.set("key", "another value".into());
12 println!("\"key\" after change: {}", config.get("key").unwrap());
13 config.save(FileFormat::Json).unwrap()
14 };
15
16 let loaded_config = Config::builder()
17 .add_file(default_file.clone())
18 .load(File::new("save.json".to_string(), FileFormat::Json, save))
19 .unwrap()
20 .build()
21 .unwrap();
22 println!("\"key\" after load: {}", loaded_config.get("key").unwrap());
23}Sourcepub fn set(&mut self, key: &str, value: Value)
pub fn set(&mut self, key: &str, value: Value)
Set a value in config changes using a key
Examples found in repository?
examples/changes.rs (line 13)
3fn main() {
4 let mut config = Config::builder()
5 .add_file(File::new_str(
6 "test_file",
7 FileFormat::Json,
8 "{\"key\": \"value\"}",
9 ))
10 .build()
11 .unwrap();
12 println!("\"key\": {}", config.get("key").unwrap());
13 config.set("key", "another value".into());
14 println!("\"key\" after change: {}", config.get("key").unwrap());
15}More examples
examples/saves.rs (line 11)
3fn main() {
4 let default_file = File::new_str("test_file", FileFormat::Json, "{\"key\": \"value\"}");
5 let save = {
6 let mut config = Config::builder()
7 .add_file(default_file.clone())
8 .build()
9 .unwrap();
10 println!("\"key\": {}", config.get("key").unwrap());
11 config.set("key", "another value".into());
12 println!("\"key\" after change: {}", config.get("key").unwrap());
13 config.save(FileFormat::Json).unwrap()
14 };
15
16 let loaded_config = Config::builder()
17 .add_file(default_file.clone())
18 .load(File::new("save.json".to_string(), FileFormat::Json, save))
19 .unwrap()
20 .build()
21 .unwrap();
22 println!("\"key\" after load: {}", loaded_config.get("key").unwrap());
23}Sourcepub fn save(&self, format: FileFormat) -> Result<String, String>
pub fn save(&self, format: FileFormat) -> Result<String, String>
Save the current configuration to a file in the specified format
Examples found in repository?
examples/saves.rs (line 13)
3fn main() {
4 let default_file = File::new_str("test_file", FileFormat::Json, "{\"key\": \"value\"}");
5 let save = {
6 let mut config = Config::builder()
7 .add_file(default_file.clone())
8 .build()
9 .unwrap();
10 println!("\"key\": {}", config.get("key").unwrap());
11 config.set("key", "another value".into());
12 println!("\"key\" after change: {}", config.get("key").unwrap());
13 config.save(FileFormat::Json).unwrap()
14 };
15
16 let loaded_config = Config::builder()
17 .add_file(default_file.clone())
18 .load(File::new("save.json".to_string(), FileFormat::Json, save))
19 .unwrap()
20 .build()
21 .unwrap();
22 println!("\"key\" after load: {}", loaded_config.get("key").unwrap());
23}Trait Implementations§
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