Skip to main content

RuntimeConfig

Trait RuntimeConfig 

Source
pub trait RuntimeConfig:
    Default
    + Clone
    + Serialize
    + DeserializeOwned
    + Send
    + Sync
    + 'static {
    // Required methods
    fn storage() -> &'static Mutex<Option<Arc<Self>>>;
    fn file_names() -> &'static [&'static str];

    // Provided methods
    fn section_file_names() -> &'static [(&'static str, &'static str)] { ... }
    fn override_from_env(self) -> Self { ... }
    fn get() -> Arc<Self>  { ... }
    fn set(config: Self) { ... }
    fn save_default<P>(path: P) -> Result<(), Error>
       where P: AsRef<Path> { ... }
    fn from_current_dir() -> Self { ... }
    fn from_file_path<P>(path: P) -> Result<Self, Error>
       where P: AsRef<Path> { ... }
    fn from_section_file_path<P>(path: P, section: &str) -> Result<Self, Error>
       where P: AsRef<Path> { ... }
}
Expand description

Trait for runtime configurations potentially loaded from a TOML file.

Implementors provide a global storage slot and the set of file names to search for; the trait supplies the lookup, lazy-initialization, and serialization logic.

The singleton stored in [Config::storage] is initialized on the first call to [Config::get] by walking up the current working directory looking for any of the names returned by [Config::file_names]. If none is found, Default is used.

Required Methods§

Source

fn storage() -> &'static Mutex<Option<Arc<Self>>>

Global storage for the configuration singleton.

Each implementor must declare its own static slot, because Rust traits cannot own statics directly.

Source

fn file_names() -> &'static [&'static str]

File names searched in each directory during [Config::from_current_dir].

The first existing file wins.

Provided Methods§

Source

fn section_file_names() -> &'static [(&'static str, &'static str)]

File names searched in each directory, where only a specific TOML section is loaded instead of the whole file.

Each entry is (file_name, section_name) and the section must deserialize to Self. Checked after [Config::file_names] at each directory level.

Source

fn override_from_env(self) -> Self

Available on std_io only.

Hook to override fields from environment variables after loading from disk.

The default implementation returns self unchanged.

Source

fn get() -> Arc<Self>

Retrieves the current configuration, loading it from the current directory if not set.

If no configuration is set, it attempts to load one from any of [Config::file_names] in the current directory or its parents. If no file is found, a default configuration is used.

§Notes

Calling this function is somewhat expensive, because of a global static lock. The config format is optimized for parsing, not for consumption. A good practice is to use a local static atomic value that you can populate with the appropriate value from the config during initialization.

Source

fn set(config: Self)

Sets the configuration to the provided value.

§Panics

Panics if the configuration has already been set or read, as it cannot be overridden.

§Warning

This method must be called at the start of the program, before any calls to [Config::get]. Attempting to set the configuration after it has been initialized will cause a panic.

Source

fn save_default<P>(path: P) -> Result<(), Error>
where P: AsRef<Path>,

Available on std_io only.

Save the default configuration to the provided file path.

Source

fn from_current_dir() -> Self

Available on std_io only.

Loads configuration from any of [Config::file_names] in the current directory or its parents.

Traverses up the directory tree until a valid configuration file is found or the root is reached. Returns a default configuration if no file is found.

Source

fn from_file_path<P>(path: P) -> Result<Self, Error>
where P: AsRef<Path>,

Available on std_io only.

Loads configuration from a specified file path.

Source

fn from_section_file_path<P>(path: P, section: &str) -> Result<Self, Error>
where P: AsRef<Path>,

Available on std_io only.

Loads configuration from a specific TOML section of the file at the given path.

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.

Implementors§