Struct File

Source
pub struct File {
    pub path: String,
    pub format: FileFormat,
    pub content: String,
}
Expand description

Representation of a configuration file.

Fields§

§path: String§format: FileFormat§content: String

Implementations§

Source§

impl File

Source

pub fn new(path: String, format: FileFormat, content: String) -> Self

Create a new file with the given path, format, and content.

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

pub fn new_str(path: &str, format: FileFormat, content: &str) -> Self

Create a new file with the given path, format, and content as a &str.

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 4)
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 parse(&self) -> Result<IndexMap<String, Value>, String>

Parse the content of the file to be used in the Config.

Trait Implementations§

Source§

impl Clone for File

Source§

fn clone(&self) -> File

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for File

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for File

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.