pub struct ConfigLoader<'a> { /* private fields */ }Expand description
The main loader struct.
Create a new loader and configure as appropriate to load your config file.
Implementations§
Source§impl<'a> ConfigLoader<'a>
impl<'a> ConfigLoader<'a>
Sourcepub const fn new(app_name: &'a str) -> ConfigLoader<'a>
pub const fn new(app_name: &'a str) -> ConfigLoader<'a>
Creates a new config loader for the provided app name. Uses a default file name of “config” and all formats.
Sourcepub const fn with_file_name(self, file_name: &'a str) -> Self
pub const fn with_file_name(self, file_name: &'a str) -> Self
Specifies the file name to look for, excluding the extension.
If not specified, defaults to “config”.
Sourcepub const fn with_formats(self, formats: &'a [Format]) -> Self
pub const fn with_formats(self, formats: &'a [Format]) -> Self
Specifies which file formats to search for, and in which order.
If not specified, all formats are checked for in the order JSON, YAML, TOML, Corn.
Sourcepub const fn with_config_dir(self, dir: &'a str) -> Self
pub const fn with_config_dir(self, dir: &'a str) -> Self
Specifies which directory the config should be loaded from.
If not specified, loads from $XDG_CONFIG_DIR/<app_name>
or $HOME/.<app_name> if the config dir does not exist.
Sourcepub fn find_and_load<T: DeserializeOwned>(
&self,
) -> Result<T, UniversalConfigError>
pub fn find_and_load<T: DeserializeOwned>( &self, ) -> Result<T, UniversalConfigError>
Attempts to locate a config file on disk and load it.
§Errors
Will return a UniversalConfigError if any error occurs
when looking for, reading, or deserializing a config file.
Sourcepub fn config_dir(&self) -> Result<PathBuf, UniversalConfigError>
pub fn config_dir(&self) -> Result<PathBuf, UniversalConfigError>
Attempts to find the directory in which the config file is stored.
§Errors
Will error if the user’s home directory cannot be located.
Sourcepub fn load<T: DeserializeOwned, P: AsRef<Path>>(
path: P,
) -> Result<T, UniversalConfigError>
pub fn load<T: DeserializeOwned, P: AsRef<Path>>( path: P, ) -> Result<T, UniversalConfigError>
Loads the file at the given path,
deserializing it into a new T.
The type is automatically determined from the file extension.
§Errors
Will return a UniversalConfigError if unable to read or deserialize the file.
Sourcepub fn save<T: Serialize>(
&self,
config: &T,
format: &Format,
) -> Result<(), UniversalConfigError>
pub fn save<T: Serialize>( &self, config: &T, format: &Format, ) -> Result<(), UniversalConfigError>
Saves the provided configuration into a file of the specified format.
The file is stored in the app’s configuration directory. Directories are automatically created if required.
§Errors
If the provided config cannot be serialised into the format, an error will be returned.
The .corn format is not supported, and the function will error if specified.
If a valid config dir cannot be found, an error will be returned.
If the file cannot be written to the specified path, an error will be returned.