Struct ConfigBuilder

Source
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

Source

pub fn build(self) -> Result<Config, String>

Creates a new ConfigBuilder instance

Examples found in repository?
examples/basic.rs (line 10)
3fn main() {
4    let 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}
More examples
Hide additional examples
examples/formats/json.rs (line 10)
3fn main() {
4    let 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}
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}
Source

pub fn add_file(self, file: File) -> Self

Adds a file to the builder

Examples found in repository?
examples/basic.rs (lines 5-9)
3fn main() {
4    let 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}
More examples
Hide additional examples
examples/formats/json.rs (lines 5-9)
3fn main() {
4    let 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}
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}
Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.