Skip to main content

Crate figen

Crate figen 

Source
Expand description

§figen

This crate provides a way to define and load configurations in Rust, allowing for structured configuration management. It supports both standard and no-std environments, making it versatile for various applications.

§Example

The following example demonstrates how to define a configuration structure using the Configuration derive macro and load it using a custom property loader. The Configuration derive macro automatically generates the necessary bindings for the configuration fields, allowing you to easily manage and load configurations from various sources.

use figen::Configuration;

#[derive(Configuration, Debug, Default)]
struct MyConfig {
    #[property]
    my_field: String,
}

fn main() {
    let loader = MyPropertyLoader::new(); // Implement your own PropertyLoader
    let config: MyConfig = figen::load_config(&loader)
        .expect("Failed to load configuration");
    println!("{:?}", config);
}

A more typical usage would involve using the config_registry macro to define a configuration registry, which also generates the necessary bindings and structs for you.

use figen::config_registry;

config_registry!(
    name = MyGroupConfig
    version = 1
    str_property("my_str", default = "default_value", max_len = 10)
    num_property("my_nested.prop", default = 42)
);

fn main() {
    let loader = MyPropertyLoader::new(); // Implement your own PropertyLoader
    let config: MyGroupConfig = figen::load_config(&loader)
       .expect("Failed to load configuration");
    println!("{:?}", config);
}

§Using Custom Types

You can also define custom types for your configuration properties. The config_registry macro allows you to specify a custom type for a property, and it will generate the necessary bindings for that type. The custom type must implement the TryFrom<&str> trait to convert the string value into the custom type. Once the type implements TryFrom<&str>, derive ConfigBinder on the type.

§Example

use figen::config_registry;

#[derive(figen::ConfigBinder)]
struct CustomType {
   field1: i32,
   field2: bool,
}

impl TryFrom<&str> for CustomType {
   type Error = &'static str;

   fn try_from(value: &str) -> Result<Self, Self::Error> {
     // Custom conversion logic from &str:
     // parse `value` and return `Ok(CustomType { ... })` or `Err(...)`
     unimplemented!()
   }
}

config_registry!(
   name = MyGroupConfig
   version = 1
   custom_property("my_custom", default = "1,true", ty = CustomType)
)

Re-exports§

pub use crate::error::Result;

Modules§

binder
error
loader
registry

Macros§

config_registry
str_ty

Structs§

NoStdBindPath

Traits§

BindPath

Functions§

load_config
load_config_using_path

Type Aliases§

BindPathImpl

Derive Macros§

ConfigBinder
Configuration