pub trait Config:
Serialize
+ Default
+ for<'de> Deserialize<'de> {
const FILE_NAME: &'static str;
const TYPE: ConfigType = ConfigType::Toml;
// Required method
fn get_save_dir() -> PathBuf;
// Provided methods
fn get_full_path() -> PathBuf { ... }
fn load() -> Self { ... }
fn save(&self) -> IoResult<()> { ... }
fn get_config_root() -> PathBuf { ... }
}
Expand description
The Config
trait allows a type to have an associated config file
Requires the Default
, Serialize
, and Deserialize
traits (can be derived) to be implemented
Required Associated Constants§
Provided Associated Constants§
Sourceconst TYPE: ConfigType = ConfigType::Toml
const TYPE: ConfigType = ConfigType::Toml
This constant is the type of config
Defaults to TOML
Required Methods§
Sourcefn get_save_dir() -> PathBuf
fn get_save_dir() -> PathBuf
Returns the save directory of the config file.
This method must be defined
Provided Methods§
Sourcefn get_full_path() -> PathBuf
fn get_full_path() -> PathBuf
Returns the full path of the config file
Sourcefn load() -> Self
fn load() -> Self
Load from the config file
Returns the defaults if the file does not exist or is corrupted
Panics if the config directory cannot be created
Sourcefn save(&self) -> IoResult<()>
fn save(&self) -> IoResult<()>
Save to the config file
It is recommended to call this in every method that has &mut self as an argument!
Sourcefn get_config_root() -> PathBuf
fn get_config_root() -> PathBuf
Helper function to get the user’s config root folder (normally $HOME/.config
on most unix-like systems)
Panics if your user does not have a home folder
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.