ConfigLoader

Struct ConfigLoader 

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

Source

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.

Source

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”.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<'a> Freeze for ConfigLoader<'a>

§

impl<'a> RefUnwindSafe for ConfigLoader<'a>

§

impl<'a> Send for ConfigLoader<'a>

§

impl<'a> Sync for ConfigLoader<'a>

§

impl<'a> Unpin for ConfigLoader<'a>

§

impl<'a> UnwindSafe for ConfigLoader<'a>

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<I, T> ExtractContext<I, ()> for T

Source§

fn extract_context(self, _original_input: I)

Given the context attached to a nom error, and given the original input to the nom parser, extract more the useful context information. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<I> RecreateContext<I> for I

Source§

fn recreate_context(_original_input: I, tail: I) -> I

Given the original input, as well as the context reported by nom, recreate a context in the original string where the error occurred. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more