Skip to main content

Module doc_generated_code

Module doc_generated_code 

Source
Expand description

Code generated by the tor_config macro

Here we describe the code generated by the derive_deftly(TorConfig) macro. Note that this is the default behavior; particular attributes not described here will override it.

The tor_config macro generates a Builder struct, with a name formed by appending Builder to the name of the configuration struct. That is, if the configuration struct is MyConfig, the macro generates MyConfigBuilder.

The builder struct has the same visibility as the configuration struct. The builder struct has, for every ordinary field1 of type T in the configuration struct, a field with the same name and type Option<T>. For every sub-builder field in the configuration struct of type T, it has a field with the same name and the type TBuilder. These fields are private.

The builder struct implements serde::Serialize and serde::Deserialize. Every field is marked with #[serde(default)] to allow it to be absent in the toml configuration.

The builder struct implements derive_deftly::Deftly.

The builder struct derives Default, Clone, and Debug. It also has a new() method that calls Default::default().

The builder struct implements ExtendBuilder and Flattenable.

The configuration struct receives a generated “builder” method, of type fn () -> MyConfigBuilder. This function calls the builder’s Default::default() method.

For every ordinary1 field of type T in the configuration struct, the builder has a setter method with the same name, of type fn (&mut self, value: T) -> &mut Self. The setter method sets the corresponding field in self to Some(value), and returns `self``.

For every sub-builder field of type SubCfg in the configuration struct, the builder has an accessor method with the same name, returning &mut SubCfgBuilder.

The builder has a build method, with the name build, of type fn (&self) -> Result<MyConfig, ConfigBuildError>. It has the same visibility as the builder struct. For every field in the builder of value Some(x), it sets the corresponding field in the configuration object to x. For every field in the builder of value None, it sets the corresponding field in the configuration object to its default, or returns an error, depending on the attributes set on the field. For every field in the builder with a sub-builder, it invokes the build method on that sub-builder, and sets the corresponding field in the configuration object to its result.

A new #[cfg(test)] module is generated, with tests for the builder behavior. This module is called test_my_config_builder, with my_config replaced with the snake-case name of your actual configuration type.


  1. For the purpose of this documentation, a field is “ordinary” if it does not have a sub-builder↩ 1 2