Struct Config

Source
pub struct Config<D>
where for<'a> D: Deserialize<'a> + Serialize,
{ pub data: D, pub path: PathBuf, pub options: InternalOptions, }
Expand description

The main class you use to create/access your configuration files!

§Construction

See Config::new and Config::from_options if you wish to construct a new Config!

§Data

This class stores data within a data struct you define yourself. This allows for the most amount of performance and safety, while also allowing you to add additional features by adding impl blocks on your struct.

Your data struct needs to implement Serialize and Deserialize. In most cases you can just use #[derive(Serialize, Deserialize)] to derive them.

§Examples

Here is a code example on how you could define the data to pass into the constructors on this class:

use serde::{Serialize, Deserialize};

// Creating a config struct to store our data
#[derive(Serialize, Deserialize)]
struct MyData {
    pub student_debt: i32,
}

fn main() {
    // Making our data and setting its default values
    let data = MyData {
        student_debt: 20
    };
    // ..
}

Implementing Serialize and Deserialize yourself is quite complicated but will provide the most flexibility.

If you wish to implement them yourself I’d recommend reading the Serde docs on it

Fields§

§data: D§path: PathBuf§options: InternalOptions

Implementations§

Source§

impl<D> Config<D>
where for<'a> D: Deserialize<'a> + Serialize,

Source

pub fn new(path: impl AsRef<Path>, data: D) -> Result<Config<D>, ConfigError>

Constructs and returns a new config object using the default options.

If there is a file at path, the file will be opened.

  • path: Takes in a path to where the config file is or should be located. If the file has no extension, the crate will attempt to guess the extension from one available format feature.

  • data: Takes in a struct that inherits Serialize and Deserialize You have to make this struct yourself, construct it, and pass it in. More info about it is provided at Config.

If you’d like to configure this object, you should take a look at using Config::from_options instead.

Source

pub fn from_options( path: impl AsRef<Path>, options: ConfigSetupOptions, data: D, ) -> Result<Config<D>, ConfigError>

Constructs and returns a new config object from a set of custom options.

  • path: Takes in a path to where the config file is or should be located.
    If the file has no extension, and there is no format selected in your options, the crate will attempt to guess the extension from one available format features.

  • options: Takes in a ConfigSetupOptions, used to configure the format language, styling of the data, and other things.
    Remember to add .. Default::default() at the end of your options as more options are going to be added to the crate later on.

  • data: Takes in a struct that inherits Serialize and Deserialize You have to make this struct yourself, construct it, and pass it in. More info is provided at Config.

Source

pub fn save(&self) -> Result<(), ConfigSaveError>

Saves the config file to the disk.

It uses the Config’s object own internal path property to get the path required to save the file so there is no need to pass in the path to save it at.

If you wish to specify the path to save it at you can change the path yourself by setting the Config’s path property.

§save_at method

There used to be a built-in function called save_at while i was developing the crate, but I ended up removing it due to the fact i didn’t see anyone actually using it, and it might’ve ended up in some users getting confused, as well as a tiny bit of performance overhead.

If you’d like this feature to be back feel free to open an issue and I’ll add it back right away!

Source

pub fn filename(&self) -> String

Gets the name of the config file

Trait Implementations§

Source§

impl<D> Drop for Config<D>
where for<'a> D: Deserialize<'a> + Serialize,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<D> Freeze for Config<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for Config<D>
where D: RefUnwindSafe,

§

impl<D> Send for Config<D>
where D: Send,

§

impl<D> Sync for Config<D>
where D: Sync,

§

impl<D> Unpin for Config<D>
where D: Unpin,

§

impl<D> UnwindSafe for Config<D>
where D: UnwindSafe,

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.