Derive Macro smart_config::ExampleConfig

source ·
#[derive(ExampleConfig)]
{
    // Attributes available to this derive:
    #[config]
}
Expand description

Derives the ExampleConfig trait for a type.

This macro is intended to be used together with DescribeConfig; it reuses the same attributes. Specifically, for each config field, the default value is assigned from the following sources in the decreasing priority order:

  1. example
  2. default / default_t, including implied ones for Optional fields
  3. From ExampleConfig implementation (only for nested / flattened configs)

§Examples

#[derive(DescribeConfig, ExampleConfig)]
struct TestConfig {
    /// Required param that still has an example value.
    #[config(example = 42)]
    required: u32,
    optional: Option<String>,
    #[config(default_t = true)]
    with_default: bool,
    #[config(default, example = vec![5, 8])]
    values: Vec<u32>,
    #[config(nest)]
    nested: NestedConfig,
}

#[derive(DescribeConfig, ExampleConfig)]
struct NestedConfig {
    #[config(default, example = ["eth_call".into()].into())]
    methods: HashSet<String>,
}

let example: TestConfig = TestConfig::example_config();
let json = SerializerOptions::default().serialize(&example);
assert_eq!(
    serde_json::Value::from(json),
    serde_json::json!({
        "required": 42,
        "optional": null,
        "with_default": true,
        "values": [5, 8],
        "nested": {
            "methods": ["eth_call"],
        },
    })
);