Struct Config

Source
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

Source

pub fn builder() -> ConfigBuilder

Creates a ConfigBuilder

Examples found in repository?
examples/basic.rs (line 4)
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 4)
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 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}
Source

pub fn get(&self, key: &str) -> Option<&Value>

Get a value from config using a key

Examples found in repository?
examples/basic.rs (line 12)
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 12)
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 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}
Source

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
Hide additional 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}
Source

pub fn list(&self) -> Vec<String>

List all keys in the config

Source

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§

Source§

impl Display for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.