From 7af417e8b18d161452e36693a0e1988688c0433e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 10 Jan 2023 01:39:02 +0000 Subject: [PATCH] Config inspection: Centralise loop Signed-off-by: Ian Jackson --- client/client.rs | 14 ++++++-------- server/server.rs | 3 ++- src/config.rs | 39 +++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/client/client.rs b/client/client.rs index 7aaebfb..b7d9d1e 100644 --- a/client/client.rs +++ b/client/client.rs @@ -344,14 +344,12 @@ async fn main() { process::exit(0); } if let Some(arg) = &opts.print_config { - for ic in &ics { - implement_print_config(ic, arg, &|k| Some(match k { - "link" => &ic.link, - "server" => &ic.link.server, - "client" => &ic.link.client, - k => return ic.inspect_key(k), - }))?; - } + implement_print_config(&mut ics.iter(), arg, &|ic, k| Some(match k { + "link" => &ic.link, + "server" => &ic.link.server, + "client" => &ic.link.client, + k => return ic.inspect_key(k), + }))?; process::exit(0); } diff --git a/server/server.rs b/server/server.rs index 231e8ef..15ba532 100644 --- a/server/server.rs +++ b/server/server.rs @@ -161,7 +161,8 @@ async fn async_main(opts: Opts, daemon: Option) { let global_config = config::InstanceConfigGlobal::from(&ics); if let Some(arg) = &opts.print_config { - implement_print_config(&global_config, arg, &|_| None)?; + implement_print_config(&mut iter::once(&global_config), + arg, &|_,__| None)?; process::exit(0); } diff --git a/src/config.rs b/src/config.rs index 8f95a5c..eab8815 100644 --- a/src/config.rs +++ b/src/config.rs @@ -96,22 +96,33 @@ pub trait InspectableConfig: Debug { } #[throws(AE)] -pub fn implement_print_config<'f>( - config: &dyn InspectableConfig, +pub fn implement_print_config<'c, C: InspectableConfig>( + configs: impl Iterator, arg: &str, - extra_key: &dyn Fn(&str) -> Option<&'f dyn InspectableConfigValue> + extra_key: &dyn Fn(&'c C, &str) -> Option<&'c dyn InspectableConfigValue> ) { - let output = arg - .split(',') - .map(|key| { - let insp = config.inspect_key(key) - .or_else(|| extra_key(key)) - .ok_or_else(|| anyhow!("unknown config key {:?}", key))?; - Ok::<_,AE>(DisplayInspectable(insp).to_string()) - }) - .collect::,_>>()? - .join(" "); - println!("{}", output); + #[throws(AE)] + pub fn print_one_config<'f>( + config: &dyn InspectableConfig, + arg: &str, + extra_key: &dyn Fn(&str) -> Option<&'f dyn InspectableConfigValue> + ) { + let output = arg + .split(',') + .map(|key| { + let insp = config.inspect_key(key) + .or_else(|| extra_key(key)) + .ok_or_else(|| anyhow!("unknown config key {:?}", key))?; + Ok::<_,AE>(DisplayInspectable(insp).to_string()) + }) + .collect::,_>>()? + .join(" "); + println!("{}", output); + } + + for config in configs { + print_one_config(config, arg, &|k| extra_key(config, k))?; + } } pub trait InspectableConfigValue { -- 2.30.2