Commit 961f6b52 authored by Ian Jackson's avatar Ian Jackson
Browse files

config list-builder: Allow overriding the per-item build method

This will be useful especially for simple lists where the entry
doesn't need a separate builder type.
parent d98d7a60
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -3342,6 +3342,7 @@ dependencies = [
 "serde",
 "serde",
 "shellexpand-fork",
 "shellexpand-fork",
 "thiserror",
 "thiserror",
 "tor-basic-utils",
 "tor-error",
 "tor-error",
 "tracing",
 "tracing",
 "tracing-test",
 "tracing-test",
+1 −0
Original line number Original line Diff line number Diff line
@@ -15,6 +15,7 @@ default = ["expand-paths"]
expand-paths = ["shellexpand", "directories"]
expand-paths = ["shellexpand", "directories"]


[dependencies]
[dependencies]
tor-basic-utils = { path="../tor-basic-utils", version = "0.2.0"}
tor-error = { path="../tor-error", version = "0.2.0"}
tor-error = { path="../tor-error", version = "0.2.0"}


thiserror = "1"
thiserror = "1"
+2 −0
Original line number Original line Diff line number Diff line
@@ -55,6 +55,8 @@ pub use err::{ConfigBuildError, ReconfigureError};
pub use mut_cfg::MutCfg;
pub use mut_cfg::MutCfg;
pub use path::CfgPath;
pub use path::CfgPath;


pub use tor_basic_utils::macro_coalesce_args;

/// Rules for reconfiguring a running Arti instance.
/// Rules for reconfiguring a running Arti instance.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[non_exhaustive]
#[non_exhaustive]
+13 −2
Original line number Original line Diff line number Diff line
@@ -17,6 +17,10 @@
/// nontrivial, you should put the actual defaulting functionality in a (probably-private)
/// nontrivial, you should put the actual defaulting functionality in a (probably-private)
/// function, as the macro will expand it twice.
/// function, as the macro will expand it twice.
///
///
/// The `item_build` clause, if supplied, provides a closure with type
/// `FnMut(&ThingBuilder) -> Result<Thing, ConfigBuildErro>`; the default is to call
/// `thing_builder.build()`.
///
/// ```
/// ```
/// use derive_builder::Builder;
/// use derive_builder::Builder;
/// use serde::Deserialize;
/// use serde::Deserialize;
@@ -59,6 +63,7 @@ macro_rules! define_list_config_builder {
        }
        }
        built: $Built:ty = $built:expr;
        built: $Built:ty = $built:expr;
        default = $default:expr;
        default = $default:expr;
        $( item_build: $item_build:expr; )?
    } => {
    } => {
        $($docs_and_attrs)*
        $($docs_and_attrs)*
        #[derive(Default, Clone, Deserialize)]
        #[derive(Default, Clone, Deserialize)]
@@ -100,10 +105,16 @@ macro_rules! define_list_config_builder {
                        &default_buffer
                        &default_buffer
                    }
                    }
                };
                };

                let $things = $things
                let $things = $things
                    .iter()
                    .iter()
                    .map(|item| item.build())
                    .map(
                    .collect::<Result<_, _>>()?;
                        $crate::macro_coalesce_args!{
                            [ $( $item_build )? ],
                            [ |item| item.build() ],
                        }
                    )
                    .collect::<Result<_, ConfigBuildError>>()?;
                Ok($built)
                Ok($built)
            }
            }
        }
        }