pub struct ConfigBuilder {
pub files: Vec<File>,
pub changes: IndexMap<String, Value>,
}Expand description
Builder for the Config struct
Fields§
§files: Vec<File>§changes: IndexMap<String, Value>Implementations§
Source§impl ConfigBuilder
impl ConfigBuilder
Sourcepub fn build(self) -> Result<Config, String>
pub fn build(self) -> Result<Config, String>
Creates a new ConfigBuilder instance
Examples found in repository?
More examples
examples/changes.rs (line 10)
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 8)
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 add_file(self, file: File) -> Self
pub fn add_file(self, file: File) -> Self
Adds a file to the builder
Examples found in repository?
More examples
examples/changes.rs (lines 5-9)
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 7)
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 load(self, file: File) -> Result<Self, String>
pub fn load(self, file: File) -> Result<Self, String>
Loads changes to default configuration from .add_file() from a file.
Example:
#[cfg(features = "json")]
{
use ronf::{Config, File, FileFormat};
let default_file = File::new_str("test_file", FileFormat::Json, "{\"key\": \"value\"}");
let save = {
let mut config = Config::builder()
.add_file(default_file.clone())
.build()
.unwrap();
println!("\"key\": {}", config.get("key").unwrap());
config.set("key", "another value".into());
println!("\"key\" after change: {}", config.get("key").unwrap());
config.save(FileFormat::Json).unwrap()
};
let loaded_config = Config::builder()
.add_file(default_file.clone())
.load(File::new("save.json".to_string(), FileFormat::Json, save))
.unwrap()
.build()
.unwrap();
println!("\"key\" after load: {}", loaded_config.get("key").unwrap());
}Examples found in repository?
examples/saves.rs (line 18)
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}Auto Trait Implementations§
impl Freeze for ConfigBuilder
impl RefUnwindSafe for ConfigBuilder
impl Send for ConfigBuilder
impl Sync for ConfigBuilder
impl Unpin for ConfigBuilder
impl UnwindSafe for ConfigBuilder
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