From 961f6b527e765a0d554165f7c85134e13e3a8907 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 25 Apr 2022 13:36:13 +0100 Subject: [PATCH] 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. --- Cargo.lock | 1 + crates/tor-config/Cargo.toml | 1 + crates/tor-config/src/lib.rs | 2 ++ crates/tor-config/src/list_builder.rs | 15 +++++++++++++-- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4d4aa13c9..0f386e3ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3342,6 +3342,7 @@ dependencies = [ "serde", "shellexpand-fork", "thiserror", + "tor-basic-utils", "tor-error", "tracing", "tracing-test", diff --git a/crates/tor-config/Cargo.toml b/crates/tor-config/Cargo.toml index 0e0c91cef..543c8c6d1 100644 --- a/crates/tor-config/Cargo.toml +++ b/crates/tor-config/Cargo.toml @@ -15,6 +15,7 @@ default = ["expand-paths"] expand-paths = ["shellexpand", "directories"] [dependencies] +tor-basic-utils = { path="../tor-basic-utils", version = "0.2.0"} tor-error = { path="../tor-error", version = "0.2.0"} thiserror = "1" diff --git a/crates/tor-config/src/lib.rs b/crates/tor-config/src/lib.rs index 26c7b6f09..62d2e17d0 100644 --- a/crates/tor-config/src/lib.rs +++ b/crates/tor-config/src/lib.rs @@ -55,6 +55,8 @@ pub use err::{ConfigBuildError, ReconfigureError}; pub use mut_cfg::MutCfg; pub use path::CfgPath; +pub use tor_basic_utils::macro_coalesce_args; + /// Rules for reconfiguring a running Arti instance. #[derive(Debug, Clone, Copy, Eq, PartialEq)] #[non_exhaustive] diff --git a/crates/tor-config/src/list_builder.rs b/crates/tor-config/src/list_builder.rs index eaac017fc..33688df64 100644 --- a/crates/tor-config/src/list_builder.rs +++ b/crates/tor-config/src/list_builder.rs @@ -17,6 +17,10 @@ /// nontrivial, you should put the actual defaulting functionality in a (probably-private) /// function, as the macro will expand it twice. /// +/// The `item_build` clause, if supplied, provides a closure with type +/// `FnMut(&ThingBuilder) -> Result`; the default is to call +/// `thing_builder.build()`. +/// /// ``` /// use derive_builder::Builder; /// use serde::Deserialize; @@ -59,6 +63,7 @@ macro_rules! define_list_config_builder { } built: $Built:ty = $built:expr; default = $default:expr; + $( item_build: $item_build:expr; )? } => { $($docs_and_attrs)* #[derive(Default, Clone, Deserialize)] @@ -100,10 +105,16 @@ macro_rules! define_list_config_builder { &default_buffer } }; + let $things = $things .iter() - .map(|item| item.build()) - .collect::>()?; + .map( + $crate::macro_coalesce_args!{ + [ $( $item_build )? ], + [ |item| item.build() ], + } + ) + .collect::>()?; Ok($built) } }