Crate btconfig

source ·
Expand description

This crate defines the common configuration system used by other Blocktree crates. To define a new configuration struct all you need to do is derive Serialize and Deserialize and implement the Default trait.

Environment variables prefixed with “BT_” are used for setting configuration values. When a configuration struct contains another struct in one of its fields, then names of the fields in the sub-struct are separated from the struct’s field name with _. In order for this to work you have to use a serde attribute to rename fields which contain _ characters. For example:

use btconfig::CredStoreConfig;
use std::path::PathBuf;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct AppConfig {
    #[serde(rename = "credstore")]
    cred_store: CredStoreConfig,
    path: PathBuf,
}

For this struct, the path field is set using the BT_PATH environment variable. In order to set credential store, the BT_CREDSTORE_TYPE variable must be set to a valid variant name of the CredStoreConfig enum, and the required fields must be set using their variables. For example, if BT_CREDSTORE_TYPE=File, then BT_CREDSTORE_PATH must be set to the path to the credential store file.

When an enum is used for configuration, serde must be configured to externally tag it in order for a variant to be specified using an environment variable. This is done by adding #[serde(tag = "type")] to the enum. See CredStoreConfig as an example.

Macros

  • Attempts to unwrap the Option field in $config called $name. This macro internal uses the ? operator, and can only be used in methods which return a [Result] which has an error type that a [btlib::Error] can be converted to.
  • Attempts to unwrap all of the Option fields with the given names in the configuration struct and returns them all as a tuple. The same consideration which apply to get_setting! apply to this macro.

Structs

Enums

  • Configuration which specifies how to construct a [btlib::crypto::CredStore].

Constants

Traits

Functions