Trait LoadConfigFile

Source
pub trait LoadConfigFile {
    // Required method
    fn load_with_specific_format(
        path: impl AsRef<Path>,
        config_type: ConfigFormat,
    ) -> Result<Option<Self>>
       where Self: Sized;

    // Provided methods
    fn load(path: impl AsRef<Path>) -> Result<Option<Self>>
       where Self: Sized { ... }
    fn load_or_default(path: impl AsRef<Path>) -> Result<Self>
       where Self: Sized + Default { ... }
}
Expand description

Trait for loading a struct from a configuration file. This trait is automatically implemented when serde::Deserialize is.

Required Methods§

Source

fn load_with_specific_format( path: impl AsRef<Path>, config_type: ConfigFormat, ) -> Result<Option<Self>>
where Self: Sized,

Load config from path with specific format, do not use extension to determine.

§Returns
  • Returns Ok(Some(config)) if the file exists.
  • Returns Ok(None) if the file does not exist.
§Errors
  • Returns Error::FileAccess if the file cannot be read.
  • Returns Error::<Format> if deserialization from file fails.

Provided Methods§

Source

fn load(path: impl AsRef<Path>) -> Result<Option<Self>>
where Self: Sized,

Load config from path.

§Returns
  • Returns Ok(Some(config)) if the file exists.
  • Returns Ok(None) if the file does not exist.
§Errors
Source

fn load_or_default(path: impl AsRef<Path>) -> Result<Self>
where Self: Sized + Default,

Load config from path, if not found, use default instead

§Returns
  • Returns the config loaded from file if the file exists, or default value if the file does not exist.
§Errors
  • Returns Error::FileAccess if the file cannot be read by Permission denied or other failures.
  • Returns Error::UnsupportedFormat if the file extension is not supported.
  • Returns Error::<Format> if deserialization from file fails.

Implementors§