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: InternalOptionsImplementations§
Source§impl<D> Config<D>where
for<'a> D: Deserialize<'a> + Serialize,
impl<D> Config<D>where
for<'a> D: Deserialize<'a> + Serialize,
Sourcepub fn new(path: impl AsRef<Path>, data: D) -> Result<Config<D>, ConfigError>
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 formatfeature. -
data: Takes in a struct that inheritsSerializeandDeserializeYou have to make this struct yourself, construct it, and pass it in. More info about it is provided atConfig.
If you’d like to configure this object, you should take a look at using Config::from_options instead.
Sourcepub fn from_options(
path: impl AsRef<Path>,
options: ConfigSetupOptions,
data: D,
) -> Result<Config<D>, ConfigError>
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 noformatselected in youroptions, the crate will attempt to guess the extension from one available formatfeatures. -
options: Takes in aConfigSetupOptions, used to configure the format language, styling of the data, and other things.
Remember to add..Default::default()at the end of youroptionsas more options are going to be added to the crate later on. -
data: Takes in a struct that inheritsSerializeandDeserializeYou have to make this struct yourself, construct it, and pass it in. More info is provided atConfig.
Sourcepub fn save(&self) -> Result<(), ConfigSaveError>
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!