Skip to main content

StoreConfigFile

Trait StoreConfigFile 

Source
pub trait StoreConfigFile: Serialize {
    // Required method
    fn store_with_specific_format(
        &self,
        path: impl AsRef<Path>,
        config_type: ConfigFormat,
    ) -> Result<()>;

    // Provided methods
    fn store(&self, path: impl AsRef<Path>) -> Result<()>
       where Self: Sized { ... }
    fn store_opts(&self, path: impl AsRef<Path>) -> StoreBuilder<'_, Self>
       where Self: Sized { ... }
}
Expand description

Trait for storing a struct into a configuration file. This trait is automatically implemented when serde::Serialize is.

Required Methods§

Source

fn store_with_specific_format( &self, path: impl AsRef<Path>, config_type: ConfigFormat, ) -> Result<()>

Store config file to path with specific format, do not use extension to determine. If the file already exists, the config file will be overwritten.

Parent directories are created automatically if they don’t exist.

§Errors

Provided Methods§

Source

fn store(&self, path: impl AsRef<Path>) -> Result<()>
where Self: Sized,

Store config file to path. If the file already exists, the config file will be overwritten.

Parent directories are created automatically if they don’t exist.

§Errors
  • Returns Error::UnsupportedFormat if the file extension is not supported.
  • Returns Error::<Format> if serialization to file fails.
Source

fn store_opts(&self, path: impl AsRef<Path>) -> StoreBuilder<'_, Self>
where Self: Sized,

Returns a StoreBuilder for storing self with full control over pretty, overwrite and format. Finish the chain with StoreBuilder::execute.

Defaults match StoreConfigFile::store: pretty on, overwrite on, format auto-detected from the path extension.

§Examples
use config_file2::StoreConfigFile;
use serde::Serialize;

#[derive(Serialize)]
struct Config;

Config
    .store_opts("/tmp/config.json")
    .pretty(false)
    .overwrite(false)
    .execute()
    .unwrap();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§